From 92cb3b9166c09e601d0e1be4d400fe8384cad732 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Mon, 19 Mar 2018 01:06:07 +0100 Subject: [PATCH] Split up Locale extender Now we have two extenders: - `Extend\LanguagePack` is the "convention over configuration" loader for complete language packs. - `Extend\Locales` can be used to load files (by locale) from a given directory - useful for extensions that bring along their own locales in multiple different languages. Refs #851. --- framework/core/src/Extend/LanguagePack.php | 66 +++++++++++++++++++ framework/core/src/Extend/Locale.php | 76 ---------------------- framework/core/src/Extend/Locales.php | 49 ++++++++++++++ 3 files changed, 115 insertions(+), 76 deletions(-) create mode 100644 framework/core/src/Extend/LanguagePack.php delete mode 100644 framework/core/src/Extend/Locale.php create mode 100644 framework/core/src/Extend/Locales.php diff --git a/framework/core/src/Extend/LanguagePack.php b/framework/core/src/Extend/LanguagePack.php new file mode 100644 index 000000000..7d1ed4931 --- /dev/null +++ b/framework/core/src/Extend/LanguagePack.php @@ -0,0 +1,66 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Extend; + +use DirectoryIterator; +use Flarum\Extension\Extension; +use Flarum\Locale\LocaleManager; +use Illuminate\Contracts\Container\Container; +use InvalidArgumentException; +use RuntimeException; + +class LanguagePack implements Extender +{ + public function __invoke(Container $container, Extension $extension = null) + { + if (is_null($extension)) { + throw new InvalidArgumentException( + 'I need an extension instance to register a language pack' + ); + } + + $locale = $extension->composerJsonAttribute('extra.flarum-locale.code'); + $title = $extension->composerJsonAttribute('extra.flarum-locale.title'); + + if (! isset($locale, $title)) { + throw new RuntimeException( + 'Language packs must define "extra.flarum-locale.code" and "extra.flarum-locale.title" in composer.json.' + ); + } + + /** @var LocaleManager $locales */ + $locales = $container->make(LocaleManager::class); + $locales->addLocale($locale, $title); + + $directory = $extension->getPath().'/locale'; + + if (! is_dir($directory)) { + throw new RuntimeException( + 'Language packs must have a "locale" subdirectory.' + ); + } + + if (file_exists($file = $directory.'/config.js')) { + $locales->addJsFile($locale, $file); + } + + if (file_exists($file = $directory.'/config.css')) { + $locales->addCssFile($locale, $file); + } + + foreach (new DirectoryIterator($directory) as $file) { + if ($file->isFile() && in_array($file->getExtension(), ['yml', 'yaml'])) { + $locales->addTranslations($locale, $file->getPathname()); + } + } + } +} diff --git a/framework/core/src/Extend/Locale.php b/framework/core/src/Extend/Locale.php deleted file mode 100644 index f127bd6fb..000000000 --- a/framework/core/src/Extend/Locale.php +++ /dev/null @@ -1,76 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Extend; - -use DirectoryIterator; -use Flarum\Extension\Extension; -use Flarum\Locale\LocaleManager; -use Illuminate\Contracts\Container\Container; -use RuntimeException; - -class Locale implements Extender -{ - protected $directory; - - public function __construct($directory) - { - $this->directory = $directory; - } - - public function __invoke(Container $container, Extension $extension = null) - { - $this->loadLanguagePackFrom( - $this->directory, - $container->make(LocaleManager::class) - ); - } - - private function loadLanguagePackFrom($directory, LocaleManager $locales) - { - $name = $title = basename($directory); - - if (file_exists($manifest = $directory.'/composer.json')) { - $json = json_decode(file_get_contents($manifest), true); - - if (empty($json)) { - throw new RuntimeException("Error parsing composer.json in $name: ".json_last_error_msg()); - } - - $locale = array_get($json, 'extra.flarum-locale.code'); - $title = array_get($json, 'extra.flarum-locale.title', $title); - } - - if (! isset($locale)) { - throw new RuntimeException("Language pack $name must define \"extra.flarum-locale.code\" in composer.json."); - } - - $locales->addLocale($locale, $title); - - if (! is_dir($localeDir = $directory.'/locale')) { - throw new RuntimeException("Language pack $name must have a \"locale\" subdirectory."); - } - - if (file_exists($file = $localeDir.'/config.js')) { - $locales->addJsFile($locale, $file); - } - - if (file_exists($file = $localeDir.'/config.css')) { - $locales->addCssFile($locale, $file); - } - - foreach (new DirectoryIterator($localeDir) as $file) { - if ($file->isFile() && in_array($file->getExtension(), ['yml', 'yaml'])) { - $locales->addTranslations($locale, $file->getPathname()); - } - } - } -} diff --git a/framework/core/src/Extend/Locales.php b/framework/core/src/Extend/Locales.php new file mode 100644 index 000000000..94409130c --- /dev/null +++ b/framework/core/src/Extend/Locales.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Extend; + +use DirectoryIterator; +use Flarum\Extension\Extension; +use Flarum\Locale\LocaleManager; +use Illuminate\Contracts\Container\Container; + +class Locales implements Extender +{ + protected $directory; + + public function __construct($directory) + { + $this->directory = $directory; + } + + public function __invoke(Container $container, Extension $extension = null) + { + /** @var LocaleManager $locales */ + $locales = $container->make(LocaleManager::class); + + foreach (new DirectoryIterator($this->directory) as $file) { + if (! $file->isFile()) { + continue; + } + + $extension = $file->getExtension(); + if (! in_array($extension, ['yml', 'yaml'])) { + continue; + } + + $locales->addTranslations( + $file->getBasename(".$extension"), + $file->getPathname() + ); + } + } +}