_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; } }