Zend_Locale-Introduction.xml 27 KB

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