Thread tagged as: Question, Addons, Blog

Blog - Switching between languages, even when in a post


I am trying to set up a blog in which every post will be written in dutch and english. I have come across multiple ways of doing this, but I'm not sure which way will work best for me.

I would like to be able to switch language when reading a post, so that the title, content etc. will change when clicking on a flag that switches ?lang=nl to ?lang=en and vice versa. I'm guessing that my best option would be to work with conditionals inside my post.html, but will the url also change?

As you can tell I'm a bit confused now, what would be my best option?

  • Mike
Mike Hendriks

Mike Hendriks 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

but will the url also change?

Can you explain what you mean?

Well, if I am using the method of creating a seperate dutch post and an english post, they will both have a different URL. How would I be able to switch between both, when changing language?

Duncan Revell

Duncan Revell 78 points
Registered Developer

I think as you've found, it's not the easiest to set up blog with multiple languages.

Do you need to use blog? Using a standard page at least lets you use a separate region for each langauge, per page (or item).

Otherwise, the only thing I can think of is setting up multiple blogs or sections - one for each language - then you'd need to create a little field type to allow you to choose a post from each blog/section. You'd be manually choosing which language post to link the current post to (and therefore use to "switch" language).

I again think you're going to struggle with blog - if you're using a slug as the unique identifier and you don't want the url to change, which slug "wins" for the url - Dutch or English?

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

You can have one post for both languages. You need to work on making your edit form template to make it user-friendly though. For example, you can use the divider-before attribute to create some separation and collapsable sections.

Assuming Dutch is the default language:

<perch:blog id="postDescHTML" type="textarea" markdown="true" divider-before="Dutch" />
<perch:blog id="postDescHTML_en" type="textarea" markdown="true" divider-before="Englisgh" />

Each post would have a single slug. You can rely on URL parameters (?lang=nl and ?lang=en) to determine which language to use can work like you suggested. You can display the English post only when ?lang=en, otherwise display the default (Dutch).

perch_get('lang') should get you the value of the lang parameter. perch_get('lang', 'nl') should set lang to nl if there is no lang value in case you need it!

Pass the value to the template with PerchSystem::set_var(). On your post.php:

PerchSystem::set_var('lang', perch_get('lang', 'nl'));

And in your template you can check for lang:

<perch:if id="lang" value="en">
<!--* English post *-->
<perch:blog id="postDescHTML_en" type="textarea" markdown="true"  />
<!--* Default Language: Dutch post *-->
<perch:blog id="postDescHTML" type="textarea" markdown="true"  />