Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » i18n in templates

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

Page: 1
Go to End
Author Topic: 1301 Views
  • VRoxane
    Avatar
    Community Member
    41 Posts

    i18n in templates Link to this post

    How do I translate text in templates ?

    Here's what I've done (With Martijn's help via IRC) :

    in Page.ss

    <% _t("Page.READMORE","Lire la suite") %>

    in mysite/lang/en_GB.php

    <?php

    /**
    * English (United Kingdom) language pack
    * @package sapphire
    * @subpackage i18n
    */

    i18n::include_locale_file('sapphire', 'en_US');

    global $lang;

    if(array_key_exists('en_GB', $lang) && is_array($lang['en_GB'])) {
       $lang['en_GB'] = array_merge($lang['en_US'], $lang['en_GB']);
    } else {
       $lang['en_GB'] = $lang['en_US'];
    }

    $lang['en_GB']['Page.ss']['READMORE'] = 'Read More';

    ?>

    and in_ config.php :

    ...
    // Set the site locale
    i18n::set_locale('fr_FR');

    // enable nested URLs for this site (e.g. page/sub-page/)
    SiteTree::enable_nested_urls();

    Translatable::set_default_locale('fr_FR');
    Object::add_extension('SiteTree', 'Translatable');
    Object::add_extension('SiteConfig', 'Translatable');

  • VRoxane
    Avatar
    Community Member
    41 Posts

    Re: i18n in templates Link to this post

    Thanks to Martijn and Zauberfisch on IRC Channel, here's what I missed :

    in Page.php

    public function init() {

    parent::init();

    if($this->dataRecord->hasExtension('Translatable'))
    i18n::set_locale($this->dataRecord->Locale); // set i18n language for _t() in template
    }

  • banal
    Avatar
    Community Member
    901 Posts

    Re: i18n in templates Link to this post

    If you have Translatable enabled, then a call like this (in init() should be sufficient:

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

    It's basically the same as you do now, just shorter ;)

    1301 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.