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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

How to change i18::current_lang via URL?


Go to End


6 Posts   2568 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

:-)