42 lines
1.4 KiB
PHP
Raw Normal View History

2015-09-25 15:37:00 +09:30
<?php
2015-10-07 23:07:44 +10:30
use Illuminate\Contracts\Events\Dispatcher;
use Flarum\Event\ConfigureLocales;
2015-09-25 15:37:00 +09:30
2015-10-07 23:07:44 +10:30
return function (Dispatcher $events) {
$events->listen(ConfigureLocales::class, function(ConfigureLocales $event) {
$name = $title = basename(__DIR__);
2015-09-25 15:37:00 +09:30
if (file_exists($manifest = __DIR__.'/composer.json')) {
2015-09-25 15:37:00 +09:30
$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);
2015-09-25 15:37:00 +09:30
}
if (! isset($locale)) {
throw new RuntimeException("Language pack $name must define \"extra.flarum-locale.code\" in composer.json.");
2015-09-25 15:37:00 +09:30
}
2015-10-07 23:07:44 +10:30
$event->locales->addLocale($locale, $title);
2015-09-25 15:37:00 +09:30
if (! is_dir($localeDir = __DIR__.'/locale')) {
throw new RuntimeException("Language pack $name must have a \"locale\" subdirectory.");
2015-09-25 15:37:00 +09:30
}
if (file_exists($file = $localeDir.'/config.js')) {
2015-10-07 23:07:44 +10:30
$event->locales->addJsFile($locale, $file);
2015-09-25 15:37:00 +09:30
}
foreach (new DirectoryIterator($localeDir) as $file) {
2015-09-25 15:37:00 +09:30
if ($file->isFile() && in_array($file->getExtension(), ['yml', 'yaml'])) {
2015-10-07 23:07:44 +10:30
$event->locales->addTranslations($locale, $file->getPathname());
2015-09-25 15:37:00 +09:30
}
}
});
};