Jump to:

23007 Posts in 11866 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » How to change i18::current_lang via URL?

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: 1426 Views
  • Martin Pales
    Avatar
    Community Member
    19 Posts

    How to change i18::current_lang via URL? Link to this post

    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

  • netnoise
    Avatar
    Community Member
    10 Posts

    Re: How to change i18::current_lang via URL? Link to this post

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

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

  • Martin Pales
    Avatar
    Community Member
    19 Posts

    Re: How to change i18::current_lang via URL? Link to this post

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

  • netnoise
    Avatar
    Community Member
    10 Posts

    Re: How to change i18::current_lang via URL? Link to this post

    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.

  • Martin Pales
    Avatar
    Community Member
    19 Posts

    Re: How to change i18::current_lang via URL? Link to this post

    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

  • netnoise
    Avatar
    Community Member
    10 Posts
    1426 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.