Jump to:

3401 Posts in 979 Topics by 855 members

Template Questions

SilverStripe Forums » Template Questions » Translating static content

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

Page: 1 2 3
Go to End
Author Topic: 4497 Views
  • Pixelspin
    Avatar
    Community Member
    9 Posts

    Translating static content Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: Translating static content Link to this post

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

  • Pixelspin
    Avatar
    Community Member
    9 Posts

    Re: Translating static content Link to this post

    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.

  • Pixelspin
    Avatar
    Community Member
    9 Posts

    Re: Translating static content Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: Translating static content Link to this post

    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

  • Pixelspin
    Avatar
    Community Member
    9 Posts

    Re: Translating static content Link to this post

    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.

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: Translating static content Link to this post

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

  • Pixelspin
    Avatar
    Community Member
    9 Posts

    Re: Translating static content Link to this post

    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?

    4497 Views
Page: 1 2 3
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.