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

i18n::get_locale() and $this->Locale return different values


Go to End


3 Posts   3439 Views

Avatar
omarkohl

Community Member, 30 Posts

12 December 2010 at 3:20am

I have implemented a site with several languages just as described in the tutorial. But when trying to display text using the _t() function it always is displayed in the default locale language (spanish). I managed to get as far as finding out that this happens because _t() gets the locale from i18n::get_locale() and this function for some reason always returns the default language.

Just for testing I made a function in my Page.php that returns a string with the values of:
- i18n::get_locale()
- $this->Locale
And Bingo! On the english version of the page the first function returns es_ES while the second value is en_GB. On the spanish version both return es_ES.

Is this normal? Am I missing something? Can _t() not be used this way?

Avatar
omarkohl

Community Member, 30 Posts

12 December 2010 at 3:52am

I fixed it by adding
i18n::set_locale(Translatable::get_current_locale());
to the init() function of the Page_Controller.

As pointed out in this post http://silverstripe.org/general-questions/show/12186 this behaviour is a little strange and IMHO it should be enabled by default because its not clear in the documentation and being able to use _t() in the Template is kinda stupid when it doesn't work.

If anyone has a nice explanation why this works this way please reply anyway.

Avatar
bartvanirsel

Community Member, 96 Posts

3 September 2011 at 12:20am

Thanks for this tip, it helped me out.

This should be working by default, you're right about that.