g11n.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2010, 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 class configuration for all aspects of globalizing your application,
  10. * including localization of text, validation rules, setting timezones and character inflections,
  11. * and identifying a user's locale.
  12. */
  13. use lithium\core\Libraries;
  14. use lithium\core\Environment;
  15. use lithium\g11n\Locale;
  16. use lithium\g11n\Catalog;
  17. use lithium\g11n\Message;
  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. * Sets the default timezone used by all date/time functions.
  25. */
  26. date_default_timezone_set('UTC');
  27. /**
  28. * Adds globalization specific settings to the environment.
  29. *
  30. * The settings for the current locale, time zone and currency are kept as environment
  31. * settings. This allows for _centrally_ switching, _transparently_ setting and retrieving
  32. * globalization related settings.
  33. *
  34. * The environment settings are:
  35. *
  36. * - `'locale'` The effective locale.
  37. * - `'locales'` Application locales available mapped to names. The available locales are used
  38. * to negotiate he effective locale, the names can be used i.e. when displaying
  39. * a menu for choosing the locale to users.
  40. */
  41. $locale = 'en';
  42. $locales = array('en' => 'English');
  43. Environment::set('production', compact('locale', 'locales'));
  44. Environment::set('development', compact('locale', 'locales'));
  45. Environment::set('test', array('locale' => 'en', 'locales' => array('en' => 'English')));
  46. /**
  47. * Globalization (g11n) catalog configuration. The catalog allows for obtaining and
  48. * writing globalized data. Each configuration can be adjusted through the following settings:
  49. *
  50. * - `'adapter'` _string_: The name of a supported adapter. The builtin adapters are `Memory` (a
  51. * simple adapter good for runtime data and testing), `Php`, `Gettext`, `Cldr` (for
  52. * interfacing with Unicode's common locale data repository) and `Code` (used mainly for
  53. * extracting message templates from source code).
  54. *
  55. * - `'path'` All adapters with the exception of the `Memory` adapter require a directory
  56. * which holds the data.
  57. *
  58. * - `'scope'` If you plan on using scoping i.e. for accessing plugin data separately you
  59. * need to specify a scope for each configuration, except for those using the `Memory`,
  60. * `Php` or `Gettext` adapter which handle this internally.
  61. */
  62. Catalog::config(array(
  63. 'runtime' => array(
  64. 'adapter' => 'Memory'
  65. ),
  66. // 'app' => array(
  67. // 'adapter' => 'Gettext',
  68. // 'path' => Libraries::get(true, 'resources') . '/g11n'
  69. // ),
  70. 'lithium' => array(
  71. 'adapter' => 'Php',
  72. 'path' => LITHIUM_LIBRARY_PATH . '/lithium/g11n/resources/php'
  73. )
  74. ) + Catalog::config());
  75. /**
  76. * Integration with `Inflector`.
  77. */
  78. // Inflector::rules('transliteration', Catalog::read(true, 'inflection.transliteration', 'en'));
  79. /**
  80. * Inflector configuration examples. If your application has custom singular or plural rules, or
  81. * extra non-ASCII characters to transliterate, you can configure that by uncommenting the lines
  82. * below.
  83. */
  84. // Inflector::rules('singular', array('rules' => array('/rata/' => '\1ratus')));
  85. // Inflector::rules('singular', array('irregular' => array('foo' => 'bar')));
  86. //
  87. // Inflector::rules('plural', array('rules' => array('/rata/' => '\1ratum')));
  88. // Inflector::rules('plural', array('irregular' => array('bar' => 'foo')));
  89. //
  90. // Inflector::rules('transliteration', array('/É|Ê/' => 'E'));
  91. //
  92. // Inflector::rules('uninflected', 'bord');
  93. // Inflector::rules('uninflected', array('bord', 'baird'));
  94. /**
  95. * Integration with `View`. Embeds message translation aliases into the `View`
  96. * class (or other content handler, if specified) when content is rendered. This
  97. * enables translation functions, i.e. `<?=$t("Translated content"); ?>`.
  98. */
  99. Media::applyFilter('_handle', function($self, $params, $chain) {
  100. $params['handler'] += array('outputFilters' => array());
  101. $params['handler']['outputFilters'] += Message::aliases();
  102. return $chain->next($self, $params, $chain);
  103. });
  104. /**
  105. * Integration with `Validator`. You can load locale dependent rules into the `Validator`
  106. * by specifying them manually or retrieving them with the `Catalog` class.
  107. */
  108. foreach (array('phone', 'postalCode', 'ssn') as $name) {
  109. Validator::add($name, Catalog::read(true, "validation.{$name}", 'en_US'));
  110. }
  111. /**
  112. * Intercepts dispatching processes in order to set the effective locale by using
  113. * the locale of the request or if that is not available retrieving a locale preferred
  114. * by the client.
  115. */
  116. ActionDispatcher::applyFilter('_callable', function($self, $params, $chain) {
  117. $request = $params['request'];
  118. $controller = $chain->next($self, $params, $chain);
  119. if (!$request->locale) {
  120. $request->params['locale'] = Locale::preferred($request);
  121. }
  122. Environment::set(Environment::get(), array('locale' => $request->locale));
  123. return $controller;
  124. });
  125. ConsoleDispatcher::applyFilter('_callable', function($self, $params, $chain) {
  126. $request = $params['request'];
  127. $command = $chain->next($self, $params, $chain);
  128. if (!$request->locale) {
  129. $request->params['locale'] = Locale::preferred($request);
  130. }
  131. Environment::set(Environment::get(), array('locale' => $request->locale));
  132. return $command;
  133. });
  134. ?>