| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Reviewed: no -->
- <sect1 id="zend.translate.plurals">
- <title>Plural notations for Translation</title>
- <para>
- As of Zend Framework 1.9, <classname>Zend_Translate</classname> is able to provide plural
- support. Professional translation will always have the need to use plurals as they are
- native in almost all languages.
- </para>
- <para>
- So what are plurals? Generally spoken plurals are words which take into account numeric
- meanings. But as you may imagine each language has it's own definition of plurals.
- English, for example, supports one plural. We have a singular definition, for example
- "car", which means implicit one car, and we have the plural definition, "cars" which could
- mean more than one car but also zero cars. Other languages like Russian or Polish have
- more plurals and also the rules for plurals are different.
- </para>
- <para>
- When you want to use plurals with <classname>Zend_Translate</classname> you must not need
- to know how the plurals are defined, only the translator must know as he does the
- translation. The only information you need to have is the language.
- </para>
- <para>
- There are two ways for using plurals... the traditional one, which means that you use a own
- method, and a modern one, which allows you to do plural translations with the same method
- as normal translations.
- </para>
- <sect2 id="zend.translate.plurals.traditional">
- <title>Traditional plural translations</title>
- <para>
- People who worked with gettext in past will be more common with traditional plural
- translations. There is a own <methodname>plural()</methodname> method which can be
- used for plural translations.
- </para>
- <example id="zend.translate.plurals.traditional.example1">
- <title>Example of traditional plural translations</title>
- <para>
- The <methodname>plural()</methodname> method accepts 4 parameters. The first
- parameter is the singular messageId, the second is the plural messageId and the
- third is the number or amount.
- </para>
- <para>
- The number will be used to detect the plural which has to be returned. A optional
- fourth parameter can be used to give a locale which will be used to return the
- translation.
- </para>
- <programlisting language="php"><![CDATA[
- $translate = new Zend_Translate(
- array(
- 'adapter' => 'gettext',
- 'content' => '/path/to/german.mo',
- 'locale' => 'de'
- )
- );
- $translate->plural('Car', 'Cars', $number);
- ]]></programlisting>
- </example>
- </sect2>
- <sect2 id="zend.translate.plurals.modern">
- <title>Modern plural translations</title>
- <para>
- As traditional plural translations are restricted to source code using English plurals
- we added a new way for plural translations. It allows to use the same
- <methodname>translate()</methodname> for standard and for plural translations.
- </para>
- <para>
- To use plural translations with <methodname>translate()</methodname> you need to give
- an array as messageId instead of an string. This array must have the original plural
- messageId's, then the amount and at last an optional locale when your given messageId's
- are not in English notation.
- </para>
- <example id="zend.translate.plurals.modern.example1">
- <title>Example of modern plural translations</title>
- <para>
- When we want to translate the same plural definitions like in the previous our
- example would have to be defined like below.
- </para>
- <programlisting language="php"><![CDATA[
- $translate = new Zend_Translate(
- array(
- 'adapter' => 'gettext',
- 'content' => '/path/to/german.mo',
- 'locale' => 'de'
- )
- );
- $translate->translate(array('Car', 'Cars', $number));
- ]]></programlisting>
- </example>
- <para>
- Using modern plural translations it is also possible to use any language as source
- for messageId's.
- </para>
- <example id="zend.translate.plurals.modern.example2">
- <title>Example of modern plural translations using a different source language</title>
- <para>
- Let's expect we want to use Russian and let's also expect that the given
- messageId's are Russian and not English.
- </para>
- <programlisting language="php"><![CDATA[
- $translate = new Zend_Translate(
- array(
- 'adapter' => 'gettext',
- 'content' => '/path/to/german.mo',
- 'locale' => 'de'
- )
- );
- $translate->translate(
- array(
- 'Car',
- 'Cars first plural',
- 'Cars second plural',
- $number,
- 'ru'
- )
- );
- ]]></programlisting>
- </example>
- <para>
- As you can see you can give more than just the one English plural. But you must give
- the source language in this case so <classname>Zend_Translate</classname> knows which
- plural rules it has to apply.
- </para>
- <para>
- When you omit the plural language then English will be used per default and any
- additional plural definition will be ignored.
- </para>
- </sect2>
- <sect2 id="zend.translate.plurals.source">
- <title>Plural source files</title>
- <para>
- Not all source formats support plural forms. Look into this list for details:
- </para>
- <table id="zend.translate.plurals.source.supportedadapters">
- <title>Plural support</title>
- <tgroup cols="4">
- <thead>
- <row>
- <entry>Adapter</entry>
- <entry>Plurals supported</entry>
- </row>
- </thead>
- <tbody>
- <row>
- <entry>Array</entry>
- <entry><emphasis>yes</emphasis></entry>
- </row>
- <row>
- <entry>Csv</entry>
- <entry><emphasis>yes</emphasis></entry>
- </row>
- <row>
- <entry>Gettext</entry>
- <entry><emphasis>yes</emphasis></entry>
- </row>
- <row>
- <entry>Ini</entry>
- <entry><emphasis>no</emphasis></entry>
- </row>
- <row>
- <entry>Qt</entry>
- <entry><emphasis>no</emphasis></entry>
- </row>
- <row>
- <entry>Tbx</entry>
- <entry><emphasis>no</emphasis></entry>
- </row>
- <row>
- <entry>Tmx</entry>
- <entry><emphasis>no</emphasis></entry>
- </row>
- <row>
- <entry>Xliff</entry>
- <entry><emphasis>no</emphasis></entry>
- </row>
- <row>
- <entry>XmlTm</entry>
- <entry><emphasis>no</emphasis></entry>
- </row>
- </tbody>
- </tgroup>
- </table>
- <para>
- Below you can find examples of plural defined source files.
- </para>
- <sect3 id="zend.translate.plurals.source.array">
- <title>Array source with plural definitions</title>
- <para>
- An array with plural definitions has to look like the following example.
- </para>
- <programlisting language="php"><![CDATA[
- array(
- 'plural_0' => array(
- 'plural_0 (ru)',
- 'plural_1 (ru)',
- 'plural_2 (ru)',
- 'plural_3 (ru)'
- ),
- 'plural_1' => ''
- );
- ]]></programlisting>
- <para>
- In the above example 'plural_0' and 'plural_1' are the
- plural definitions from the source code. And the array at 'plural_0'
- has all translated plural forms available. Take a look at the following example
- with real content and translation from English source to German.
- </para>
- <programlisting language="php"><![CDATA[
- array(
- 'Car' => array(
- 'Auto',
- 'Autos'
- ),
- 'Cars' => ''
- );
- ]]></programlisting>
- <para>
- When your translated language supports more plural forms then simply add them to
- the array below the first plural form. When your source language supports more
- plural forms, than simply add a new empty translation.
- </para>
- </sect3>
- <sect3 id="zend.translate.plurals.source.csv">
- <title>Csv source with plural definitions</title>
- <para>
- A csv file with plural definitions has to look like the following example.
- </para>
- <programlisting language="php"><![CDATA[
- "plural_0";"plural_0 (ru)";"plural_1 (ru)";"plural_2 (ru)";"plural_3 (ru)"
- "plural_1";
- ]]></programlisting>
- <para>
- All translated plural forms have to be added after the first plural of the source
- language. And all further plural forms of the source language have to be added
- below but without translation. Note that you must add a delimiter to empty
- source plurals.
- </para>
- </sect3>
- <sect3 id="zend.translate.plurals.source.gettext">
- <title>Gettext source with plural definitions</title>
- <para>
- Gettext sources support plural forms out of the box. There is no need for adoption
- as the <filename>*.mo</filename> file will contain all necessary data.
- </para>
- <note>
- <para>
- Note that gettext does not support the usage of source languages which are not
- using english plural forms. When you plan to use a source language which
- supports other plural forms like russian for example, then you can not use
- gettext sources.
- </para>
- </note>
- </sect3>
- </sect2>
- <sect2 id="zend.translate.plurals.customrules">
- <title>Custom plural rules</title>
- <para>
- In rare cases it could be useful to be able to define own plural rules. See Chinese for
- example. This language defines two plural rules. Per default it does not use plurals.
- But in rare cases it uses a rule like <emphasis>(number == 1) ? 0 : 1</emphasis>.
- </para>
- <para>
- Also when you want to use a language which has no known plural rules, and would want to
- define your own rules.
- </para>
- <para>
- This can be done by using <methodname>Zend_Translate_Plural::setRule()</methodname>.
- The method expects two parameters which must be given. A rule, which is simply a
- callback to a self defined method. And a locale for which the rule will be used.
- </para>
- <para>
- Your rule could look like this:
- </para>
- <programlisting language="php"><![CDATA[
- public function MyRule($number) {
- return ($number == 10) ? 0 : 1;
- }
- ]]></programlisting>
- <para>
- As you see, your rule must accept one parameter. It is the number which you will use to
- return which plural the translation has to use. In our example we defined that when we
- get a '10' the plural definition 0 has to be used, in all other cases we're using 1.
- </para>
- <para>
- Your rules can be as simple or as complicated as you want. You must only return an
- integer value. The plural definition 0 stands for singular translation, and 1 stands for
- the first plural rule.
- </para>
- <para>
- To activate your rule, and to link it to the wished locale, you have to call it like
- this:
- </para>
- <programlisting language="php"><![CDATA[
- Zend_Translate_Plural::setPlural('MyPlural', 'zh');
- ]]></programlisting>
- <para>
- Now we linked our plural definition to the Chinese language.
- </para>
- <para>
- You can define one plural rule for every language. But you should be aware that you set
- the plural rules before you are doing translations.
- </para>
- <note>
- <title>Define custom plurals only when needed</title>
- <para>
- <classname>Zend_Translate</classname> defines plurals for most known languages.
- You should not define own plurals when you are not in need. The default rules work
- most of time.
- </para>
- </note>
- </sect2>
- </sect1>
- <!--
- vim:se ts=4 sw=4 et:
- -->
|