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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Template Questions /

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

i18n in templates


Go to End


3 Posts   1817 Views

Avatar
VRoxane

Community Member, 42 Posts

10 September 2010 at 5:03am

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');

Avatar
VRoxane

Community Member, 42 Posts

10 September 2010 at 7:10am

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
}

Avatar
bummzack

Community Member, 904 Posts

10 September 2010 at 8:55am

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 ;)