Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Redirect route including language

Started by Andre Allin 8 years ago · 17 replies · 1307 views
8 years ago

Trying to redirect same link on multilingual page to its proper languages files e.g.:

/en/link: 'someaddress.com/en/file.ext'
/cs/link: 'someaddress.com/cs/file.ext'

Is it possible to do it some easy way?

8 years ago

Share more details. U want to create a link in twig or markdown? Is it an external file u want to link?

8 years ago

the task was to create link with exact addres like mysite.com/cs/the-link which is pointing to pdf file
so i've managed to create redirect using site.yaml redirect section,
but then realized, that it doesn't work in other language e.g. mysite.com/en/the-link

I mean, it's pointing to same address file, in my case the cz pdf, but I have to pdfs, czech and english ...

last edited 07/12/18 by Andre Allin
8 years ago

In Twig you might try '/' ~ page.language ~ '/path/to/file.pdf'

8 years ago

If i link an img in a md file like this:

TXT
[link-name](blah.jpg)

then it is availaible by the following routes:
www.blah.de/de/subdir/blah.jpg
www.blah.de/en/subdir/blah.jpg
www.blah.de/subdir/blah.jpg

You can even disable the language in routes in the system settings.

👍 1
8 years ago

That's cool @npetri

I wonder, is twig that smart or has Grav added that smartness?

8 years ago

Haha, no twig cant handle routes. This is a really superveryverybiggestawesome builtin feature of grav. ;)

last edited 07/12/18 by Nico Petri
8 years ago

Just learned a word which wasn't in my vocabulary but found it in the urban dictionary...

8 years ago

I mean i need to put somehow to site.yaml
redirect:
mysite.com/en/mylink: 'absoluteurl/myfile-EN.pdf'
mysite.com/cz/mylink: 'absoluteurl/myfile-CZ.pdf'

I hope I've explained it correctly.

8 years ago

Ahhhh now i get u. But i have no idea if its possible. U are able to create any route to a page, but to a file i dont know.

8 years ago

no, in fact, redirecting in multilingual sites is done after the lang slash ... as written in docs

8 years ago

I was able to set a redirect to a particular file like this:

/de/hijpeg -> /subdir/hi.jpeg

The language indicator will be added automatically to the target. So u may try in your case:

/en/mylink: /myfile-EN.pdf
/cz/mylink: /myfile-CZ.pdf

But your pdf files need to be inside the pages folder.

8 years ago

A different approach...

I've added an onPageContentRaw function to the theme. The function searches the Markdown code for the PDF and replace it by a proper <a>

All pages contain the same language neutral Markdown code [PFD](/user/pages/images/mypdf.pdf).

For example, when the English page is requested by the user, the Markdown code [PDF](/user/pages/images/mypdf.pfp) will be replaced by <a href="/user/pages/images/mypdf-en.pdf">PDF</a>

All pdf files are stored in /user/pages/images and are named mypdf-en.pdf, mypdf-cz.pdf etc.

PHP
public function onPageContentRaw(Event $e)
{
    $content = $e['page']->getRawContent();
    // Regex of Markdown url pattern
    $needle = '/\[(.*?)\]\((\/user\/pages\/images\/mypdf)\.pdf\)/';
    // Regex of replace pattern
    $replace = '<a href="$2-' . $e['page']->language() . '.pdf">$1</a>';
    $content = preg_replace($needle, $replace, $content);

    $e['page']->setRawContent($content);
}

This code will only be called once when the page is being processed. Thereafter the page will be fetched from cache.

👍 1
8 years ago

hey, this one seems really elegant, but doest it really work for you exactly like this? I get 404 as I try it this way ///
my site.yaml:

redirects:
/en/gdpr: /GDPR_OKTOURS_EN.pdf
/cs/gdpr: /GDPR_OKTOURS_CZ.pdf

is it like this?

8 years ago

This actually isn't that bad of an idea :)
If you'd need to link different files (same files but other language..)
You could even do it by using the page.language in an if statement if you'd really want to,

TWIG
  {% if page.language == "en" %}
    href = someaddress.com/en/en_file.ext
  {% elseif page.language == "cs"
    href = someaddress.com/cs/cs_file.ext
  {% else %}
    href = provided_default.ext
  {% endif %}

Hope it helps you some way 🙂

👍 1
8 years ago

oh man, this is really cool but i need actually to serve the redirect, and this just process afterwards, you know, client have some address which is exactly the same ... e.g. mysite.com/en/gdpr ... and afterwards i can use your simple answer....eh

8 years ago

I was able able to define and test this configuration:

TXT
redirects:
/de/read-de: /externals/doc-de.pdf
/cz/read-cz: /externals/doc-cz.pdf

The de and cz language was added to system.yaml as a supported language. It works with 'Include default language' enabled or not.

8 years ago

There are always a few possibilties to solve a problem, in the end it doesn't really matther how you do it, it's good if it works 🙂

👍 1

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 80 9 hours ago
General · by pamtbaau, 15 hours ago
1 51 14 hours ago
General · by Andy Miller, 1 day ago
0 45 1 day ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 40 5 days ago