Zend_Locale-Introduction.xml 27 KB

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