Jump to:

23355 Posts in 17838 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » i18n::get_locale() and $this->Locale return different values

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

Page: 1
Go to End
Author Topic: 1573 Views
  • omarkohl
    Avatar
    Community Member
    30 Posts

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

    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?

  • omarkohl
    Avatar
    Community Member
    30 Posts

    Re: i18n::get_locale() and $this->Locale return different values Link to this post

    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.

  • bartvanirsel
    Avatar
    Community Member
    94 Posts

    Re: i18n::get_locale() and $this->Locale return different values Link to this post

    Thanks for this tip, it helped me out.

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

    1573 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.