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.
This commit is contained in:
Franz Liedke 2018-03-19 01:06:07 +01:00
parent b4e093ab8a
commit 8d2d987680
No known key found for this signature in database
GPG Key ID: 9A0231A879B055F4
3 changed files with 115 additions and 76 deletions

View File

@ -0,0 +1,66 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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());
}
}
}
}

View File

@ -1,76 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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());
}
}
}
}

49
src/Extend/Locales.php Normal file
View File

@ -0,0 +1,49 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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()
);
}
}
}