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.

Template Questions

Translating static content


Reply

17 Posts   4882 Views

Avatar
Pixelspin

10 May 2011 at 3:02am Community Member, 9 Posts

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

21 June 2011 at 12:18am Community Member, 9 Posts

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

Avatar
silverseba

5 August 2011 at 2:25am Community Member, 26 Posts

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

5 August 2011 at 11:22pm (Last edited: 5 August 2011 11:27pm), Community Member, 5 Posts

@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

6 August 2011 at 3:30am Forum Moderator, 1091 Posts

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

6 August 2011 at 4:51am Community Member, 5 Posts

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

btw. I think it would be worth pointing out in silverstripe's [url=http://doc.silverstripe.org/sapphire/en/topics/i18n]i18n documentation[/url] that pages translated by translatable require that additional effort.

Avatar
BigMoose

22 November 2011 at 4:10pm Community Member, 19 Posts

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

25 November 2011 at 5:17pm Community Member, 19 Posts

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.