Translating OSClass plugins & themes

i18n is an abbreviation for __internationalization__. You can get more information in the wikipedia. OSClass uses the GNU gettext libraries.

In order to make a string translatable, in your application you have to just wrap the original string:

  • __($text) Looks for a translated version of $textand returns the result, i.e.:
    • __("Hello") ;
  • _e($text) Looks for a translated version of $textand echo the result to the screen (same as echo __($text))
    • _e("Hello") ;

However, in order to make the string translatable for plugins and themes, you should add an extra parameter in the functions:

  • __($text, $domain) and _e($text, $domain). The variable $domain would be the name of the plugin or the theme. In the case of the modern theme, it would be: __("Hello", "modern") and _e("Hello", "modern").

If you want to add variables in the string, the next code won’t work:

  • _e("Hello $name", "modern") ;

The string for translation are extracted from the sources, so the translators will see: Hello $name. However, in the application _e will be called with an argument like Hello Juan and gettext won’t find a suitable translation of this one and will return its argument: Hello Juan. the solution is to use printf. Here is the right solution:

  • printf(__("Hello %s", "modern"), $name) ;

Best practices

  • Entire sentences: in most languages word order is different.
  • Split at paragraphs: do not include whole page of text in one string.
  • Use sprintf and printf: sprintf(__('Replace %s with %s', 'modern'), $a, $b) ; is better than __('Replace ') . $a . __(' with ') . $b ;

Generating a PO file

This file is the one you need to translate your strings. There’s many ways to create a POT file, but we use POEdit. This article explains how to create .po file. Then, you should place in the right place the .mo file:

  • Themes: {theme folder}/languages/{locale}/theme.mo
  • Plugins: {plugin folder}/languages/{locale}/messages.mo

{locale} will be the ISO Code of the languages, i.e.: en_US, es_ES, de_DE, fr_FR, es_AR, etc.

Finally, if you want to override the flash messages, you should copy the file from oc-content/languages/{locale}/messages.po to oc-content/themes/{theme}/languages/{locale}/messages.po, modify the translation as you want and compile (with POEdit) the .po file (it will generate a .mo file).

Don’t hesitate of asking any doubts in the comments.

P.S.: Next tutorial we’ll talk about plurals

This entry was posted in Tutorial. Bookmark the permalink. Juan Ramón

2 Responses to Translating OSClass plugins & themes

  1. diasa says:

    Hallo Everybody,

    the language Hebrew was complete translated, and i wan you use this language for my shop, but the problem this language ist not approved,
    and i wantr know if is possible to use this although this is not approved.

    If ok how can i do thad please help me

    Thank

    Diasa

  2. Juan Ramón says:

    @Diasa: I don’t see the Hebrew language complete. Moreover, in the admin.osclass.org it hasn’t been started..