Redirect route including language

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?

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

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 …

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

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

[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 Like

That’s cool @npetri

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

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

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

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.

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.

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

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.

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.

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 Like

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?

This actually isn’t that bad of an idea :slight_smile:
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,

  {% 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 :slight_smile:

1 Like

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

I was able able to define and test this configuration:

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.

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 :slight_smile:

1 Like