Zend_Locale-Introduction.xml 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.locale.introduction">
  4. <title>Introduction</title>
  5. <para>
  6. <classname>Zend_Locale</classname> is the Frameworks answer to the question, "How can the same application be used around
  7. the whole world?" Most people will say, "That's easy. Let's translate all our output to several languages."
  8. However, using simple translation tables to map phrases from one language to another is not sufficient.
  9. Different regions will have different conventions for first names, surnames, salutory titles, formatting of
  10. numbers, dates, times, currencies, etc.
  11. </para>
  12. <para>
  13. We need
  14. <ulink url="http://en.wikipedia.org/wiki/L10n">Localization</ulink>
  15. and complementary
  16. <ulink url="http://en.wikipedia.org/wiki/L10n">Internationalization</ulink>
  17. . Both are often abbreviated to <code>L10n</code> and <code>I18n</code>. Internationalization refers more to
  18. support for use of systems, regardless of special needs unique to groups of users related by language, region,
  19. number format conventions, financial conventions, time and date conventions, etc. Localization involves adding
  20. explicit support to systems for special needs of these unique groups, such as language translation, and support
  21. for local customs or conventions for communicating plurals, dates, times, currencies, names, symbols, sorting
  22. and ordering, etc. <code>L10n</code> and <code>I18n</code> compliment each other. Zend Framework provides
  23. support for these through a combination of components, including <classname>Zend_Locale</classname>, <classname>Zend_Date</classname>, <classname>Zend_Measure</classname>,
  24. <classname>Zend_Translate</classname>, <classname>Zend_Currency</classname>, and <classname>Zend_TimeSync</classname>.
  25. </para>
  26. <tip>
  27. <title>Zend_Locale and setLocale()</title>
  28. <para>
  29. <ulink url="http://php.net/setlocale">PHP's documentation</ulink> states that <methodname>setlocale()</methodname>
  30. is not threadsave because it is maintained per process and not per thread. This means that, in
  31. multithreaded environments, you can have the problem that the locale changes while the script
  32. never has changed the locale itself. This can lead to unexpected behaviour when you use
  33. <methodname>setlocale()</methodname> in your scripts.
  34. </para>
  35. <para>
  36. When you are using <classname>Zend_Locale</classname> you will not have this limitations, because
  37. <classname>Zend_Locale</classname> is not related to or coupled with <acronym>PHP</acronym>'s <methodname>setlocale()</methodname>.
  38. </para>
  39. </tip>
  40. <sect2 id="zend.locale.whatislocalization">
  41. <title>What is Localization</title>
  42. <para>
  43. Localization means that an application (or homepage) can be used from different users which speak different
  44. languages. But as you already have expected Localization means more than only translating strings. It
  45. includes
  46. </para>
  47. <itemizedlist mark='opencircle'>
  48. <listitem>
  49. <para>
  50. <classname>Zend_Locale</classname> - Backend support of locales available for
  51. localization support within other Zend Framework components.
  52. </para>
  53. </listitem>
  54. <listitem>
  55. <para>
  56. <classname>Zend_Translate</classname> - Translating of strings.
  57. </para>
  58. </listitem>
  59. <listitem>
  60. <para>
  61. <classname>Zend_Date</classname> - Localization of dates, times.
  62. </para>
  63. </listitem>
  64. <listitem>
  65. <para>
  66. <classname>Zend_Calendar</classname> - Localization of calendars (support for non-Gregorian calendar systems)
  67. </para>
  68. </listitem>
  69. <listitem>
  70. <para>
  71. <classname>Zend_Currency</classname> - Localization of currencies.
  72. </para>
  73. </listitem>
  74. <listitem>
  75. <para>
  76. <classname>Zend_Locale_Format</classname> - Parsing and generating localized numbers.
  77. </para>
  78. </listitem>
  79. <listitem>
  80. <para>
  81. <classname>Zend_Locale_Data</classname> - Retrieve localized standard strings as country names, language names
  82. and
  83. <ulink url="http://unicode.org/cldr/">more from the <acronym>CLDR</acronym></ulink>
  84. .
  85. </para>
  86. </listitem>
  87. <listitem>
  88. <para>
  89. <code>TODO</code> - Localization of collations
  90. </para>
  91. </listitem>
  92. </itemizedlist>
  93. </sect2>
  94. <sect2 id="zend.locale.whatis">
  95. <title>What is a Locale?</title>
  96. <para>
  97. Each computer user makes use of Locales, even when they don't know it. Applications lacking localization
  98. support, normally have implicit support for one particular locale (the locale of the author). When a class
  99. or function makes use of localization, we say it is <code>locale-aware</code>. How does the code know which
  100. localization the user is expecting?
  101. </para>
  102. <para>
  103. A locale string or object identifying a supported locale gives <classname>Zend_Locale</classname> and its subclasses
  104. access to information about the language and region expected by the user. Correct formatting, normalization,
  105. and conversions are made based on this information.
  106. </para>
  107. </sect2>
  108. <sect2 id="zend.locale.representation">
  109. <title>How are Locales Represented?</title>
  110. <para>
  111. Locale identifiers consist of information about the user's language and preferred/primary geographic region
  112. (e.g. state or province of home or workplace). The locale identifier strings used in Zend Framework are
  113. internationally defined standard abbreviations of language and region, written as
  114. <code>language_REGION</code>. Both the language and region parts are abbreviated to alphabetic, <acronym>ASCII</acronym>
  115. characters.
  116. </para>
  117. <note>
  118. <para>
  119. Be aware that there exist not only locales with 2 characters as most people think. Also there
  120. are languages and regions which are not only abbreviated with 2 characters. Therefor you should
  121. NOT strip the region and language yourself, but use <classname>Zend_Locale</classname> when you want to strip language
  122. or region from a locale string. Otherwise you could have unexpected behaviour within your code
  123. when you do this yourself.
  124. </para>
  125. </note>
  126. <para>
  127. A user from USA would expect the language <code>English</code> and the region <constant>USA</constant>, yielding the
  128. locale identifier "en_US". A user in Germany would expect the language <code>German</code> and the region
  129. <code>Germany</code>, yielding the locale identifier "de_DE". See the
  130. <ulink url="http://unicode.org/cldr/data/diff/supplemental/languages_and_territories.html">list of pre-defined locale and region combinations</ulink>, if you need to select a specific locale within Zend Framework.
  131. </para>
  132. <example id="zend.locale.representation.example-1">
  133. <title>Choosing a specific locale</title>
  134. <programlisting language="php"><![CDATA[
  135. $locale = new Zend_Locale('de_DE'); // German language _ Germany
  136. ]]></programlisting>
  137. </example>
  138. <para>
  139. A German user in America might expect the language <code>German</code> and the region <constant>USA</constant>, but
  140. these non-standard mixes are not supported directly as recognized "locales". Instead, if an invalid
  141. combination is used, then it will automatically be truncated by dropping the region code. For example,
  142. "de_IS" would be truncated to "de", and "xh_RU" would be truncated to "xh", because neither of these
  143. combinations are valid. Additionally, if the base language code is not supported (e.g. "zz_US") or does not
  144. exist, then a default "root" locale will be used. The "root" locale has default definitions for
  145. internationally recognized representations of dates, times, numbers, currencies, etc. The truncation process
  146. depends on the requested information, since some combinations of language and region might be valid for one
  147. type of data (e.g. dates), but not for another (e.g. currency format).
  148. </para>
  149. <para>
  150. Beware of historical changes, as Zend Framework components do not know about or attempt
  151. to track the numerous timezone changes made over many years by many regions. For
  152. example, <ulink url="http://www.statoids.com/tus.html">we can see a historical
  153. list</ulink> showing dozens of changes made by governments to when and if a
  154. particular region observes Daylight Savings Time, and even which timezone a particular
  155. geographic area belongs. Thus, when performing date math, the math performed by Zend
  156. Framework components will not adjust for these changes, but instead will give the
  157. correct time for the timezone using current, modern rules for <acronym>DST</acronym> and timezone
  158. assignment for geographic regions.
  159. </para>
  160. </sect2>
  161. <sect2 id="zend.locale.selection">
  162. <title>Selecting the Right Locale</title>
  163. <para>
  164. For most situations, <code>new Zend_Locale()</code> will automatically select the correct locale, with
  165. preference given to information provided by the user's web browser. However, if <code>new
  166. Zend_Locale(Zend_Locale::ENVIRONMENT)</code> is used, then preference will be given to using the host
  167. server's environment configuration, as described below.
  168. </para>
  169. <example id="zend.locale.selection.example-1">
  170. <title>Automatically selecting a locale</title>
  171. <programlisting language="php"><![CDATA[
  172. $locale = new Zend_Locale();
  173. // default behavior, same as above
  174. $locale1 = new Zend_Locale(Zend_Locale::BROWSER);
  175. // prefer settings on host server
  176. $locale2 = new Zend_Locale(Zend_Locale::ENVIRONMENT);
  177. // perfer framework app default settings
  178. $locale3 = new Zend_Locale(Zend_Locale::FRAMEWORK);
  179. ]]></programlisting>
  180. </example>
  181. <para>
  182. The search algorithm used by <classname>Zend_Locale</classname> for automatic selection of a locale uses three sources
  183. of information:
  184. <orderedlist>
  185. <listitem>
  186. <para>
  187. const <constant>Zend_Locale::BROWSER</constant> - The user's Web browser provides information with each
  188. request, which is published by <acronym>PHP</acronym> in the global variable <constant>HTTP_ACCEPT_LANGUAGE</constant>. If
  189. no matching locale can be found, then preference is given to <constant>ENVIRONMENT</constant> and lastly
  190. <constant>FRAMEWORK</constant>.
  191. </para>
  192. </listitem>
  193. <listitem>
  194. <para>
  195. const <constant>Zend_Locale::ENVIRONMENT</constant> - <acronym>PHP</acronym> publishes the host server's locale via the <acronym>PHP</acronym>
  196. internal function <methodname>setlocale()</methodname>. If no matching locale can be found, then preference
  197. is given to <constant>FRAMEWORK</constant> and lastly <constant>BROWSER</constant>.
  198. </para>
  199. </listitem>
  200. <listitem>
  201. <para>
  202. const <constant>Zend_Locale::FRAMEWORK</constant> - When Zend Framework has a standardized way of
  203. specifying component defaults (planned, but not yet available), then using this constant during
  204. instantiation will give preference to choosing a locale based on these defaults. If no matching
  205. locale can be found, then preference is given to <constant>ENVIRONMENT</constant> and lastly
  206. <constant>BROWSER</constant>.
  207. </para>
  208. </listitem>
  209. </orderedlist>
  210. </para>
  211. </sect2>
  212. <sect2 id="zend.locale.selection.automatic">
  213. <title>Usage of automatic Locales</title>
  214. <para>
  215. <classname>Zend_Locale</classname> provides three additionally locales. These locales do not belong to
  216. any language or region. They are "automatic" locales which means that they have the same effect
  217. as the method <methodname>getDefault()</methodname> but without the negative effects like creating an instance.
  218. These "automatic" locales can be used anywhere, where also a standard locale and also the
  219. definition of a locale, its string representation, can be used. This offers simplicity for
  220. situations like working with locales which are provided by a browser.
  221. </para>
  222. <para>
  223. There are three locales which have a slightly different behaviour:
  224. <orderedlist>
  225. <listitem>
  226. <para>
  227. <code>'browser'</code> - <classname>Zend_Locale</classname> should work with the information
  228. which is provided by the user's Web browser. It is published by <acronym>PHP</acronym> in the global
  229. variable <constant>HTTP_ACCEPT_LANGUAGE</constant>.
  230. </para>
  231. <para>
  232. If a user provides more than one locale within his browser, <classname>Zend_Locale</classname>
  233. will use the first found locale. If the user does not provide a locale or the script is
  234. being called from the command line the automatic locale <code>'environment'</code>
  235. will automatically be used and returned.
  236. </para>
  237. </listitem>
  238. <listitem>
  239. <para>
  240. <code>'environment'</code> - <classname>Zend_Locale</classname> should work with the information
  241. which is provided by the host server. It is published by <acronym>PHP</acronym> via the internal function
  242. <methodname>setlocale()</methodname>.
  243. </para>
  244. <para>
  245. If a environment provides more than one locale, <classname>Zend_Locale</classname> will use the
  246. first found locale. If the host does not provide a locale the automatic locale
  247. <code>'browser'</code> will automatically be used and returned.
  248. </para>
  249. </listitem>
  250. <listitem>
  251. <para>
  252. <code>'auto'</code> - <classname>Zend_Locale</classname> should automatically detect any locale
  253. which can be worked with. It will first search for a users locale and then, if not
  254. successful, search for the host locale.
  255. </para>
  256. <para>
  257. If no locale can be detected, it will throw an exception and tell you that the
  258. automatic detection has been failed.
  259. </para>
  260. </listitem>
  261. </orderedlist>
  262. </para>
  263. <example id="zend.locale.selection.automatic.example-1">
  264. <title>Using automatic locales</title>
  265. <programlisting language="php"><![CDATA[
  266. // without automatic detection
  267. //$locale = new Zend_Locale(Zend_Locale::BROWSER);
  268. //$date = new Zend_Date($locale);
  269. // with automatic detection
  270. $date = new Zend_Date('auto');
  271. ]]></programlisting>
  272. </example>
  273. </sect2>
  274. <sect2 id="zend.locale.defaultlocale">
  275. <title>Using a default Locale</title>
  276. <para>
  277. In some environments it is not possible to detect a locale automatically. You can expect this
  278. behaviour when you get an request from command line or the requesting browser has no language tag
  279. set and additionally your server has the default locale 'C' set or another proprietary locale.
  280. </para>
  281. <para>
  282. In such cases <classname>Zend_Locale</classname> will normally throw an exception with a message that
  283. the automatic detection of any locale was not successful. You have two options to handle such
  284. a situation. Either through setting a new locale per hand, or defining a default locale.
  285. </para>
  286. <example id="zend.locale.defaultlocale.example-1">
  287. <title>Handling locale exceptions</title>
  288. <programlisting language="php"><![CDATA[
  289. // within the bootstrap file
  290. try {
  291. $locale = new Zend_Locale('auto');
  292. } catch (Zend_Locale_Exception $e) {
  293. $locale = new Zend_Locale('de');
  294. }
  295. // within your model/controller
  296. $date = new Zend_Date($locale);
  297. ]]></programlisting>
  298. </example>
  299. <para>
  300. But this has one big negative effect. You will have to set your locale object within
  301. every class using <classname>Zend_Locale</classname>. This could become very unhandy if you are using
  302. multiple classes.
  303. </para>
  304. <para>
  305. Since Zend Framework Release 1.5 there is a much better way to handle this. You can set a
  306. default locale which the static <methodname>setDefault()</methodname> method. Of course, every unknown
  307. or not full qualified locale will also throw an exception. <methodname>setDefault()</methodname> should
  308. be the first call before you initiate any class using <classname>Zend_Locale</classname>. See the
  309. following example for details:
  310. </para>
  311. <example id="zend.locale.defaultlocale.example-2">
  312. <title>Setting a default locale</title>
  313. <programlisting language="php"><![CDATA[
  314. // within the bootstrap file
  315. Zend_Locale::setDefault('de');
  316. // within your model/controller
  317. $date = new Zend_Date();
  318. ]]></programlisting>
  319. </example>
  320. <para>
  321. In the case that no locale can be detected, automatically the locale
  322. <emphasis>de</emphasis> will be used. Otherwise, the detected locale will be used.
  323. </para>
  324. </sect2>
  325. <sect2 id="zend.locale.interoperate">
  326. <title>ZF Locale-Aware Classes</title>
  327. <para>
  328. In the Zend Framework, locale-aware classes rely on <classname>Zend_Locale</classname> to
  329. automatically select a locale, as explained above. For example, in a Zend Framework web
  330. application, constructing a date using <classname>Zend_Date</classname> without
  331. specifying a locale results in an object with a locale based on information provided by
  332. the current user's web browser.
  333. </para>
  334. <example id="zend.locale.interoperate.example-1">
  335. <title>Dates default to correct locale of web users</title>
  336. <programlisting language="php"><![CDATA[
  337. $date = new Zend_Date('2006',Zend_Date::YEAR);
  338. ]]></programlisting>
  339. </example>
  340. <para>
  341. To override this default behavior, and force locale-aware Zend Framework components to
  342. use specific locales, regardless of the origin of your website visitors, explicitly
  343. specify a locale as the third argument to the constructor.
  344. </para>
  345. <example id="zend.locale.interoperate.example-2">
  346. <title>Overriding default locale selection</title>
  347. <programlisting language="php"><![CDATA[
  348. $usLocale = new Zend_Locale('en_US');
  349. $date = new Zend_Date('2006', Zend_Date::YEAR, $usLocale);
  350. $temp = new Zend_Measure_Temperature('100,10',
  351. Zend_Measure::TEMPERATURE,
  352. $usLocale);
  353. ]]></programlisting>
  354. </example>
  355. <para>
  356. If you know many objects should all use the same default locale, explicitly specify the default locale to
  357. avoid the overhead of each object determining the default locale.
  358. </para>
  359. <example id="zend.locale.interoperate.example-3">
  360. <title>Performance optimization when using a default locale</title>
  361. <programlisting language="php"><![CDATA[
  362. $locale = new Zend_Locale();
  363. $date = new Zend_Date('2006', Zend_Date::YEAR, $locale);
  364. $temp = new Zend_Measure_Temperature('100,10',
  365. Zend_Measure::TEMPERATURE,
  366. $locale);
  367. ]]></programlisting>
  368. </example>
  369. </sect2>
  370. <sect2 id="zend.locale.frameworkwidelocale">
  371. <title>Application wide locale</title>
  372. <para>
  373. Zend Framework allows the usage of an application wide locale. You simply set an instance of
  374. <classname>Zend_Locale</classname> to the registry with the key 'Zend_Locale'. Then this instance will
  375. be used within all locale aware classes of Zend Framework. This way you set one locale within
  376. your registry and then you can forget about setting it again. It will automatically be used
  377. in all other classes. See the below example for the right usage:
  378. </para>
  379. <example id="zend.locale.frameworkwidelocale.example">
  380. <title>Usage of an application wide locale</title>
  381. <programlisting language="php"><![CDATA[
  382. // within your bootstrap
  383. $locale = new Zend_Locale('de_AT');
  384. Zend_Registry::set('Zend_Locale', $locale);
  385. // within your model or controller
  386. $date = new Zend_Date();
  387. // print $date->getLocale();
  388. echo $date->getDate();
  389. ]]></programlisting>
  390. </example>
  391. </sect2>
  392. <sect2 id="zend.locale.formatoptions">
  393. <title>Zend_Locale_Format::setOptions(array $options)</title>
  394. <para>
  395. The 'precision' option of a value is used to truncate or stretch extra digits. A value of '-1' disables
  396. modification of the number of digits in the fractional part of the value. The 'locale' option helps when
  397. parsing numbers and dates using separators and month names. The date format 'format_type' option selects between
  398. <acronym>CLDR</acronym>/ISO date format specifier tokens and <acronym>PHP</acronym>'s date() tokens. The 'fix_date' option enables or disables
  399. heuristics that attempt to correct invalid dates. The 'number_format' option specifies a default number
  400. format for use with <methodname>toNumber()</methodname> (see
  401. <xref
  402. linkend= "zend.locale.number.localize"/>
  403. ).
  404. </para>
  405. <para>
  406. The 'date_format' option can be used to specify a default date format string, but beware of using getDate(),
  407. checkdateFormat() and getTime() after using setOptions() with a 'date_format'. To use these four methods
  408. with the default date format for a locale, use array('date_format' => null, 'locale' => $locale) for their
  409. options.
  410. </para>
  411. <example id="zend.locale.formatoptions.example-1">
  412. <title>Dates default to correct locale of web users</title>
  413. <programlisting language="php"><![CDATA[
  414. Zend_Locale_Format::setOptions(array('locale' => 'en_US',
  415. 'fix_date' => true,
  416. 'format_type' => 'php'));
  417. ]]></programlisting>
  418. </example>
  419. <para>
  420. For working with the standard definitions of a locale the option <constant>Zend_Locale_Format::STANDARD</constant> can be used.
  421. Setting the option <constant>Zend_Locale_Format::STANDARD</constant> for <code>date_format</code> uses the standard definitions from the actual
  422. set locale. Setting it for number_format uses the standard number format for this locale.
  423. And setting it for locale uses the standard locale for this environment or browser.
  424. </para>
  425. <example id="zend.locale.formatoptions.example-2">
  426. <title>Using STANDARD definitions for setOptions()</title>
  427. <programlisting language="php"><![CDATA[
  428. Zend_Locale_Format::setOptions(array('locale' => 'en_US',
  429. 'date_format' => 'dd.MMMM.YYYY'));
  430. // overriding the global set date format
  431. $date = Zend_Locale_Format::getDate('2007-04-20',
  432. array('date_format' =>
  433. Zend_Locale_Format::STANDARD);
  434. // global setting of the standard locale
  435. Zend_Locale_Format::setOptions(array('locale' => Zend_Locale_Format::STANDARD,
  436. 'date_format' => 'dd.MMMM.YYYY'));
  437. ]]></programlisting>
  438. </example>
  439. </sect2>
  440. <sect2 id="zend.locale.cache">
  441. <title>Speed up Zend_Locale and its subclasses</title>
  442. <para>
  443. <classname>Zend_Locale</classname> and its subclasses can be speed up by the usage of
  444. <classname>Zend_Cache</classname>. Use the static method
  445. <methodname>Zend_Locale::setCache($cache)</methodname> if you are using
  446. <classname>Zend_Locale</classname>. <classname>Zend_Locale_Format</classname> can be
  447. speed up the using the option <code>cache</code> within
  448. <classname>Zend_Locale_Format::setOptions(array('cache' => $adapter));</classname>.
  449. If you are using both classes you should only set the cache for
  450. <classname>Zend_Locale</classname>, otherwise the last set cache will overwrite the
  451. previous set cache. For convenience there are also the static methods
  452. <methodname>getCache()</methodname>, <methodname>hasCache()</methodname>,
  453. <methodname>clearCache()</methodname> and <methodname>removeCache()</methodname>.
  454. </para>
  455. <para>
  456. When no cache is set, then <classname>Zend_Locale</classname> will automatically set a
  457. cache itself. Sometimes it is wished to prevent that a cache is set, even if this
  458. degrades performance. In this case the static
  459. <methodname>disableCache(true)</methodname> method should be used. It does not only
  460. disable the actual set cache, without erasing it, but also prevents that a cache is
  461. automatically generated when no cache is set.
  462. </para>
  463. </sect2>
  464. </sect1>
  465. <!--
  466. vim:se ts=4 sw=4 et:
  467. -->