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

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


Reply

3 Posts   1654 Views

Avatar
omarkohl

12 December 2010 at 3:20am Community Member, 30 Posts

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

12 December 2010 at 3:52am Community Member, 30 Posts

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

3 September 2011 at 12:20am Community Member, 94 Posts

Thanks for this tip, it helped me out.

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