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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

Translating static content


Go to End


17 Posts   10341 Views

Avatar
Pixelspin

Community Member, 9 Posts

10 May 2011 at 3:02am

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" xml: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');

Avatar
Pixelspin

Community Member, 9 Posts

21 June 2011 at 12:18am

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

Avatar
silverseba

Community Member, 26 Posts

5 August 2011 at 2:25am

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 (.)

Avatar
mv

Community Member, 5 Posts

5 August 2011 at 11:22pm

Edited: 05/08/2011 11:27pm

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

Avatar
martimiz

Forum Moderator, 1391 Posts

6 August 2011 at 3:30am

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

Avatar
mv

Community Member, 5 Posts

6 August 2011 at 4:51am

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.

Avatar
BigMoose

Community Member, 19 Posts

22 November 2011 at 4:10pm

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.

Avatar
BigMoose

Community Member, 19 Posts

25 November 2011 at 5:17pm

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.