Tと国際化

□未翻訳

□翻訳中

□翻訳完了(細田謙二)

■レビュー(中垣健志)

T と国際化

Tオブジェクトは言語の翻訳機です。これは、web2pyのクラスであるgluon.language.translatorの単一のグローバルインスタンスから構成されています。すべての文字列定数は(そして文字列定数のみ)次の例のようにTによってマークされるべきです:

The object T is the language translator. It constitutes a single global instance of the web2py class gluon.language.translator. All string constants (and only string constants) should be marked by T, for example:

1.

a = T("hello world")

Tによってマークされた文字列は、言語の翻訳が必要なものとしてweb2pyによって特定され、(モデル、コントローラ、ビューの)コードが実行されたときに翻訳されます。翻訳する文字列が定数でなく変数の場合は、後で翻訳するために実行時(GAEを除く)に翻訳ファイルにその文字列が加えられます。

Strings that are marked with T are identified by web2py as needing language translation and they will be translated when the code (in the model, controller, or view) is executed. If the string to be translated is not a constant but a variable, it will be added to the translation file at runtime (except on GAE) to be translated later.

Tオブジェクトは補完される変数を次の例のように含むことができます:

The T object can also contain interpolated variables, for example:

1.

a = T("hello %(name)s", dict(name="Massimo"))

最初の文字列はリクエストされた言語のファイルに応じて翻訳され、name変数が言語と独立に置換されます。

The first string is translated according to the requested language file and the name variable is replaced independently of the language.

翻訳文字列を連結するのは良いアイデアではありません。そのため、web2pyは次のようにすることを許可していません:

Concatenating translation strings is not a good idea; this is why web2py does not allow you to do:

1.

T("blah ") + name + T(" blah") # invalid!

しかし、次のものは許可しています:

but it does allow:

1.

T("blah %(name)s blah", dict(name='Tim'))

次のような代替構文も可能です

or the alternative syntax

1.

T("blah %(name)s blah") % dict(name='Tim')

どちらの場合でも、翻訳は、"%(name)s"スロットの中で変数の名前が置換される前に発生します。次のような代替案は使うべきではありません:

In both cases the translation occurs before the variable name is substituted in the "%(name)s" slot. The following alternative should NOT BE USED:

1.

T("blah %(name)s blah" % dict(name='Tim'))

なぜなら、翻訳が置換後に発生するからです。

because translation would occur after substitution.

リクエストされた言語は、HTTPヘッダにある"Accept-Language"フィールドによって決められます。しかし、この選択は、次のように特定のファイルを要求することにより、プログラム上で上書きすることができます:

The requested language is determined by the "Accept-Language" field in the HTTP header, but this selection can be overwritten programmatically by requesting a specific file, for example:

1.

T.force('it-it')

これは"languages/it-it.py"の言語ファイルを読み取ります。言語ファイルは、管理インターフェースを介して作成、編集することができます。

which reads the "languages/it-it.py" language file. Language files can be created and edited via the administrative interface.

通常、文字列の変換は、ビューがレンダリングされるときに遅延的に評価されます。したがって、翻訳オブジェクトのforceメソッドはビュー内で呼び出してはいけません。

Normally, string translation is evaluated lazily when the view is rendered; hence, the translator force method should not be called inside a view.

遅延評価は次のようにして無効化することが可能です

It is possible to disable lazy evaluation via

1.

T.lazy = False

これにより、文字列は、現在のアクセプト、または、強制された言語に基づいて、T演算子によって直ちに翻訳されます。

In this way, strings are translated immediately by the T operator based on the currently accepted or forced language.

一般的な問題は次のとおりです。元のアプリケーションが英語で書かれいたとします。翻訳ファイル(たとえばイタリア語、"it-it.py")があり、かつ、HTTPクライアントが英語(en)とイタリア語(it-it)をこの順序で受け入れることを宣言していると想定してください。以下のような望まない状況が発生します:web2pyはデフォルトは英語で書かれていることは知りません。したがって、イタリア語の翻訳ファイルしか見つからないので、すべてをイタリア語(it-it)に翻訳するようにします。もし"it-it.py"ファイルが見つからなかったとすれば、デフォルトの言語の文字列(英語)が使用されたはずです。

A common issue is the following. The original application is in English. Suppose that there is a translation file (for example Italian, "it-it.py") and the HTTP client declares that it accepts both English (en) and Italian (it-it) in that order. The following unwanted situation occurs: web2py does not know the default is written in English (en). Therefore, it prefers translating everything into Italian (it-it) because it only found the Italian translation file. If it had not found the "it-it.py" file, it would have used the default language strings (English).

この問題には2つの解決方法があります。1つは、英語の翻訳ファイルを作成することですが、それは冗長で不要です。より良い方法は、web2pyにどの言語がデフォルトの言語の文字列を使用するかを知らせることです。これは次のようにして行うことができます:

There are two solutions for this problem: create a translation language for English, which would be redundant and unnecessary, or better, tell web2py which languages should use the default language strings (the strings coded into the application). This can be done with:

1.

T.set_current_languages('en', 'en-en')

これは、T.current_languagesに、翻訳が必要でない言語のリストを保存し、言語ファイルのリロードを実施します。

It stores in T.current_languages a list of languages that do not require translation and forces a reload of the language files.

なお、"it"と"it-it"はweb2pyのビューの観点からすると違う言語になります。それらの両方をサポートするためには、常に小文字の名前の、2つの翻訳ファイルが必要となります。他の言語についても同様です。

Notice that "it" and "it-it" are different languages from the point of view of web2py. To support both of them, one would need two translation files, always lower case. The same is true for all other languages.

現在受け入れ可能な言語は次の場所に格納されています

The currently accepted language is stored in

1.

T.accepted_language

T(...)は、単に文字列を変換するだけでなく、変数を翻訳できることに留意してください:

Mind that T(...) does not just translate strings but can also translated variables:

1.

2.

>>> a="test"

>>> print T(a)

この場合、翻訳される単語"test"は、見つからずまた、ファイルシステムが書き込み可能であれば、言語ファイルにある翻訳対象単語リストに追加されます。

In this case the word "test" in translated but, if not found and if the filesystem is writable, it will add it to the list of words to be translated in the language file.