Jump to:

3434 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Translating static content

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

Page: 1 2 3
Go to End
Author Topic: 4753 Views
  • Pixelspin
    Avatar
    Community Member
    9 Posts

    Re: Translating static content Link to this post

    When I add "i18n::set_locale('en_GB');" it also finds the English translation, but it doesn't seem to get the difference between the sites. Now it even overwrites the Dutch translations. When viewing the source code it also shows the right languages in the "<html lang="nl-NL" lang="nl-NL" xmlns="http://www.w3.org/1999/xhtml">" line.

    What am I doing wrong? My config now looks like this;

    // Set the site locale
    Translatable::set_default_locale('nl_NL');

    Object::add_extension('SiteTree', 'Translatable');
    Object::add_extension('SiteConfig', 'Translatable'); // 2.4 or newer only

    i18n::set_locale('nl_NL');
    i18n::set_locale('en_GB');

  • Pixelspin
    Avatar
    Community Member
    9 Posts

    Re: Translating static content Link to this post

    Is there maybe somebody else who knows a solution to this problem?

  • silverseba
    Avatar
    Community Member
    26 Posts

    Re: Translating static content Link to this post

    You need to put that in your Templates:

    <% _t('Page.ss.SEARCH','Search') %>

    The reason for this is that you are calling an array.
    You stored your Translation in: $lang['nl_NL']['Page.ss']['SEARCH']
    You need to call _t('Page.ss.SEARCH')

    So what you basically do is seperate each value in the $lang array by a dot (.)

  • mv
    Avatar
    Community Member
    5 Posts

    Re: Translating static content Link to this post

    @Pixelspin, I'm having the exact same issue and it seems there's something messed with the silverstripe's implementation of i18n.

    Let me explain it one more time for the others:

    In my /mysite/ directory I've made a new one called /lang/. Inside I've created 2 files:
    es_ES.php, with the following contents:

    <?php
    global $lang;
    $lang['es_ES']['Header']['HELLO_MSG'] = 'Hola';
    ?>


    en_US.php, with the following contents:

    <?php
    global $lang;
    $lang['en_US']['Header']['HELLO_MSG'] = 'Hello';
    ?>

    Dynamic content on the website is already translated using the Translatable class, so now when I go to http://<domain>/hello it shows me dynamic content in English, and correspondingly the Spanish content at http://<domain>/hola

    Here's the problem with i18n - it doesn't change the locale automatically, even though the site engine knows what language is being displayed. My _config.php file contains the line: i18n::set_locale('es_ES'); and it feels like i18n takes for granted that's the current language all the time.

    When the Page.ss template contains this:

    <% _t('Header.CHOOSE_COUNTRY') %> $ContentLocale


    I always get the hello message in Spanish, no matter which language version of page I'm browsing (/hola or /hello), whilst the $ContentLocale changes depending on the current lang.

    I know I could call the i18n::set_locale method in a controller and therefore force the website to show content in the right language but shouldn't this be an automatic behavior? After all if $ContentLocale is set properly upon every request, then why the locale isn't?

    I'm using the latest stable SS version 2.4.5, and XAMPP 1.7.3.

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: Translating static content Link to this post

    Hi there

    SilverStripe doesn't automatically set the correct i18n locale for pages translated by translatable. Try and put the following in your Page_Controller init() function, to force the correct locale for the current page:

    public function init() {
       parent::init();

       if($this->dataRecord->hasExtension('Translatable')) {
          i18n::set_locale($this->dataRecord->Locale);
       }
       ...

    Also read the SilverStripe Translatable documentation on github:

    https://github.com/silverstripe/silverstripe-translatable/blob/master/docs/en/index.md

  • mv
    Avatar
    Community Member
    5 Posts

    Re: Translating static content Link to this post

    Thanks for help martimiz, your solution works like a charm.

    btw. I think it would be worth pointing out in silverstripe's i18n documentation that pages translated by translatable require that additional effort.

  • BigMoose
    Avatar
    Community Member
    19 Posts

    Re: Translating static content Link to this post

    martimiz, I know you answered months ago, but this solution saved me today. I was completely stumped until I stumbled upon this little gem. Worked perfectly. Thanks.

  • BigMoose
    Avatar
    Community Member
    19 Posts

    Re: Translating static content Link to this post

    Spoke too soon. All my translations are working perfectly, but the javascript translations for the validator - eg (Please fill out "%s", it is required) are no longer switching languages along with everything else.

    4753 Views
Page: 1 2 3
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.