| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Reviewed: no -->
- <!-- EN-Revision: 24249 -->
- <sect1 id="zend.translate.additional">
- <title>翻訳用の追加機能</title>
- <para>
- <classname>Zend_Translate</classname> がサポートする機能は他にもあります。
- ここに、追加情報をまとめました。
- </para>
- <sect2 id="zend.translate.additional.options">
- <title>アダプタのオプション</title>
- <para>
- すべてのアダプタで、オプションを使用できます。
- もちろん、アダプタによってオプションは異なります。
- アダプタを作成する際に、オプションを設定できます。
- すべてのアダプタで共通のオプションは '<emphasis>clear</emphasis>'
- で、これは、翻訳データを既存のものに追記するのかしないのかを指定します。
- 標準の動作は、新しい翻訳を既存の翻訳に追記します。
- しかし、これを指定すると、既存の翻訳データをいったん消去して
- 新しいデータを追加します。消去されるのは選択した言語のみであり、
- その他の言語は影響を受けません。
- </para>
- <para>
- <methodname>addTranslation()</methodname>に与えることによって、
- 一時的にオプションをセットできます。
- そして、オプションを固定で設定するには、メソッド <methodname>setOptions()</methodname>を使えます。
- </para>
- <example id="zend.translate..additional.options.example">
- <title>翻訳オプションの使用</title>
- <programlisting language="php"><![CDATA[
- // ':' を、翻訳ソースファイルの区切り文字として指定します
- $translate = new Zend_Translate(
- array(
- 'adapter' => 'csv',
- 'content' => '/path/to/mytranslation.csv',
- 'locale' => 'de',
- 'delimiter' => ':'
- )
- );
- ...
- // 定義されている言語を消去し、新しい翻訳データを使用します
- $translate->addTranslation(
- array(
- 'content' => '/path/to/new.csv',
- 'locale' => 'fr',
- 'clear' => true
- )
- );
- ]]></programlisting>
- </example>
- <para>
- 各アダプタで使用できるオプションについて、
- その使用法を以下にまとめます。
- </para>
- <table id="zend.translate.additional.options.alloptions">
- <title>翻訳アダプタのオプション</title>
- <tgroup cols="4">
- <thead>
- <row>
- <entry>オプション</entry>
- <entry>アダプタ</entry>
- <entry>説明</entry>
- <entry>デフォルト値</entry>
- </row>
- </thead>
- <tbody>
- <row>
- <entry>adapter</entry>
- <entry><classname>Zend_Translate</classname> のみ</entry>
- <!-- TODO : to be translated -->
- <entry>
- Defines the adapter which will be used for the translation. This option
- can only be given when a new instance of
- <classname>Zend_Translate</classname> is created. When it is set
- afterwards, then it will be ignored
- </entry>
- <entry><emphasis>Must be set as it has no default value</emphasis></entry>
- </row>
- <row>
- <entry>clear</entry>
- <entry>すべて</entry>
- <entry>
- <constant>TRUE</constant> にすると、既に読み込んでいる翻訳を消去します。
- 新しい翻訳データを読み込む際に、
- 新しいインスタンスを作成する代わりに使用します。
- </entry>
- <entry><emphasis><constant>FALSE</constant></emphasis></entry>
- </row>
- <row>
- <entry>cache</entry>
- <entry>すべて</entry>
- <!-- TODO : to be translated -->
- <entry>
- Sets a cache for the translation adapter. This must be a instance of
- <classname>Zend_Cache_Core</classname>
- </entry>
- <entry>
- <emphasis>Per default no cache is set</emphasis>
- </entry>
- </row>
- <row>
- <entry>content</entry>
- <entry>all</entry>
- <entry>
- Sets the content for the translation adapter. This could be an array,
- a filename or a directory. Which type of content is supported depends
- on the used adapter
- </entry>
- <entry>
- <emphasis>The default value depends on the used adapter</emphasis>
- </entry>
- </row>
- <row>
- <entry>disableNotices</entry>
- <entry>すべて</entry>
- <entry>
- <constant>TRUE</constant> に設定すると、翻訳が存在しないことについての注意メッセージを無効にします。
- 実運用環境ではこのオプションを <constant>TRUE</constant> に設定しなければなりません。
- </entry>
- <entry><emphasis><constant>FALSE</constant></emphasis></entry>
- </row>
- <row>
- <entry>ignore</entry>
- <entry>すべて</entry>
- <entry>
- このプレフィックスで始まるすべてのディレクトリとファイルが、
- ファイルを探す際に無視されます。この値のデフォルトは
- <emphasis>'.'</emphasis>
- で、すべての隠しファイルを無視するようになります。
- この値を <emphasis>'tmp'</emphasis> に設定すると、たとえば 'tmpImages' や 'tmpFiles'
- といった名前のファイルやディレクトリ
- (とその配下のすべてのディレクトリ) を無視します。
- <!-- TODO : to be translated -->
- This option also accepts an array which can be used when
- you want to ignore more than one prefix.
- </entry>
- <entry><emphasis>.</emphasis></entry>
- </row>
- <row>
- <entry>log</entry>
- <entry>すべて</entry>
- <entry>
- 未翻訳のメッセージや注意が書き込まれる
- <classname>Zend_Log</classname> のインスタンス
- </entry>
- <entry><emphasis><constant>NULL</constant></emphasis></entry>
- </row>
- <row>
- <entry>logMessage</entry>
- <entry>すべて</entry>
- <entry>
- ログに書き込まれるメッセージ
- </entry>
- <entry>
- <emphasis>'%locale%': %message% 内で翻訳されていないメッセージ</emphasis>
- </entry>
- </row>
- <row>
- <entry>logPriority</entry>
- <entry>すべて</entry>
- <entry>ログにメッセージを書き込むために使用される優先度</entry>
- <entry>
- <emphasis>5</emphasis>
- </entry>
- </row>
- <row>
- <entry>logUntranslated</entry>
- <entry>すべて</entry>
- <entry>
- このオプションを <constant>TRUE</constant> に設定すると、翻訳できなかったすべてのメッセージ ID
- が添付のログに書き込まれます。
- </entry>
- <entry><emphasis><constant>FALSE</constant></emphasis></entry>
- </row>
- <row>
- <entry>reload</entry>
- <entry>すべて</entry>
- <entry>
- このオプションを <constant>TRUE</constant> に設定すると、ファイルはキャッシュに再読み込みされます。
- このオプションはキャッシュの再作成や、
- キャッシュが既に作成された後で、キャッシュ済みのデータに翻訳を追加するために
- 使えます。
- </entry>
- <entry><emphasis><constant>FALSE</constant></emphasis></entry>
- </row>
- <row>
- <entry>route</entry>
- <entry>すべて</entry>
- <!-- TODO : to be translated -->
- <entry>
- This option allows to use reroute from non existing translations to
- other languages. See the <link
- linkend="zend.translate.additional.rerouting">Rerouting
- Section</link> for details about this option.
- </entry>
- <entry><emphasis><constant>NULL</constant></emphasis></entry>
- </row>
- <row>
- <entry>scan</entry>
- <entry>すべて</entry>
- <entry>
- <constant>NULL</constant> にすると、ディレクトリ構造のスキャンを行いません。
- <constant>Zend_Translate::LOCALE_DIRECTORY</constant> にすると、
- ディレクトリからロケールを検出します。
- <constant>Zend_Translate::LOCALE_FILENAME</constant> にすると、
- ファイル名からロケールを検出します。
- 詳細は <link linkend="zend.translate.additional.detection">この章</link>
- を参照ください。
- </entry>
- <entry><emphasis><constant>NULL</constant></emphasis></entry>
- </row>
- <row>
- <entry>tag</entry>
- <entry>すべて</entry>
- <!-- TODO : to be translated -->
- <entry>
- Sets an individual tag which is used for the attached cache. Using this
- option allows to use and clear the cache for single instances. When this
- option is not set, the attached cache is used for all instances combined
- </entry>
- <entry><emphasis><classname>Zend_Translate</classname></emphasis></entry>
- </row>
- <row>
- <entry>delimiter</entry>
- <entry>Csv</entry>
- <entry>
- ソースと翻訳を区切る際に使用する記号を指定します。
- </entry>
- <entry><emphasis>;</emphasis></entry>
- </row>
- <row>
- <entry>enclosure</entry>
- <entry>Csv</entry>
- <entry>
- 値を囲むための文字を定義します。デフォルトはダブルクォートです。
- </entry>
- <entry><emphasis>"</emphasis></entry>
- </row>
- <row>
- <entry>length</entry>
- <entry>Csv</entry>
- <entry>
- CSV の行の長さの最大値を定義します。0 にすると、自動的に検出します。
- </entry>
- <entry><emphasis>0</emphasis></entry>
- </row>
- <row>
- <entry>useId</entry>
- <entry>Xliff 及び Tmx</entry>
- <entry>
- もしこのオプションを <constant>FALSE</constant> に設定すると、
- ソースの文字列はメッセージ Id として使われます。
- このオプションの既定値は <constant>TRUE</constant> で、
- それは, 翻訳単位要素からの Id がメッセージ Id として使われることを意味します。
- </entry>
- <entry><emphasis><constant>TRUE</constant></emphasis></entry>
- </row>
- </tbody>
- </tgroup>
- </table>
- <para>
- 自分でオプションを定義すれば、それをすべてのアダプタで使用できます。
- オプションを定義するには <methodname>setOptions()</methodname>
- メソッドを使用します。<methodname>setOptions()</methodname>
- には、指定したいオプションの配列を渡します。
- 指定したオプションがすでに存在する場合は、上書きされます。
- 存在しないオプションを指定した場合はアダプタは何もしないので、
- 必要となるであろうオプションはすべて指定しておけます。
- アダプタが使用している既存オプションは上書きされないことに注意してください。
- </para>
- <para>
- 現在設定されているオプションを取得するには <methodname>getOptions()</methodname>
- メソッドを使用します。<methodname>getOptions()</methodname>
- をパラメータなしでコールすると、すべてのオプションを返します。
- オプションのパラメータを指定した場合は、
- 特定のオプションの内容のみを返します。
- </para>
- </sect2>
- <sect2 id="zend.translate.additional.languages">
- <title>言語の処理</title>
- <para>
- 複数の言語を使用する場合に便利なメソッドを紹介します。
- </para>
- <para>
- <methodname>getLocale()</methodname> メソッドを使用すると、
- 実際に設定されている言語を取得できます。
- これは、<classname>Zend_Locale</classname>
- のインスタンスかロケール ID のいずれかとなります。
- </para>
- <para>
- <methodname>setLocale()</methodname> メソッドは、
- 翻訳用の新しい標準言語を設定します。
- これを使用すると、<methodname>translate()</methodname>
- に毎回オプションの言語パラメータを指定する必要がなくなります。
- 指定した言語が存在しない場合やその言語用の翻訳データがない場合、
- もし地域指定のない言語があれば <methodname>setLocale()</methodname>
- は代わりにそれを使用しようとします。つまり、たとえば
- <emphasis>en_US</emphasis> の場合だと代わりに <emphasis>en</emphasis>
- を使用するということです。これも見つからない場合は、
- 例外をスローします。
- </para>
- <para>
- <methodname>isAvailable()</methodname> メソッドは、
- 指定した言語が既に存在するかどうかを調べます。
- 指定した言語のデータが存在する場合に <constant>TRUE</constant>
- を返します。
- </para>
- <para>
- また、<methodname>getList()</methodname> メソッドを使用すると、
- そのアダプタに設定されている言語の一覧を配列で取得できます。
- </para>
- <example id="zend.translate.additional.languages.example">
- <title>アダプタの言語の処理</title>
- <programlisting language="php"><![CDATA[
- // 現在設定されている言語を返します
- $actual = $translate->getLocale();
- // 翻訳時にオプションのパラメータで言語を指定できます
- echo $translate->_("my_text", "fr");
- // あるいは新しい言語を設定することもできます
- $translate->setLocale("fr");
- echo $translate->_("my_text");
- // 基底言語を参照します
- // fr_CH は fr となります
- $translate->setLocale("fr_CH");
- echo $translate->_("my_text");
- // この言語が存在するかどうかを調べます
- if ($translate->isAvailable("fr")) {
- // 存在します
- }
- ]]></programlisting>
- </example>
- <sect3 id="zend.translate.additional.languages.automatic">
- <title>言語の自動処理</title>
- <para>
- 新しい翻訳ソースの追加を <methodname>addTranslation()</methodname>
- メソッドでのみ行っている場合は、自動ロケール
- '<emphasis>auto</emphasis>' あるいは '<emphasis>browser</emphasis>'
- を使用していれば <classname>Zend_Translate</classname>
- が環境にあわせて適切な言語を自動設定します。
- つまり、通常は <methodname>setLocale()</methodname> をコールする必要はありません。
- これは、自動ソース検出と組み合わせて使用しなければなりません。
- </para>
- <para>
- ユーザのブラウザやサーバの環境に応じて、最適なロケールを探します。
- 詳細は、以下の例を参照ください。
- </para>
- <example id="zend.translate.additional.languages.automatic.example">
- <title>言語の自動検出の動作例</title>
- <programlisting language="php"><![CDATA[
- // ブラウザから返される言語設定は次のようなものであると仮定します
- // HTTP_ACCEPT_LANGUAGE = "de_AT=1;fr=1;en_US=0.8";
- // 例 1:
- // 適切な言語がみつからないので、メッセージ ID を返します
- $translate = new Zend_Translate(
- array(
- 'adapter' => 'gettext',
- 'content' => 'my_it.mo',
- 'locale' => 'auto',
- 'scan' => Zend_Translate::LOCALE_FILENAME
- )
- );
- // 例 2:
- // 適切な言語は 'fr' となります
- $translate = new Zend_Translate(
- array(
- 'adapter' => 'gettext',
- 'content' => 'my_fr.mo',
- 'locale' => 'auto',
- 'scan' => Zend_Translate::LOCALE_FILENAME
- )
- );
- // 例 3:
- // 適切な言語は 'de' となります。'de_AT' の代替言語は 'de' だからです
- $translate = new Zend_Translate(
- array(
- 'adapter' => 'gettext',
- 'content' => 'my_de.mo',
- 'locale' => 'auto',
- 'scan' => Zend_Translate::LOCALE_FILENAME
- )
- );
- // 例 4:
- // 翻訳ソースとして 'it' を返し、自動設定を上書きします
- $translate = new Zend_Translate(
- array(
- 'adapter' => 'gettext',
- 'content' => 'my_it.mo',
- 'locale' => 'auto',
- 'scan' => Zend_Translate::LOCALE_FILENAME
- )
- );
- $translate->addTranslation(array('content' => 'my_ru.mo', 'locale' => 'ru'));
- $translate->setLocale('it_IT');
- ]]></programlisting>
- </example>
- <para>
- <methodname>setLocale()</methodname> メソッドで言語を手動設定したら、
- 自動設定機能は無効となります。
- </para>
- <para>
- 自動検出を再度有効にしたい場合は、<methodname>setLocale()</methodname>
- で言語として <emphasis>auto</emphasis>
- を指定します。これにより、<classname>Zend_Translate</classname>
- の自動検出機能が再度有効になります。
- </para>
- <para>
- Zend Framework 1.7.0 以降では、<classname>Zend_Translate</classname>
- はアプリケーション単位でのロケールの使用にも対応します。
- そのためには、<classname>Zend_Locale</classname>
- のインスタンスを以下のようにレジストリに登録します。
- このようにすれば、同じロケールを何度も使用したいときに
- 各インスタンスで毎回ロケールを設定する手間を省けます。
- </para>
- <programlisting language="php"><![CDATA[
- // 起動ファイルで
- $locale = new Zend_Locale();
- Zend_Registry::set('Zend_Locale', $locale);
- // リクエストされた言語が存在しない場合のデフォルト言語
- $defaultlanguage = 'en';
- // アプリケーションのどこかで
- $translate = new Zend_Translate(
- array('adapter' => 'gettext', 'content' => 'my_de.mo')
- );
- if (!$translate->isAvailable($locale->getLanguage())) {
- // 存在しない言語をリクエストされた場合はデフォルト設定を使用します
- $translate->setLocale($defaultlanguage);
- }
- $translate->getLocale();
- ]]></programlisting>
- </sect3>
- <sect3 id="zend.translate.additional.languages.territory">
- <title>言語として国を使用</title>
- <!-- TODO : to be translated -->
- <para>
- You can also use a country as locale parameter. This could be useful when you
- provide your user with flags, which represent the country in which he lives, and
- when he selects his flag, he would automatically get the default language for this
- country.
- </para>
- <para>
- For example, when the user selected <emphasis>US</emphasis> then you would get
- <emphasis>en_US</emphasis> in return as locale which is being used. This leads
- automatically to the language <emphasis>en</emphasis> which is the default language
- for the country <emphasis>US</emphasis>.
- </para>
- <programlisting language="php"><![CDATA[
- $translate = new Zend_Translate(
- array(
- 'adapter' => 'gettext',
- 'content' => 'my_de.mo',
- 'locale' => 'US'
- )
- );
- ]]></programlisting>
- <note>
- <title>Always uppercase countries</title>
- <para>
- Using this syntax you should always uppercase the input when you know that it's
- a country. The reason is that there are also languages which have the same
- letters as a country. Take for example <emphasis>om</emphasis>. You could expect
- to get <emphasis>ar_OM</emphasis> when you mean the country "Oman", or you could
- expect the language "Oromo" which is spoken in Kenia for example.
- </para>
- <para>
- As <classname>Zend_Translate</classname> is related to languages it will always
- use the language in this case. Therefor always uppercase the locale when you
- want it to be recognised as country.
- </para>
- </note>
- </sect3>
- </sect2>
- <sect2 id="zend.translate.additional.detection">
- <title>自動的なソース検出</title>
- <para>
- <classname>Zend_Translate</classname> は、翻訳ソースを自動的に検出できます。
- つまり、各ソースファイルを手動で宣言する必要はないということです。
- そんな作業は <classname>Zend_Translate</classname> に任せてしまい、
- ディレクトリ内からソースファイルを見つけさせることができるのです。
- </para>
- <note>
- <para>
- 自動的なソース検出機能は、Zend Framework バージョン 1.5
- 以降で使用可能です。
- </para>
- </note>
- <para>
- 使用法は、翻訳ソースを個別に登録していくのとほとんど同じですが、
- ひとつだけ違う点があります。ファイル名の代わりに、
- ソースを探すディレクトリを指定するのです。
- </para>
- <example id="zend.translate.additional.languages.directory.example">
- <title>ディレクトリを指定してソースを探す</title>
- <programlisting language="php"><![CDATA[
- // 以下のようなディレクトリ構造があることを想定しています
- // /language/
- // /language/login/login.tmx
- // /language/logout/logout.tmx
- // /language/error/loginerror.tmx
- // /language/error/logouterror.tmx
- $translate = new Zend_Translate(
- array('adapter' => 'tmx', 'content' => '/language')
- );
- ]]></programlisting>
- </example>
- <para>
- <classname>Zend_Translate</classname> は、指定したディレクトリだけでなく
- そのサブディレクトリすべてから翻訳ソースファイルを探します。
- おかげで、非常に簡単に使用できるようになっています。
- しかし、<classname>Zend_Translate</classname> では
- ソースを含まないファイルは無視します。
- また翻訳データの読み込みに失敗した場合もそのファイルを無視します。
- つまり、翻訳ソースが正しい形式であることと
- 読み込み可能であることを確認しておく必要があります。
- ファイルの形式が間違っていたり読み込みに失敗したりした場合でもエラーは発生しないからです。
- </para>
- <note>
- <para>
- ディレクトリ階層の深さやその中のファイルの数によっては、
- <classname>Zend_Translate</classname> の処理に長い時間がかかることもあります。
- </para>
- </note>
- <para>
- この例では <acronym>TMX</acronym> フォーマットを使用しており、言語の情報をソース内に含んでいます。
- しかし、他のフォーマットの多くは言語の情報をファイル内に持たせられません。
- そんなソースであっても自動検索できます。
- ただし、次に示す条件を満たす必要があります。
- </para>
- <sect3 id="zend.translate.additional.detection.directory">
- <title>ディレクトリ名からの言語の取得</title>
- <para>
- 自動的に言語を検出させる方法のひとつは、
- 言語名を表すディレクトリの配下にソースファイルを配置することです。
- これはもっとも簡単な方法であり、標準的な gettext
- の実装でも用いられています。
- </para>
- <para>
- <classname>Zend_Translate</classname> に '<property>scan</property>' オプションを指定すると、
- ディレクトリ名から言語を検出できます。
- 詳細は次の例を参照ください。
- </para>
- <example id="zend.translate.additional.detection.directory.example">
- <title>ディレクトリ名による言語の検出</title>
- <programlisting language="php"><![CDATA[
- // 以下のようなディレクトリ構造があることを想定しています
- // /language/
- // /language/de/login/login.mo
- // /language/de/error/loginerror.mo
- // /language/en/login/login.mo
- // /language/en/error/loginerror.mo
- $translate = new Zend_Translate(
- array(
- 'adapter' => 'gettext',
- 'content' => '/language',
- 'scan' => Zend_Translate::LOCALE_DIRECTORY
- )
- );
- ]]></programlisting>
- </example>
- <note>
- <para>
- これが動作するのは、
- ソースファイル中に言語情報を持たないフォーマットを使用している場合のみです。
- たとえば <acronym>TMX</acronym> などでこのオプションを使用しても、無視されます。
- また、このオプションを使用した場合は
- ファイル名による言語の自動検出は無視されます。
- </para>
- </note>
- <note>
- <para>
- 同じ構造のもとで複数のサブディレクトリがある場合は注意が必要です。
- たとえば <filename>/language/module/de/en/file.mo</filename>
- のような構造を考えてみましょう。
- このパスには、ロケールと検出されうる文字列が複数含まれています。
- <emphasis>de</emphasis> と <emphasis>en</emphasis> です。
- このような場合は、ファイル名による検出を用いることを推奨します。
- </para>
- </note>
- </sect3>
- <sect3 id="zend.translate.additional.detection.filename">
- <title>ファイル名からの言語の取得</title>
- <para>
- 言語を自動検出するもうひとつの方法は、特別なファイル名を使用することです。
- ファイル名を言語名そのものにするか、あるいはその一部に言語名を含めます。
- この方式を使用する場合は、初期化時に '<property>scan</property>'
- オプションを設定する必要があります。
- ファイル名のつけかたには、以下に示すようにいくつかの方法があります。
- </para>
- <example id="zend.translate.additional.detection.filename.example">
- <title>ファイル名からの言語の取得</title>
- <programlisting language="php"><![CDATA[
- // 以下のようなディレクトリ構造があることを想定しています
- // /language/
- // /language/login/login_en.mo
- // /language/login/login_de.mo
- // /language/error/loginerror_en.mo
- // /language/error/loginerror_de.mo
- $translate = new Zend_Translate(
- array(
- 'adapter' => 'gettext',
- 'content' => '/language',
- 'scan' => Zend_Translate::LOCALE_FILENAME
- )
- );
- ]]></programlisting>
- </example>
- <sect4 id="zend.translate.additional.detection.filename.complete">
- <title>ファイル名全体</title>
- <para>
- 言語名そのものをファイル名にしてしまうのは一番シンプルな方法ですが、
- 同一ディレクトリにソースファイルがひとつだけの場合にしか使用できません。
- </para>
- <programlisting language="txt"><![CDATA[
- /languages/
- /languages/en.mo
- /languages/de.mo
- /languages/es.mo
- ]]></programlisting>
- </sect4>
- <sect4 id="zend.translate.additional.detection.filename.extension">
- <title>ファイルの拡張子</title>
- <para>
- もうひとつのシンプルな方法としては、
- ファイル名の拡張子を用いて言語を検出させるというものがあります。
- しかしこの方法にも問題があり、本来の拡張子が何であったのかがわからなくなります。
- </para>
- <programlisting language="txt"><![CDATA[
- /languages/
- /languages/view.en
- /languages/view.de
- /languages/view.es
- ]]></programlisting>
- </sect4>
- <sect4 id="zend.translate.additional.detection.filename.token">
- <title>ファイル名の一部</title>
- <para>
- <classname>Zend_Translate</classname> は、
- ファイル名の一部に言語名が含まれている場合にもそれを検出できます。
- しかし、この方式を使用する場合は言語名をトークンで分割する必要があります。
- トークンとしてサポートされているのは、小数点 '.' かアンダーライン '_'、
- あるいはハイフン '=' のいずれかです。
- </para>
- <programlisting language="txt"><![CDATA[
- /languages/
- /languages/view_en.mo -> 英語となります
- /languages/view_de.mo -> ドイツ語となります
- /languages/view_it.mo -> イタリア語となります
- ]]></programlisting>
- <para>
- ロケールとして判断できる部分が複数あった場合は、
- 最初に見つかったものを使用します。詳細は次の例でご確認ください。
- </para>
- <programlisting language="txt"><![CDATA[
- /languages/
- /languages/view_en_de.mo -> 英語となります
- /languages/view_en_es.mo -> 英語となり、最初のファイルを上書きします
- /languages/view_it_it.mo -> イタリア語となります
- ]]></programlisting>
- <para>
- 3 種類のトークンのどれを用いても言語を検出できます。
- まず最初に使用するのが小数点 '.'、次に使用するのがアンダーライン
- '_'、そして最後に使用するのがハイフン '-' となります。
- ひとつのファイル名の中に複数のトークンが用いられている場合、
- トークンの優先順位の順に調べて最初に見つかったものを使用します。
- 詳細は次の例でご確認ください。
- </para>
- <programlisting language="txt"><![CDATA[
- /languages/
- /languages/view_en-it.mo -> 英語となります。'_' のほうが '-' より優先されるからです
- /languages/view-en_it.mo -> イタリア語となります。'_' のほうが '-' より優先されるからです
- /languages/view_en.it.mo -> イタリア語となります。'.' のほうが '_' より優先されるからです
- ]]></programlisting>
- </sect4>
- </sect3>
- <sect3 id="zend.translate.additional.detection.ignore">
- <title>特定のファイルやディレクトリを無視</title>
- <!-- TODO : to be translated -->
- <para>
- Sometimes it is useful to exclude files or even directories from being added
- automatically. Therefor you can use the <property>ignore</property> option which
- accepts 3 possible usages.
- </para>
- <sect4 id="zend.translate.additional.detection.ignore.string">
- <title>Ignore a special directory or file</title>
- <para>
- Per default <classname>Zend_Translate</classname> is set to ignore all
- files and directories beginning with
- <emphasis>'<filename>/.</filename>'</emphasis>. This means that
- all <acronym>SVN</acronym> files will be ignored.
- </para>
- <para>
- You can set your own syntax by giving a string for the
- <property>ignore</property> option. The directory separator will be attached
- automatically and has to be omitted.
- </para>
- <programlisting language="php"><![CDATA[
- $options = array('ignore' => 'test');
- $translate = new Zend_Translate(
- array(
- 'adapter' => $adapter,
- 'content' => $content,
- 'locale' => $locale,
- 'ignore' => 'test'
- )
- );
- ]]></programlisting>
- <para>
- The above example will ignore all files and directories beginning with
- <emphasis>test</emphasis>. This means for example
- <filename>/test/en.mo</filename>, <filename>/testing/en.mo</filename> and
- <filename>/dir/test_en.mo</filename>. But it would still add
- <filename>/mytest/en.mo</filename> or <filename>/dir/atest.mo</filename>.
- </para>
- <note>
- <title>Prevent SVN files from being searched</title>
- <para>
- When you set this option, then the default
- <emphasis>'<filename>/.</filename>'</emphasis> will
- be erased. This means that <classname>Zend_Translate</classname> will then
- add all files from the hidden <acronym>SVN</acronym> directories. When you
- are working with <acronym>SVN</acronym>, then you should use the array
- syntax described in the next section.
- </para>
- </note>
- </sect4>
- <sect4 id="zend.translate.additional.detection.ignore.array.files">
- <title>Ignore several directories or files</title>
- <para>
- You can also ignore several files and directories. Instead of a string,
- you can simply give an array with all wished names which will be ignored.
- </para>
- <programlisting language="php"><![CDATA[
- $options = array('ignore' => array('.', 'test', 'old'));
- $translate = new Zend_Translate(
- array(
- 'adapter' => $adapter,
- 'content' => $content,
- 'locale' => $locale,
- 'ignore' => array('.', 'test', 'old')
- )
- );
- ]]></programlisting>
- <para>
- In the above case all 3 syntax will be ignored. But still they have to
- begin with the syntax to be detected and ignored.
- </para>
- </sect4>
- <sect4 id="zend.translate.additional.detection.ignore.array.names">
- <title>Ignore specific names</title>
- <para>
- To ignore files and directories which are not beginning with a defined syntax
- but have a special syntax anywhere within their name you can use a regular
- expression.
- </para>
- <para>
- To use a regular expression the array key of the <property>ignore</property>
- option has to begin with <emphasis>regex</emphasis>.
- </para>
- <programlisting language="php"><![CDATA[
- $options = array(
- 'ignore' => array(
- 'regex' => '/test/u',
- 'regex_2' => '/deleted$/u'
- )
- );
- $translate = new Zend_Translate(
- array(
- 'adapter' => $adapter,
- 'content' => $content,
- 'locale' => $locale,
- 'ignore' => array('regex' => '/test/u', 'regex_2' => '/deleted$/u')
- )
- );
- ]]></programlisting>
- <para>
- In the above case we defined 2 regular expressions. The files and directories
- will always being searched with all given regular expressions. In our example
- this means that any files which contains <emphasis>test</emphasis> anywhere in
- their name will be ignored. Additionally all files and directories which end
- with <emphasis>deleted</emphasis> will not be added as translation.
- </para>
- </sect4>
- </sect3>
- </sect2>
- <sect2 id="zend.translate.additional.rerouting">
- <title>Routing for translations</title>
- <para>
- Not every message ID can be translated. But sometimes is can be useful to output the
- translation from another language instead of returning the message ID itself. You can
- archive this by using the <property>route</property> option.
- </para>
- <para>
- You can add one route for every language. See the following example:
- </para>
- <programlisting language="php"><![CDATA[
- $translate = new Zend_Translate(
- array(
- 'adapter' => $adapter,
- 'content' => $content,
- 'locale' => $locale,
- 'route' => array('fr' => 'en', 'de' => 'fr')
- )
- );
- ]]></programlisting>
- <para>
- The above returns a english translation for all messages which can not be translated to
- french. And it returns a french translation for all messages which can not be translated
- to german. It will even return an english translation for all messages which can wether
- be translated to german nor to french. So you can even define a complete translation
- chain.
- </para>
- <para>
- This feature seems ot be interesting for anyone. But be aware that returning
- translations for wrong or other languages can be problematic when the user does not
- understand this language. So you should always use this feature sparingly.
- </para>
- </sect2>
- <sect2 id="zend.translate.additional.combination">
- <title>Combining multiple translation sources</title>
- <para>
- When you are working with multiple translations you may come into a situation where you
- want to use different source types. For example the resource files which are provided
- by the framework and your own translations which are available by using the gettext
- adapter.
- </para>
- <para>
- By combining multiple translation adapters you can use them within one instance. See
- the following example:
- </para>
- <programlisting language="php"><![CDATA[
- $translate = new Zend_Translate(
- array(
- 'adapter' => 'gettext',
- 'content' => '\path\to\translation.mo',
- 'locale' => 'en'
- )
- );
- $translate_second = new Zend_Translate(
- array(
- 'adapter' => 'array',
- 'content' => '\resources\languages\en\Zend_Validate.php',
- 'locale' => 'en'
- )
- );
- $translate->addTranslation(array('content' => $translate_second));
- ]]></programlisting>
- <para>
- Now the first instance holds all translations from the second instance and you can use
- it within the application even if you used different source types.
- </para>
- <note>
- <title>Memory savings</title>
- <para>
- As you may have noted the second instance is no longer used as soon as it has been
- added to the first instance. To save some memory you may want to unset it.
- </para>
- </note>
- <para>
- When you are scanning for directories you may additionally want to use only one defined
- language. The predefined resources for example are available in more than 10 languages.
- But your application is not available in all of those language. Therefor you can also
- add only one language from the second adapter.
- </para>
- <programlisting language="php"><![CDATA[
- $translate->addTranslation(
- array(
- 'content' => $translate_second,
- 'locale' => 'en'
- )
- );
- ]]></programlisting>
- <para>
- This allows you still to scan through the directories and still add only those languages
- which are relevant for your application.
- </para>
- </sect2>
- <sect2 id="zend.translate.additional.istranslated">
- <title>翻訳の確認</title>
- <para>
- 通常は、テキストが翻訳されているかどうかを気にすることはありません。
- しかし、そのテキストが翻訳されているかどうかを、ソースコードから調べたいこともあるでしょう。
- そんな場合に使用するメソッドが <methodname>isTranslated()</methodname> です。
- </para>
- <para>
- <methodname>isTranslated($messageId, $original = false, $locale = null)</methodname>
- の最初のパラメータには、翻訳されているかどうかを調べたいテキストを指定します。
- また、オプションの三番目のパラメータには、翻訳を調べたいロケールを指定します。
- オプションの二番目のパラメータで指定するのは、
- その言語に完全に一致した翻訳があるのか、あるいはもう少し広い範囲の翻訳を使用するのかという内容です。
- たとえば、あるテキストについて 'en' の翻訳はあるが 'en_US' の翻訳はないといった場合、
- 通常は 'en' の翻訳を取得することになるでしょう。しかし <varname>$original</varname>
- を <constant>TRUE</constant> にしておくと、このような場合は <methodname>isTranslated()</methodname> は <constant>FALSE</constant> を返すようになります。
- </para>
- <example id="zend.translate.additional.istranslated.example">
- <title>テキストの翻訳が存在するかどうかの確認</title>
- <programlisting language="php"><![CDATA[
- $english = array(
- 'message1' => 'Nachricht 1',
- 'message2' => 'Nachricht 2',
- 'message3' => 'Nachricht 3');
- $translate = new Zend_Translate(
- array(
- 'adapter' => 'array',
- 'content' => $english,
- 'locale' => 'de_AT'
- )
- );
- if ($translate->isTranslated('message1')) {
- print "'message1' の翻訳が存在します";
- }
- if (!($translate->isTranslated('message1', true, 'de'))) {
- print "'message1' は 'de' に翻訳することはできません。"
- . "'de_AT' 用の翻訳しかありません";
- }
- if ($translate->isTranslated('message1', false, 'de')) {
- print "'message1' は 'de_AT' に翻訳できます。もし存在しない場合は代替として 'de' を使用できます";
- }
- ]]></programlisting>
- </example>
- </sect2>
- <sect2 id="zend.translate.additional.logging">
- <title>見つからなかった翻訳をログに記録する方法</title>
- <para>
- 大規模なサイトを管理していたり翻訳ファイルを手作業で作ったりしている場合に、
- うまく翻訳ができないメッセージに悩まされることがよくあるでしょう。
- <classname>Zend_Translate</classname> を使っていれば、こんなときにも簡単な解決方法があります。
- </para>
- <para>
- そのためには、次の 2、3 のステップに従う必要があります。
- まず <classname>Zend_Log</classname> のインスタンスを作成します。
- そして、そのインスタンスを <classname>Zend_Translate</classname> にアタッチします。
- 次の例を参照ください。
- </para>
- <example id="zend.translate.additional.logging.example">
- <title>翻訳のログ出力</title>
- <programlisting language="php"><![CDATA[
- $translate = new Zend_Translate(
- array(
- 'adapter' => 'gettext',
- 'content' => $path,
- 'locale' => 'de'
- )
- );
- // log のインスタンスを作成します
- $writer = new Zend_Log_Writer_Stream('/path/to/file.log');
- $log = new Zend_Log($writer);
- // それを translation インスタンスにアタッチします
- $translate->setOptions(
- array(
- 'log' => $log,
- 'logUntranslated' => true
- )
- );
- $translate->translate('unknown string');
- ]]></programlisting>
- </example>
- <para>
- これで、<emphasis>Untranslated message within 'de': unknown string</emphasis>
- のような注意メッセージがログに記録されるようになるでしょう。
- </para>
- <note>
- <para>
- 翻訳が見つからなかったものがすべてログに記録されることに注意しましょう。
- つまり、サポートしていない言語でのリクエストがユーザからあった場合は、
- すべての翻訳がログに記録されるということです。また、
- 翻訳不能なメッセージへのリクエストは毎回ログに記録されます。
- つまり、100 人の人が同じ翻訳をリクエストしたら、
- 100 件のログが記録されるというわけです。
- </para>
- </note>
- <para>
- この機能はメッセージのログ出力だけに使うことはできず、
- 同時にこの「未翻訳」メッセージを空の翻訳ファイルにアタッチします。
- これを記録するには、好みの書式で書き出して先頭の "Untranslated message"
- を除去するログライターを自作しなければなりません。
- </para>
- <para>
- '<property>logMessage</property>' オプションを設定すると、
- 独自のログメッセージを使用できます。
- '<emphasis>%message%</emphasis>' トークンはログメッセージ内で messageId
- に置き換えられ、'<emphasis>%locale%</emphasis>' トークンは
- 要求されたロケールに置き換えられます。
- ログメッセージを自分で定義する方法については次の例を参照ください。
- </para>
- <example id="zend.translate.additional.logging.example2">
- <title>自分で定義したログメッセージ</title>
- <programlisting language="php"><![CDATA[
- $translate = new Zend_Translate(
- array(
- 'adapter' => 'gettext',
- 'content' => $path,
- 'locale' => 'de'
- )
- );
- // log のインスタンスを作成します
- $writer = new Zend_Log_Writer_Stream('/path/to/file.log');
- $log = new Zend_Log($writer);
- // それを translation インスタンスにアタッチします
- $translate->setOptions(
- array(
- 'log' => $log,
- 'logMessage' => "Missing '%message%' within locale '%locale%'",
- 'logUntranslated' => true
- )
- );
- $translate->translate('unknown string');
- ]]></programlisting>
- </example>
- <!-- TODO : to be translated -->
- <para>
- Additionally you are also able to change the priority which is used to write the message
- into the log. Per default the priority <emphasis>Zend_Log::NOTICE</emphasis> is used.
- It equals with <emphasis>5</emphasis>. When you want to change the priority you can use
- any of <classname>Zend_Log</classname>'s priorities. See the following example:
- </para>
- <example id="zend.translate.additional.logging.example3">
- <title>Self defined log priority</title>
- <programlisting language="php"><![CDATA[
- // Create a log instance
- $writer = new Zend_Log_Writer_Stream('/path/to/file.log');
- $log = new Zend_Log($writer);
- $translate = new Zend_Translate(
- array(
- 'adapter' => 'gettext',
- 'content' => $path,
- 'locale' => 'de',
- 'log' => $log,
- 'logMessage' => "Missing '%message%' within locale '%locale%'",
- 'logPriority' => Zend_Log::ALERT,
- 'logUntranslated' => true
- )
- );
- $translate->translate('unknown string');
- ]]></programlisting>
- </example>
- </sect2>
- <sect2 id="zend.translate.additional.sourcedata">
- <title>ソースデータへのアクセス</title>
- <para>
- 時には、翻訳前のソースデータにアクセスしたいこともあるでしょう。
- そんなときのために2つのメソッドを用意しています。
- </para>
- <para>
- <methodname>getMessageIds($locale = null)</methodname> メソッドは、
- すべてのメッセージの ID を配列で返します。
- </para>
- <!-- TODO : to be translated -->
- <para>
- When you want to know the message ID for a given translation then you can use the
- <methodname>getMessageId()</methodname> method.
- </para>
- <para>
- そして、<methodname>getMessages($locale = null)</methodname> メソッドは
- 翻訳前のソースを配列で返します。メッセージ ID がキー、
- それに対応するデータが値となります。
- </para>
- <para>
- どちらのメソッドについても、オプションのパラメータ <varname>$locale</varname>
- を指定できます。これを指定すると、
- 指定した言語についての翻訳情報を返します。
- このパラメータを省略した場合は、実際に設定されている言語を対象とします。
- 注意してほしいのは、普通はすべての言語ですべての翻訳が存在すべきであるということです。
- つまり、通常はこのパラメータを指定する必要はないはずです。
- </para>
- <para>
- さらに、<methodname>getMessages()</methodname> メソッドで翻訳辞書全体を返すこともできます。
- その際には、疑似ロケール 'all' を指定します。
- これを指定すると、追加された各ロケールについてのすべての翻訳データを返します。
- </para>
- <note>
- <para>
- 注意: 追加されているロケールの数や翻訳データの量によっては、
- 返される配列は <emphasis>非常に大きな</emphasis>
- ものとなります。
- </para>
- </note>
- <example id="zend.translate.additional.sourcedata.example">
- <title>アダプタでの言語の処理</title>
- <programlisting language="php"><![CDATA[
- // すべてのメッセージ ID を返します
- $messageIds = $translate->getMessageIds();
- print_r($messageIds);
- // あるいは指定した言語の ID を返します
- $messageIds = $translate->getMessageIds('en_US');
- print_r($messageIds);
- // すべての翻訳データを返します
- $source = $translate->getMessages();
- print_r($source);
- ]]></programlisting>
- </example>
- </sect2>
- </sect1>
- <!--
- vim:se ts=4 sw=4 et:
- -->
|