Interface translation in symfony

Shyamal Mehta

Symfony provides interface translation using the XLIFF standard. While using the XLIFF standard here is good, it has one pain point. The pain point becomes apparent when the need arises for changing/editing the phrases. If the phrase is used in several templates, you need to search all the templates and change there as well as in the dictionary.

For example, lets say we define a simple label like “Username” in the dictionary and in several templates. The dictionary will look like:

<?xml version="1.0" ?>
<xliff version="1.0">
  <file orginal="global" source-language="en_US"
    datatype="plaintext">
    <body>
      <trans-unit id="1">
        <source>Username</source>
        <target>ユーザー名</target>
      </trans-unit>
    </body>
  </file>
</xliff>

And in all templates that use the label “Username” we will use the following to translate it based on user’s culture:

echo __('Username');

This is good, but lets say down the line we decide to use the label “User Id” instead of “Username”. To do that we need to update all the templates and the dictionary. This becomes cumbersome if you have a large number of templates that reference this label. If you notice, the cause of the problem lies in the fact that we have used the actual text itself to identify the i18n phrase within all templates.

To get around this problem, what we did was to use a code to identify the phrase and use that in the template. And we provide a dictionary for each of the supported language. So in this case we create an English and Japanese dictionary both. But what about the source language, you would ask. Well, thats a hack: we ended up providing a non existent ISO code. Based on the comment from Dennis, we use English as the source since translation happens even if source and target languages are same.

So here is what our English and Japanese dictionary look like:

<?xml version="1.0" ?>
<xliff version="1.0">
  <file orginal="global" source-language="en_US"
    datatype="plaintext">
    <body>
      <trans-unit id="1">
        <source>LABEL_USERNAME</source>
        <target>Username</target>
      </trans-unit>
    </body>
  </file>
</xliff>

<?xml version="1.0" ?>
<xliff version="1.0">
  <file orginal="global" source-language="en_US"
    datatype="plaintext">
    <body>
      <trans-unit id="1">
        <source>LABEL_USERNAME</source>
        <target>ユーザー名</target>
      </trans-unit>
    </body>
  </file>
</xliff>

And the phrase in the template looks like:

echo __('LABEL_USERNAME')

So now if you want to change the English version of the phrase, you simply update the dictionary and no need to change the templates.

about the author

Shyamal Mehta

Shyamal is an experienced technology professional having expertise on the web and mobile technologies. His strength lies in his ability to successfully and repeatedly deliver projects and products (large and small) for a variety of industries.

  1. Dennis

    March 10, 2008

    There is a more clean way (in my opinion) to do this:
    You don’t need a non-existing source code. It is possible to translate from source language to the same language as target.

  2. Shyamal

    March 10, 2008

    Thanks for the tip Dennis. Will update the main article with your suggestion.

  3. arvind

    July 16, 2008

    hi , i have done all the setting , but still it not working
    my setting is :
    is have used config/i18n/messages.hi.xml ,

    can u give me the code and procedure for changing the text code by symfony

  4. Shyamal

    July 16, 2008

    Arvind, you need to keep the files under the i18n directory directly under the root project folder. It is not to be kept in the config folder.

    So the directory will look something like:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    myProject
    |--apps
    |--batch
    |--config
    |--data
    |--i18n
    |&nbsp;&nbsp;|--messages.en.xml
    |&nbsp;&nbsp;|--messages.en.xml
    |
    |--lib
    |--web

    Alternatively, you could keep the whole i18n folder under the application folder, for example under

    1
    myProject/apps/frontend/i18n

  5. Forest Swade

    December 30, 2011

    That is a remarkably amazing powerful resource that you’re offering and you just provide it away cost-free!! I that can compare with discovering websites which often are aware of the particular property value providing you a wonderful learning resource for zero cost. We truly dearly loved examining these pages. Enjoy it!

  6. dresses

    November 27, 2012

    I guess, it comes down to 105599 simple choice!

  7. Billy Hemmings

    July 5, 2013

    Oooh, I like both of those. Which would you be more comfortable in? I know that you adore wearing tights and those types of tops, although the maxi would be pretty summery!