Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

How to change i18::current_lang via URL?


Reply


6 Posts   1609 Views

Avatar
Martin Pales

Community Member, 19 Posts

29 April 2010 at 10:02am

Hello,

I am working on a site with 2 languages and it has a language chooser page to choose from 2 different homepages. When a homepage is selected, everything works just fine except 18n strings (I use them in templates via <% _t() %> ). How do I force i18n to look for a correct translation?
Setting the ?locale=... or hreflang=... in URL does not help.

Currently I use a dirty hack in HomePage.php index() to check the URLSegment and set locale with i18n::set_locale().
Is there a nicer way to do that or is it just a plain bug that the i18n does not follow the page language setting?

Thanks.

Harl

Avatar
netnoise

Community Member, 10 Posts

29 April 2010 at 8:56pm

Not sure, but try adding this to your config.php:

i18n::set_locale(Translatable::get_current_locale());

Avatar
Martin Pales

Community Member, 19 Posts

9 May 2010 at 10:23pm

Thanks! This indeed works as expected! However one would expect that this works automatically. Perhaps a bug in sapphire?

Avatar
netnoise

Community Member, 10 Posts

10 May 2010 at 7:13pm

Not a bug. I18N and Translatable are independent of each other. I18n is for backend/ui translation where Translatable allows to have website content in different languages. A bit confusing, though.

Avatar
Martin Pales

Community Member, 19 Posts

10 May 2010 at 7:32pm

But what about <% _t() %> in templates? That defines page content as well. And this translation ignores page's locale by default. I still consider that code snippet above a hack :-)

Avatar
netnoise

Community Member, 10 Posts

10 May 2010 at 7:34pm

:-)