| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\I18n;
- use Aura\Intl\Package;
- use Cake\Core\App;
- use Cake\Core\Plugin;
- use Cake\Utility\Inflector;
- use \Locale;
- /**
- * A generic translations package factory that will load translations files
- * based on the file extension and the package name.
- *
- * This class is a callable, so it can be used as a package loader argument.
- */
- class MessagesFileLoader {
- /**
- * The package (domain) name.
- *
- * @var string
- */
- protected $_name;
- /**
- * The locale to load for the given package.
- *
- * @var string
- */
- protected $_locale;
- /**
- * The extension name.
- *
- * @var string
- */
- protected $_extension;
- /**
- * Creates a translation file loader. The file to be loaded corresponds to
- * the following rules:
- *
- * - The locale is a folder under the `Locale` directory, a fallback will be
- * used if the folder is not found.
- * - The $name corresponds to the file name to load
- * - If there is a loaded plugin with the underscored version of $name, the
- * translation file will be loaded from such plugin.
- *
- * @param string $name The name (domain) of the translations package.
- * @param string $locale The locale to load, this will be mapped to a folder
- * in the system.
- * @param string $extension The file extension to use. This will also be mapped
- * to a messages parser class.
- */
- public function __construct($name, $locale, $extension = 'po') {
- $this->_name = $name;
- $this->_locale = $locale;
- $this->_extension = $extension;
- }
- /**
- * Loads the translation file and parses it. Returns an instance of a translations
- * package containing the messages loaded from the file.
- *
- * @return Aura\Intl\Package
- * @throws \RuntimeException if no file parser class could be found for the specified
- * file extension.
- */
- public function __invoke() {
- $package = new Package;
- $folder = $this->translationsFolder();
- $ext = $this->_extension;
- if (!$folder || !is_file($folder . $this->_name . ".$ext")) {
- return $package;
- }
- $name = ucfirst($ext);
- $class = App::classname($name, 'I18n\Parser', 'FileParser');
- if (!$class) {
- throw new \RuntimeException(sprintf('Could not find class %s', "{$name}FileParser"));
- }
- $messages = (new $class)->parse($folder . $this->_name . ".$ext");
- $package->setMessages($messages);
- return $package;
- }
- /**
- * Returns the folder where the file should be looked for according to the locale
- * and package name.
- *
- * @return string|boolean The folder where the file should be looked for or false
- * if it does not exists.
- */
- public function translationsFolder() {
- $locale = Locale::parseLocale($this->_locale) + ['region' => null];
- $folders = [
- implode('_', [$locale['language'], $locale['region']]),
- $locale['language']
- ];
- $pluginName = Inflector::camelize($this->_name);
- $basePath = APP . 'Locale' . DS;
- if (Plugin::loaded($pluginName)) {
- $basePath = Plugin::path($pluginName) . 'src' . DS . 'Locale' . DS;
- }
- foreach ($folders as $folder) {
- $path = $basePath . $folder . DS;
- if (is_dir($path)) {
- return $path;
- }
- }
- return false;
- }
- }
|