Zend_Locale-Introduction.xml 27 KB

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