PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/cakephp/cakephp/src/I18n/MessagesFileLoader.php

https://gitlab.com/vannh/portal_training
PHP | 182 lines | 76 code | 18 blank | 88 comment | 6 complexity | c7645e9c2cee62bf2913ec22e56bf8b9 MD5 | raw file
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\I18n;
  16. use Aura\Intl\Package;
  17. use Cake\Core\App;
  18. use Cake\Core\Plugin;
  19. use Cake\Utility\Inflector;
  20. use Locale;
  21. use RuntimeException;
  22. /**
  23. * A generic translations package factory that will load translations files
  24. * based on the file extension and the package name.
  25. *
  26. * This class is a callable, so it can be used as a package loader argument.
  27. */
  28. class MessagesFileLoader
  29. {
  30. /**
  31. * The package (domain) name.
  32. *
  33. * @var string
  34. */
  35. protected $_name;
  36. /**
  37. * The locale to load for the given package.
  38. *
  39. * @var string
  40. */
  41. protected $_locale;
  42. /**
  43. * The extension name.
  44. *
  45. * @var string
  46. */
  47. protected $_extension;
  48. /**
  49. * Creates a translation file loader. The file to be loaded corresponds to
  50. * the following rules:
  51. *
  52. * - The locale is a folder under the `Locale` directory, a fallback will be
  53. * used if the folder is not found.
  54. * - The $name corresponds to the file name to load
  55. * - If there is a loaded plugin with the underscored version of $name, the
  56. * translation file will be loaded from such plugin.
  57. *
  58. * ### Examples:
  59. *
  60. * Load and parse src/Locale/fr/validation.po
  61. *
  62. * ```
  63. * $loader = new MessagesFileLoader('validation', 'fr_FR', 'po');
  64. * $package = $loader();
  65. * ```
  66. *
  67. * Load and parse src/Locale/fr_FR/validation.mo
  68. *
  69. * ```
  70. * $loader = new MessagesFileLoader('validation', 'fr_FR', 'mo');
  71. * $package = $loader();
  72. * ```
  73. *
  74. * Load the plugins/MyPlugin/src/Locale/fr/my_plugin.po file:
  75. *
  76. * ```
  77. * $loader = new MessagesFileLoader('my_plugin', 'fr_FR', 'mo');
  78. * $package = $loader();
  79. * ```
  80. *
  81. * @param string $name The name (domain) of the translations package.
  82. * @param string $locale The locale to load, this will be mapped to a folder
  83. * in the system.
  84. * @param string $extension The file extension to use. This will also be mapped
  85. * to a messages parser class.
  86. */
  87. public function __construct($name, $locale, $extension = 'po')
  88. {
  89. $this->_name = $name;
  90. $this->_locale = $locale;
  91. $this->_extension = $extension;
  92. }
  93. /**
  94. * Loads the translation file and parses it. Returns an instance of a translations
  95. * package containing the messages loaded from the file.
  96. *
  97. * @return \Aura\Intl\Package
  98. * @throws \RuntimeException if no file parser class could be found for the specified
  99. * file extension.
  100. */
  101. public function __invoke()
  102. {
  103. $package = new Package('default');
  104. $folders = $this->translationsFolders();
  105. $ext = $this->_extension;
  106. $file = false;
  107. $fileName = $this->_name;
  108. $pos = strpos($fileName, '/');
  109. if ($pos !== false) {
  110. $fileName = substr($fileName, $pos + 1);
  111. }
  112. foreach ($folders as $folder) {
  113. $path = $folder . $fileName . ".$ext";
  114. if (is_file($path)) {
  115. $file = $path;
  116. break;
  117. }
  118. }
  119. if (!$file) {
  120. return $package;
  121. }
  122. $name = ucfirst($ext);
  123. $class = App::classname($name, 'I18n\Parser', 'FileParser');
  124. if (!$class) {
  125. throw new RuntimeException(sprintf('Could not find class %s', "{$name}FileParser"));
  126. }
  127. $messages = (new $class)->parse($file);
  128. $package->setMessages($messages);
  129. return $package;
  130. }
  131. /**
  132. * Returns the folders where the file should be looked for according to the locale
  133. * and package name.
  134. *
  135. * @return array The list of folders where the translation file should be looked for
  136. */
  137. public function translationsFolders()
  138. {
  139. $locale = Locale::parseLocale($this->_locale) + ['region' => null];
  140. $folders = [
  141. implode('_', [$locale['language'], $locale['region']]),
  142. $locale['language']
  143. ];
  144. $searchPaths = [];
  145. $localePaths = App::path('Locale');
  146. if (empty($localePaths)) {
  147. $localePaths[] = APP . 'Locale' . DS;
  148. }
  149. foreach ($localePaths as $path) {
  150. foreach ($folders as $folder) {
  151. $searchPaths[] = $path . $folder . DS;
  152. }
  153. }
  154. // If space is not added after slash, the character after it remains lowercased
  155. $pluginName = Inflector::camelize(str_replace('/', '/ ', $this->_name));
  156. if (Plugin::loaded($pluginName)) {
  157. $basePath = Plugin::classPath($pluginName) . 'Locale' . DS;
  158. foreach ($folders as $folder) {
  159. $searchPaths[] = $basePath . $folder . DS;
  160. }
  161. }
  162. return $searchPaths;
  163. }
  164. }