1
0
mirror of https://github.com/flarum/core.git synced 2025-10-13 16:05:05 +02:00

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
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());
}
}
}
}