Skip to content

English plugin dev 1 4

semuel edited this page Nov 7, 2011 · 14 revisions

Plugin Localization

Localization Mechanics

Movable Type works around the globe, and we try to enable each user to work in his own native language.

Btw, because the word ‘Localization’ is so long, there is a abbreviation for that: L10N. Saying “that word with L in the beginning, N in the end, and 10 more letters that I don’t remember in the middle”.

Movable Type has been localized for the following languages:

  • Japanese
  • English
  • French
  • German
  • Spanish
  • Dutch

The default for user language is in System Overview→Settings→User→Default User Language. And then each user can set his own language in his profile page.

A phrases or keywords are translated to the target language. For example, in the “$MT_DIR/tmpl/cms/error.tmpl” template there is the following line:

<__trans phrase="Close">

The “Close” phrase will be translated, with the result replacing the entire __trans tag. (In past versions we used other tag name, MT_TRANS. it still works, but please use the newer style, with the __trans tag name)

The dictionary file for each language is placed below the “$MT_DIR/lib/MT/L10N” directory.

Language Dictionary file
Japanese ja.pm
English en_us.pm
French fr.pm
German de.pm
Spanish es.pm
Dutch nl.pm

Inside these files, translation are simply key-value combinations of original_phrase => translation. For example, the Close word is translated in the Japanese translation file ja.pm as:

'Close' => '閉じる',

If this phrase is not a simple word or sentence, we use “_” in the beginning of the phrase. for example, ‘_USAGE_FORGOT_PASSWORD_1’

Movable Typeの管理画面は、HTMLファイルのようなテンプレートファイル(TMPLファイル)を用意して、そこに表示する内容を定義します。(実際先ほど例に挙げた"$MT_DIR/tmpl/cms/error.tmpl"を見るとHTMLファイルに似た記述になっている事が分かります。)そして表示する際、テンプレートエンジンがTMPLファイルを解析し"<__trans>"タグがあれば辞書ファイルを参照して置き換えます。この際、指定されている言語の辞書ファイルに対応するフレーズが無い場合はデフォルトの英語の文言に置き換わります。また、指定した言語にも、英語辞書にも無い時はフレーズであればそのまま置き換え、"_"で始まるキーワードの場合はエラーとなります。

Translation resolution order:

  1. First the phrase will be searched in the current language file (i.e. ja.pm)
  2. If not found, it will be searched in the default language, English, in the en_us.pm file
  3. If still not found, for normal string it will be simply displayed, for a keyword (that start with ‘_’) there will be an error

In the case that the translation need parameters, such as page number or user name, use the “[_1]” syntax inside the phrase and the translation. for example:

'Pages: from [_1] to [_2]' => '[_1]~[_2]ページ',

For which you use in your templates:

<__trans phrase="Pages: from [_1] to [_2]" params='32%%64' >

And the output will be “32~64ページ”, with the parameters embedded in place. notice the “%%” separator between the different parameters. Also, in this case 32 and 64 are fixed values, but can be output of other template tags too.

In addition to this template tag there are also translation functions that can be used inside you Perl code for printing error massages and log in the configured language

So, how do I handle different languages in my plugin?

All the info above is true for plugins too. Just specify in the config.yaml file the directory of the translation dictionaries. (“l10n_class: MyPlugin02::L10N”)

config.yaml before adding L10N

id: MyPlugin02
name: Sample plugin L10N
version: 1.0
description: Sample plugin L10N
author_name: Plugin author
author_link: http://www.example.com/about/
doc_link: http://www.example.com/docs/

config.yaml after adding L10N

id: MyPlugin02
name: <__trans phrase="Sample plugin L10N">
version: 1.0
description: <__trans phrase="_PLUGIN_DESCRIPTION">
author_name: <__trans phrase="_PLUGIN_AUTHOR">
author_link: http://www.example.com/about/
doc_link: http://www.example.com/docs/
l10n_class: MyPlugin02::L10N

Writing L10N.pm

Just inherit from ‘MT::Plugin::L10N’, and let it handle everything:

package MyPlugin02::L10N;
use strict;
use base 'MT::Plugin::L10N';

1;

Writing en_us.pm

Create a file for your English translation. inherit from the ‘MyPlugin02::L10N’ class that we just wrote:

package MyPlugin02::L10N::en_us;

use strict;
use base 'MyPlugin02::L10N';
use vars qw( %Lexicon );

%Lexicon = (
    '_PLUGIN_DESCRIPTION' => 'Sample plugin L10N',
    '_PLUGIN_AUTHOR' => 'Plugin author',
);

1;

Writing ja.pm

Well, for this you need to know Japanese. And if so, did you know that we have a Japanese version of this documentation too? it is very good – take a look!
Create a file for your Japanese translation, that inherits from MyPlugin02::L10N::en_us

# Sample Plugin: Plugin L10N

package MyPlugin02::L10N::ja;

use strict;
use base 'MyPlugin02::L10N::en_us';
use vars qw( %Lexicon );

%Lexicon = (
    'Sample plugin L10N' => 'サンプルプラグイン L10N',
    '_PLUGIN_DESCRIPTION' => 'サンプルプラグイン L10N',
    '_PLUGIN_AUTHOR' => 'プラグイン作者',
);

1;

Directory Structure

This is how your plugin directory will look like now:

MT_DIR/
|__ plugins/
   |__ MyPlugin02/
      |__ config.yaml
      |__ lib/
         |_ MyPlugin02/
            |__ L10N.pm
            |_ L10N/
               |_ en_us.pm
               |_ ja.pm

View the result of your hard work

If all done correctly, change your language to Japanese and look on Tools→Plugins and open the configuration of this plugin. It should look like in this screen capture:

If you need help returning to English, click on your name in the upper-right corner and look for the combo-box of the languages. select English, and click the save button below

Summary

As much as we like English, think that everyone around the globe knows it, or should know it, people are just more comfortable working in their native language.

Also, Movable Type itself includes translation to French, German, Spanish and Dutch too. So even it is unrealistic to support all of them right out of the bat, at least we can have the infrastructure to add support to these languages easily in the future

Plugin Download

MyPlugin02.zip(1.86KB)

Navigation

Adding configuration directives << Index >> Next:Test Driven Development of Plugins

Clone this wiki locally