Zend_Locale-Introduction.xml 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 <code>setlocale()</code>
  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. <code>setlocale()</code> 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 PHP's <code>setlocale()</code>.
  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 localization support within
  51. other ZF 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 CLDR</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, ASCII
  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 <code>USA</code>, 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 <code>USA</code>, 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 ZF components do not know about or attempt to track the numerous timezone
  151. changes made over many years by many regions. For example,
  152. <ulink url="http://www.statoids.com/tus.html">we can see a historical list</ulink>
  153. showing dozens of changes made by governments to when and if a particular region observes Daylight Savings
  154. Time, and even which timezone a particular geographic area belongs. Thus, when performing date math, the
  155. math performed by ZF components will not adjust for these changes, but instead will give the correct time
  156. for the timezone using current, modern rules for DST and timezone assignment for geographic regions.
  157. </para>
  158. </sect2>
  159. <sect2 id="zend.locale.selection">
  160. <title>Selecting the Right Locale</title>
  161. <para>
  162. For most situations, <code>new Zend_Locale()</code> will automatically select the correct locale, with
  163. preference given to information provided by the user's web browser. However, if <code>new
  164. Zend_Locale(Zend_Locale::ENVIRONMENT)</code> is used, then preference will be given to using the host
  165. server's environment configuration, as described below.
  166. </para>
  167. <example id="zend.locale.selection.example-1">
  168. <title>Automatically selecting a locale</title>
  169. <programlisting language="php"><![CDATA[
  170. $locale = new Zend_Locale();
  171. // default behavior, same as above
  172. $locale1 = new Zend_Locale(Zend_Locale::BROWSER);
  173. // prefer settings on host server
  174. $locale2 = new Zend_Locale(Zend_Locale::ENVIRONMENT);
  175. // perfer framework app default settings
  176. $locale3 = new Zend_Locale(Zend_Locale::FRAMEWORK);
  177. ]]></programlisting>
  178. </example>
  179. <para>
  180. The search algorithm used by <classname>Zend_Locale</classname> for automatic selection of a locale uses three sources
  181. of information:
  182. <orderedlist>
  183. <listitem>
  184. <para>
  185. const <classname>Zend_Locale::BROWSER</classname> - The user's Web browser provides information with each
  186. request, which is published by PHP in the global variable <code>HTTP_ACCEPT_LANGUAGE</code>. If
  187. no matching locale can be found, then preference is given to <code>ENVIRONMENT</code> and lastly
  188. <code>FRAMEWORK</code>.
  189. </para>
  190. </listitem>
  191. <listitem>
  192. <para>
  193. const <classname>Zend_Locale::ENVIRONMENT</classname> - PHP publishes the host server's locale via the PHP
  194. internal function <code>setlocale()</code>. If no matching locale can be found, then preference
  195. is given to <code>FRAMEWORK</code> and lastly <code>BROWSER</code>.
  196. </para>
  197. </listitem>
  198. <listitem>
  199. <para>
  200. const <classname>Zend_Locale::FRAMEWORK</classname> - When Zend Framework has a standardized way of
  201. specifying component defaults (planned, but not yet available), then using this constant during
  202. instantiation will give preference to choosing a locale based on these defaults. If no matching
  203. locale can be found, then preference is given to <code>ENVIRONMENT</code> and lastly
  204. <code>BROWSER</code>.
  205. </para>
  206. </listitem>
  207. </orderedlist>
  208. </para>
  209. </sect2>
  210. <sect2 id="zend.locale.selection.automatic">
  211. <title>Usage of automatic Locales</title>
  212. <para>
  213. <classname>Zend_Locale</classname> provides three additionally locales. These locales do not belong to
  214. any language or region. They are "automatic" locales which means that they have the same effect
  215. as the method <code>getDefault()</code> but without the negative effects like creating an instance.
  216. These "automatic" locales can be used anywhere, where also a standard locale and also the
  217. definition of a locale, its string representation, can be used. This offers simplicity for
  218. situations like working with locales which are provided by a browser.
  219. </para>
  220. <para>
  221. There are three locales which have a slightly different behaviour:
  222. <orderedlist>
  223. <listitem>
  224. <para>
  225. <code>'browser'</code> - <classname>Zend_Locale</classname> should work with the information
  226. which is provided by the user's Web browser. It is published by PHP in the global
  227. variable <code>HTTP_ACCEPT_LANGUAGE</code>.
  228. </para>
  229. <para>
  230. If a user provides more than one locale within his browser, <classname>Zend_Locale</classname>
  231. will use the first found locale. If the user does not provide a locale or the script is
  232. being called from the command line the automatic locale <code>'environment'</code>
  233. will automatically be used and returned.
  234. </para>
  235. </listitem>
  236. <listitem>
  237. <para>
  238. <code>'environment'</code> - <classname>Zend_Locale</classname> should work with the information
  239. which is provided by the host server. It is published by PHP via the internal function
  240. <code>setlocale()</code>.
  241. </para>
  242. <para>
  243. If a environment provides more than one locale, <classname>Zend_Locale</classname> will use the
  244. first found locale. If the host does not provide a locale the automatic locale
  245. <code>'browser'</code> will automatically be used and returned.
  246. </para>
  247. </listitem>
  248. <listitem>
  249. <para>
  250. <code>'auto'</code> - <classname>Zend_Locale</classname> should automatically detect any locale
  251. which can be worked with. It will first search for a users locale and then, if not
  252. successful, search for the host locale.
  253. </para>
  254. <para>
  255. If no locale can be detected, it will throw an exception and tell you that the
  256. automatic detection has been failed.
  257. </para>
  258. </listitem>
  259. </orderedlist>
  260. </para>
  261. <example id="zend.locale.selection.automatic.example-1">
  262. <title>Using automatic locales</title>
  263. <programlisting language="php"><![CDATA[
  264. // without automatic detection
  265. //$locale = new Zend_Locale(Zend_Locale::BROWSER);
  266. //$date = new Zend_Date($locale);
  267. // with automatic detection
  268. $date = new Zend_Date('auto');
  269. ]]></programlisting>
  270. </example>
  271. </sect2>
  272. <sect2 id="zend.locale.defaultlocale">
  273. <title>Using a default Locale</title>
  274. <para>
  275. In some environments it is not possible to detect a locale automatically. You can expect this
  276. behaviour when you get an request from command line or the requesting browser has no language tag
  277. set and additionally your server has the default locale 'C' set or another proprietary locale.
  278. </para>
  279. <para>
  280. In such cases <classname>Zend_Locale</classname> will normally throw an exception with a message that
  281. the automatic detection of any locale was not successful. You have two options to handle such
  282. a situation. Either through setting a new locale per hand, or defining a default locale.
  283. </para>
  284. <example id="zend.locale.defaultlocale.example-1">
  285. <title>Handling locale exceptions</title>
  286. <programlisting language="php"><![CDATA[
  287. // within the bootstrap file
  288. try {
  289. $locale = new Zend_Locale('auto');
  290. } catch (Zend_Locale_Exception $e) {
  291. $locale = new Zend_Locale('de');
  292. }
  293. // within your model/controller
  294. $date = new Zend_Date($locale);
  295. ]]></programlisting>
  296. </example>
  297. <para>
  298. But this has one big negative effect. You will have to set your locale object within
  299. every class using <classname>Zend_Locale</classname>. This could become very unhandy if you are using
  300. multiple classes.
  301. </para>
  302. <para>
  303. Since Zend Framework Release 1.5 there is a much better way to handle this. You can set a
  304. default locale which the static <code>setDefault()</code> method. Of course, every unknown
  305. or not full qualified locale will also throw an exception. <code>setDefault()</code> should
  306. be the first call before you initiate any class using <classname>Zend_Locale</classname>. See the
  307. following example for details:
  308. </para>
  309. <example id="zend.locale.defaultlocale.example-2">
  310. <title>Setting a default locale</title>
  311. <programlisting language="php"><![CDATA[
  312. // within the bootstrap file
  313. Zend_Locale::setDefault('de');
  314. // within your model/controller
  315. $date = new Zend_Date();
  316. ]]></programlisting>
  317. </example>
  318. <para>
  319. In the case that no locale can be detected, automatically the locale
  320. <emphasis>de</emphasis> will be used. Otherwise, the detected locale will be used.
  321. </para>
  322. </sect2>
  323. <sect2 id="zend.locale.interoperate">
  324. <title>ZF Locale-Aware Classes</title>
  325. <para>
  326. In the ZF, locale-aware classes rely on <classname>Zend_Locale</classname> to automatically select a locale, as
  327. explained above. For example, in a ZF web application, constructing a date using <classname>Zend_Date</classname>
  328. without specifying a locale results in an object with a locale based on information provided by the current
  329. user's web browser.
  330. </para>
  331. <example id="zend.locale.interoperate.example-1">
  332. <title>Dates default to correct locale of web users</title>
  333. <programlisting language="php"><![CDATA[
  334. $date = new Zend_Date('2006',Zend_Date::YEAR);
  335. ]]></programlisting>
  336. </example>
  337. <para>
  338. To override this default behavior, and force locale-aware ZF components to use specific locales, regardless
  339. of the origin of your website visitors, explicitly specify a locale as the third argument to the
  340. constructor.
  341. </para>
  342. <example id="zend.locale.interoperate.example-2">
  343. <title>Overriding default locale selection</title>
  344. <programlisting language="php"><![CDATA[
  345. $usLocale = new Zend_Locale('en_US');
  346. $date = new Zend_Date('2006', Zend_Date::YEAR, $usLocale);
  347. $temp = new Zend_Measure_Temperature('100,10',
  348. Zend_Measure::TEMPERATURE,
  349. $usLocale);
  350. ]]></programlisting>
  351. </example>
  352. <para>
  353. If you know many objects should all use the same default locale, explicitly specify the default locale to
  354. avoid the overhead of each object determining the default locale.
  355. </para>
  356. <example id="zend.locale.interoperate.example-3">
  357. <title>Performance optimization when using a default locale</title>
  358. <programlisting language="php"><![CDATA[
  359. $locale = new Zend_Locale();
  360. $date = new Zend_Date('2006', Zend_Date::YEAR, $locale);
  361. $temp = new Zend_Measure_Temperature('100,10',
  362. Zend_Measure::TEMPERATURE,
  363. $locale);
  364. ]]></programlisting>
  365. </example>
  366. </sect2>
  367. <sect2 id="zend.locale.frameworkwidelocale">
  368. <title>Application wide locale</title>
  369. <para>
  370. Zend Framework allows the usage of an application wide locale. You simply set an instance of
  371. <classname>Zend_Locale</classname> to the registry with the key 'Zend_Locale'. Then this instance will
  372. be used within all locale aware classes of Zend Framework. This way you set one locale within
  373. your registry and then you can forget about setting it again. It will automatically be used
  374. in all other classes. See the below example for the right usage:
  375. </para>
  376. <example id="zend.locale.frameworkwidelocale.example">
  377. <title>Usage of an application wide locale</title>
  378. <programlisting language="php"><![CDATA[
  379. // within your bootstrap
  380. $locale = new Zend_Locale('de_AT');
  381. Zend_Registry::set('Zend_Locale', $locale);
  382. // within your model or controller
  383. $date = new Zend_Date();
  384. // print $date->getLocale();
  385. echo $date->getDate();
  386. ]]></programlisting>
  387. </example>
  388. </sect2>
  389. <sect2 id="zend.locale.formatoptions">
  390. <title>Zend_Locale_Format::setOptions(array $options)</title>
  391. <para>
  392. The 'precision' option of a value is used to truncate or stretch extra digits. A value of '-1' disables
  393. modification of the number of digits in the fractional part of the value. The 'locale' option helps when
  394. parsing numbers and dates using separators and month names. The date format 'format_type' option selects between
  395. CLDR/ISO date format specifier tokens and PHP's date() tokens. The 'fix_date' option enables or disables
  396. heuristics that attempt to correct invalid dates. The 'number_format' option specifies a default number
  397. format for use with <code>toNumber()</code> (see
  398. <xref
  399. linkend= "zend.locale.number.localize"/>
  400. ).
  401. </para>
  402. <para>
  403. The 'date_format' option can be used to specify a default date format string, but beware of using getDate(),
  404. checkdateFormat() and getTime() after using setOptions() with a 'date_format'. To use these four methods
  405. with the default date format for a locale, use array('date_format' => null, 'locale' => $locale) for their
  406. options.
  407. </para>
  408. <example id="zend.locale.formatoptions.example-1">
  409. <title>Dates default to correct locale of web users</title>
  410. <programlisting language="php"><![CDATA[
  411. Zend_Locale_Format::setOptions(array('locale' => 'en_US',
  412. 'fix_date' => true,
  413. 'format_type' => 'php'));
  414. ]]></programlisting>
  415. </example>
  416. <para>
  417. For working with the standard definitions of a locale the option <classname>Zend_Locale_Format::STANDARD</classname> can be used.
  418. Setting the option <classname>Zend_Locale_Format::STANDARD</classname> for <code>date_format</code> uses the standard definitions from the actual
  419. set locale. Setting it for number_format uses the standard number format for this locale.
  420. And setting it for locale uses the standard locale for this environment or browser.
  421. </para>
  422. <example id="zend.locale.formatoptions.example-2">
  423. <title>Using STANDARD definitions for setOptions()</title>
  424. <programlisting language="php"><![CDATA[
  425. Zend_Locale_Format::setOptions(array('locale' => 'en_US',
  426. 'date_format' => 'dd.MMMM.YYYY'));
  427. // overriding the global set date format
  428. $date = Zend_Locale_Format::getDate('2007-04-20',
  429. array('date_format' =>
  430. Zend_Locale_Format::STANDARD);
  431. // global setting of the standard locale
  432. Zend_Locale_Format::setOptions(array('locale' => Zend_Locale_Format::STANDARD,
  433. 'date_format' => 'dd.MMMM.YYYY'));
  434. ]]></programlisting>
  435. </example>
  436. </sect2>
  437. <sect2 id="zend.locale.cache">
  438. <title>Speed up Zend_Locale and its subclasses</title>
  439. <para>
  440. <classname>Zend_Locale</classname> and its subclasses can be speed up by the usage of
  441. <classname>Zend_Cache</classname>. Use the static method
  442. <classname>Zend_Locale::setCache($cache)</classname> if you are using
  443. <classname>Zend_Locale</classname>. <classname>Zend_Locale_Format</classname> can be
  444. speed up the using the option <code>cache</code> within
  445. <classname>Zend_Locale_Format::setOptions(array('cache' => $adapter));</classname>.
  446. If you are using both classes you should only set the cache for
  447. <classname>Zend_Locale</classname>, otherwise the last set cache will overwrite the
  448. previous set cache. For convenience there are also the static methods
  449. <methodname>getCache()</methodname>, <methodname>hasCache()</methodname>,
  450. <methodname>clearCache()</methodname> and <methodname>removeCache()</methodname>.
  451. </para>
  452. <para>
  453. When no cache is set, then <classname>Zend_Locale</classname> will automatically set a
  454. cache itself. Sometimes it is wished to prevent that a cache is set, even if this
  455. degrades performance. In this case the static
  456. <methodname>disableCache(true)</methodname> method should be used. It does not only
  457. disable the actual set cache, without erasing it, but also prevents that a cache is
  458. automatically generated when no cache is set.
  459. </para>
  460. </sect2>
  461. </sect1>
  462. <!--
  463. vim:se ts=4 sw=4 et:
  464. -->