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

29 April 2011 at 2:43am

Hello everyone,

I have looked all over but can't seem to find a way to easily translate static content in my templates. For example, I have a ProductPage with information about a certain product with a button below to order the product. In Dutch, this button needs to say "Product bestellen" but in the English version it would need to say "Order product". Is there some if statement you can use?

Best regards,
Steven

Avatar
Willr

Forum Moderator, 5523 Posts

30 April 2011 at 1:46am

Have you had a look at http://doc.silverstripe.org/sapphire/en/topics/i18n?

You will want to use the _t('STRINGNAME', 'Default Value') statement (as of 2.4 and below, this has changed in 3.0).

Avatar
Pixelspin

Community Member, 9 Posts

30 April 2011 at 3:56am

Thanks, I will ty it out! I came across that page, but it wasn't really clear to me that this was the easiest way.

Avatar
Pixelspin

Community Member, 9 Posts

5 May 2011 at 8:51am

I am trying but I can't find the right way to get the translation. All I need is for hard coded text like "Order this product" to be translated for the specific languages. According to the documentation I can use "<% _t('ORDER','Order this product') %>" for text in my SS templates, but what is my next step? I already set the locale to dutch (the default language) so that part in the _config.php looks like this;

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

(Do I also need to add English if that is the second language?)

Then it seems like I need to collect all the text, but for that it seems I need PHPUnit. Or am I missing something?

Avatar
Willr

Forum Moderator, 5523 Posts

5 May 2011 at 11:24am

Yes you will need PHPUnit and run the text collector. All the documentation can be found on http://doc.silverstripe.org/sapphire/en/topics/i18n

Avatar
Pixelspin

Community Member, 9 Posts

6 May 2011 at 9:09am

I updated Pear to 1.9.2 for correct installation and installed PHPUnit. I am using xampp and there now is a directory called PHPUnit under C:/xampp/php/PEAR/

After that, I ran the text collector using http://localhost/project/dev/tasks/i18nTextCollectorTask but I still get the following error;

Fatal error: require_once() [function.require]: Failed opening required 'PHPUnit/Framework/TestResult.php' (include_path='C:\xampp\htdocs\sumerianoptics\site\web/sapphire;C:\xampp\htdocs\sumerianoptics\site\web/sapphire/parsers;C:\xampp\htdocs\sumerianoptics\site\web/sapphire/thirdparty;.;\xampp\php\PEAR') in C:\xampp\htdocs\sumerianoptics\site\web\sapphire\dev\SapphireTestReporter.php on line 2

And indeed, there is no TestResult.php in the directory.

I am mostly a frontend developer so this is a bit new to me. If all this help is a bit of a bother, please let me now, and I will try to solve this another way.

Avatar
martimiz

Forum Moderator, 1391 Posts

6 May 2011 at 10:15pm

Edited: 06/05/2011 10:15pm

No bother - these translating issues are often more complicated then it seems, and there definitely are some issues :-)

Using the TextCollectorTask just like that, will have it try and create new languagefiles for the entire framework/cms. I don't suppose you want that anyway, so it's better to do it on a per-module base. This works fine for any custom modules you want to translate and you'll find that you won't need PHPUnit at all:

http://mydomain/dev/tasks/i18nTextCollectorTask/?module=mymodule

This will try and create a lang/en_US.php file in your module so make sure it's writable! Next you translate that file by making a nl_NL.php copy and replace all en_US occurences in it by nl_NL. That's it!

One somewhat irritating issue: if you try to collect module=mysite, it will result in an error, because for no seemingly valid reason SilverStripe wants to add the Page singularname/pluralname translations to the sapphire/lang/en_US.php file, that (lucky for you) isn't writable, else it would overwrite the file with just these two items. To solve this you need to override the SiteTree provideI18nEntities() methods in your Page class.

Finally, you can always create languagefiles by hand and skip the TextCollector entirely... :-)

Why PHPUnit anyhow?
For Silverstripe to collect text from classes, it seems to need to instantiate them. Working the sapphire module, it then stumbles upon a class like CliTestReporter.php, that wants the PHPUnit. Hence the demand for PHPUnit, which the TextCollector itself doesn't use at all...

Avatar
Pixelspin

Community Member, 9 Posts

7 May 2011 at 6:09am

Thanks a lot. I am now trying to make the lang files by hand, but it seems I'm still missing something. My default language is Dutch, and the second language is English (UK). In the mysite/_config.php I have the following code;

-------------------------------
// Set the site locale
i18n::set_locale('nl_NL');

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

Translatable::set_default_locale('nl_NL');
-------------------------------

Then, I have a nl_NL.php and en_GB.php file in a mysite/lang directory with this line;

Dutch: $lang['nl_NL']['Page.ss']['SEARCH'] = 'Zoeken';
English: $lang['en_GB']['Page.ss']['SEARCH'] = 'Search';

Finally, I put this code in the themes/project/templates/Page.ss file; <% _t('SEARCH') %>

Now, when viewing the Dutch site it does show the Dutch text, but on the English site the text is not translated. Do I need to add something in the _config.php file?

Go to Top