Jump to:

23581 Posts in 19403 Topics by 2893 members

General Questions

SilverStripe Forums » General Questions » Translatable Javascript

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 1927 Views
  • spierala
    Community Member
    80 Posts

    Translatable Javascript Link to this post


    I try to make my javascript messages translatable, but it does not work.

    I followed these instructions: http://doc.silverstripe.org/i18n?s=sprintf

    I added this line to my Requirements in Page.php

    Requirements::javascript(SAPPHIRE_DIR . "/javascript/i18n.js");

    Then i added the language files in js-version e.g. en_GB.js containing following code:

    if(typeof(ss) == 'undefined' || typeof(ss.i18n) == 'undefined') {
       console.error('Class ss.i18n not defined');
    } else {
       ss.i18n.addDictionary('en_GB', {
          'FormContact.MYENTITY' : "Delete Article?"

    Then I try to test the code in the Page.ss template via:

    <script type="text/javascript">

    But the alert is always empty. What went wrong?

    Many thx,

    //SS. 2.3.3

    //PS: I would like to have a "Translatable" Section or something similar in the Forum

  • martimiz
    Forum Moderator
    1091 Posts

    Re: Translatable Javascript Link to this post

    I've been running this as well, and it looks to be far more complex then it should be.

    1. Where did you place your en_GB.js language file? Supposing you put it in /mysite/lang/*.* you'd have to point SilverStripe to it in the init() method like:


    2. You need to provide a default en_US.js file at all times, so there's something to fall back to, if the correct current locale (en_GB) is not recognized

    3. You should not hardcode the <script> tags in your template, since required scripts, like i18n.js, are loaded at the end of the page, so you want your customscript to be placed below that. In init():


    By now you should see the en_US default translation

    4. i18n.js tries to find out what the current locale is, by interrogating the content-language metattag. If that is not set, it falls back to the default locale, that is 'en_US', so you need to have this in your page.

    <meta http-equiv="Content-Language" content="en_GB" />

    5. And this is the downfall: it still might not test OK... In my case i18n.js does find the correct locale - but only after the alert has fired. Timing...

    I do support your request for a section i18n and translatable (which are not necessarily the same)

  • spierala
    Community Member
    80 Posts

    Re: Translatable Javascript Link to this post

    Oh nooo, sorry, i had my js files in the standard lang folder NOT in mysite/javascript/lang/
    Now everything works fine.

    is also important.

    I just want to translate Form Validation Code. Therefore I have fortunately no timing problems.
    Thanx for clarifying the topic.


  • Pike
    Community Member
    40 Posts

    Re: Translatable Javascript Link to this post

    DO NOT FORGET ADD to page.php function init():

    Requirements::insertHeadTags('<meta http-equiv="Content-language" content="' . i18n::get_locale() . '" />');

    to header meta tags. Without that, javascript localization not works.

  • ryanwachtl
    Community Member
    46 Posts

    Re: Translatable Javascript Link to this post

    In SilverStripe 3.0.x you can add a lang attribute to the body element and it will get picked up by the i18n JavaScript implementation. You may want to do it this way if you're using an HTML5 doctype and have already defined the locale within the lang attribute of the html element and don't want to use the, now obsolete, Content-Language meta element.

    <body lang="$ContentLocale">

Page: 1
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.