g11n.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. /**
  9. * This bootstrap file contains configurations for all globalizing
  10. * aspects of your application.
  11. */
  12. use lithium\core\Libraries;
  13. use lithium\core\Environment;
  14. use lithium\g11n\Locale;
  15. use lithium\g11n\Catalog;
  16. use lithium\g11n\Message;
  17. use lithium\g11n\Multibyte;
  18. use lithium\util\Inflector;
  19. use lithium\util\Validator;
  20. use lithium\net\http\Media;
  21. use lithium\action\Dispatcher as ActionDispatcher;
  22. use lithium\console\Dispatcher as ConsoleDispatcher;
  23. /**
  24. * Dates
  25. *
  26. * Sets the default timezone used by all date/time functions.
  27. */
  28. date_default_timezone_set('UTC');
  29. /**
  30. * Locales
  31. *
  32. * Adds globalization specific settings to the environment. The settings for
  33. * the current locale, time zone and currency are kept as environment settings.
  34. * This allows for _centrally_ switching, _transparently_ setting and
  35. * retrieving globalization related settings.
  36. *
  37. * The environment settings are:
  38. *
  39. * - `'locale'` The default effective locale.
  40. * - `'locales'` Application locales available mapped to names. The available locales are used
  41. * to negotiate he effective locale, the names can be used i.e. when displaying
  42. * a menu for choosing the locale to users.
  43. *
  44. * @see lithiumm\g11n\Message
  45. * @see lithiumm\core\Environment
  46. */
  47. $locale = 'en';
  48. $locales = array('en' => 'English');
  49. Environment::set('production', compact('locale', 'locales'));
  50. Environment::set('development', compact('locale', 'locales'));
  51. Environment::set('test', array('locale' => 'en', 'locales' => array('en' => 'English')));
  52. /**
  53. * Effective/Request Locale
  54. *
  55. * Intercepts dispatching processes in order to set the effective locale by using
  56. * the locale of the request or if that is not available retrieving a locale preferred
  57. * by the client.
  58. *
  59. * @see lithiumm\g11n\Message
  60. * @see lithiumm\core\Environment
  61. */
  62. $setLocale = function($self, $params, $chain) {
  63. if (!$params['request']->locale()) {
  64. $params['request']->locale(Locale::preferred($params['request']));
  65. }
  66. Environment::set(true, array('locale' => $params['request']->locale()));
  67. return $chain->next($self, $params, $chain);
  68. };
  69. ActionDispatcher::applyFilter('_callable', $setLocale);
  70. ConsoleDispatcher::applyFilter('_callable', $setLocale);
  71. /**
  72. * Resources
  73. *
  74. * Globalization (g11n) catalog configuration. The catalog allows for obtaining and
  75. * writing globalized data. Each configuration can be adjusted through the following settings:
  76. *
  77. * - `'adapter'` _string_: The name of a supported adapter. The builtin adapters are `Memory` (a
  78. * simple adapter good for runtime data and testing), `Php`, `Gettext`, `Cldr` (for
  79. * interfacing with Unicode's common locale data repository) and `Code` (used mainly for
  80. * extracting message templates from source code).
  81. *
  82. * - `'path'` All adapters with the exception of the `Memory` adapter require a directory
  83. * which holds the data.
  84. *
  85. * - `'scope'` If you plan on using scoping i.e. for accessing plugin data separately you
  86. * need to specify a scope for each configuration, except for those using the `Memory`,
  87. * `Php` or `Gettext` adapter which handle this internally.
  88. *
  89. * @see lithiumm\g11n\Catalog
  90. * @link https://github.com/UnionOfRAD/li3_lldr
  91. * @link https://github.com/UnionOfRAD/li3_cldr
  92. */
  93. Catalog::config(array(
  94. 'runtime' => array(
  95. 'adapter' => 'Memory'
  96. ),
  97. // 'app' => array(
  98. // 'adapter' => 'Gettext',
  99. // 'path' => Libraries::get(true, 'resources') . '/g11n'
  100. // ),
  101. 'lithium' => array(
  102. 'adapter' => 'Php',
  103. 'path' => LITHIUM_LIBRARY_PATH . '/lithium/g11n/resources/php'
  104. )
  105. ) + Catalog::config());
  106. /**
  107. * Multibyte Strings
  108. *
  109. * Configuration for the `Multibyte` class which allows to work with UTF-8
  110. * encoded strings. At least one configuration named `'default'` must be
  111. * present. Available adapters are `Intl`, `Mbstring` and `Iconv`. Please keep
  112. * in mind that each adapter may act differently upon input containing bad
  113. * UTF-8 sequences. These differences aren't currently equalized or abstracted
  114. * away.
  115. *
  116. * @see lithiumm\g11n\Multibyte
  117. */
  118. Multibyte::config(array(
  119. // 'default' => array('adapter' => 'Intl'),
  120. 'default' => array('adapter' => 'Mbstring'),
  121. // 'default' => array('adapter' => 'Iconv')
  122. ));
  123. /**
  124. * Transliteration
  125. *
  126. * Load locale specific transliteration rules through the `Catalog` class or
  127. * specify them manually to make `Inflector::slug()` work better with
  128. * characters specific to a locale.
  129. *
  130. * @see lithiumm\g11n\Catalog
  131. * @see lithium\util\Inflector::slug()
  132. */
  133. // Inflector::rules('transliteration', Catalog::read(true, 'inflection.transliteration', 'en'));
  134. // Inflector::rules('transliteration', array('/É|Ê/' => 'E'));
  135. /**
  136. * Grammar
  137. *
  138. * If your application has custom singular or plural rules you can configure
  139. * that by uncommenting the lines below.
  140. *
  141. * @see lithiumm\g11n\Catalog
  142. * @see lithium\util\Inflector
  143. */
  144. // Inflector::rules('singular', array('rules' => array('/rata/' => '\1ratus')));
  145. // Inflector::rules('singular', array('irregular' => array('foo' => 'bar')));
  146. // Inflector::rules('plural', array('rules' => array('/rata/' => '\1ratum')));
  147. // Inflector::rules('plural', array('irregular' => array('bar' => 'foo')));
  148. // Inflector::rules('uninflected', 'bord');
  149. // Inflector::rules('uninflected', array('bord', 'baird'));
  150. /**
  151. * Validation
  152. *
  153. * Adds locale specific rules through the `Catalog` class. You can load more
  154. * locale dependent rules into the by specifying them manually or retrieving
  155. * them with the `Catalog` class.
  156. *
  157. * Enables support for multibyte strings through the `Multibyte` class by
  158. * overwriting rules (currently just `lengthBetween`).
  159. *
  160. * @see lithiumm\g11n\Catalog
  161. * @see lithiumm\g11n\Multibyte
  162. * @see lithium\util\Validator
  163. */
  164. foreach (array('phone', 'postalCode', 'ssn') as $name) {
  165. Validator::add($name, Catalog::read(true, "validation.{$name}", 'en_US'));
  166. }
  167. Validator::add('lengthBetween', function($value, $format, $options) {
  168. $length = Multibyte::strlen($value);
  169. $options += array('min' => 1, 'max' => 255);
  170. return ($length >= $options['min'] && $length <= $options['max']);
  171. });
  172. /**
  173. * In-View Translation
  174. *
  175. * Integration with `View`. Embeds message translation aliases into the `View`
  176. * class (or other content handler, if specified) when content is rendered. This
  177. * enables translation functions, i.e. `<?=$t("Translated content"); ?>`.
  178. *
  179. * @see lithiumm\g11n\Message::aliases()
  180. * @see lithiumm\net\http\Media
  181. */
  182. Media::applyFilter('_handle', function($self, $params, $chain) {
  183. $params['handler'] += array('outputFilters' => array());
  184. $params['handler']['outputFilters'] += Message::aliases();
  185. return $chain->next($self, $params, $chain);
  186. });
  187. ?>