Load only translations for enabled extensions from language packs (#2020)

fix #1837

Co-authored-by: Daniel Klabbers <daniel+git@klabbers.email>
This commit is contained in:
Robert Korulczyk 2020-04-19 16:29:45 +02:00 committed by GitHub
parent b9fb92d49a
commit ee7a4627d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,6 +11,7 @@ namespace Flarum\Extend;
use DirectoryIterator; use DirectoryIterator;
use Flarum\Extension\Extension; use Flarum\Extension\Extension;
use Flarum\Extension\ExtensionManager;
use Flarum\Locale\LocaleManager; use Flarum\Locale\LocaleManager;
use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Container\Container;
use InvalidArgumentException; use InvalidArgumentException;
@ -18,6 +19,11 @@ use RuntimeException;
class LanguagePack implements ExtenderInterface, LifecycleInterface class LanguagePack implements ExtenderInterface, LifecycleInterface
{ {
private const CORE_LOCALE_FILES = [
'core',
'validation',
];
private $path; private $path;
/** /**
@ -49,13 +55,13 @@ class LanguagePack implements ExtenderInterface, LifecycleInterface
$container->resolving( $container->resolving(
LocaleManager::class, LocaleManager::class,
function (LocaleManager $locales) use ($extension, $locale, $title) { function (LocaleManager $locales) use ($extension, $locale, $title, $container) {
$this->registerLocale($locales, $extension, $locale, $title); $this->registerLocale($container, $locales, $extension, $locale, $title);
} }
); );
} }
private function registerLocale(LocaleManager $locales, Extension $extension, $locale, $title) private function registerLocale(Container $container, LocaleManager $locales, Extension $extension, $locale, $title)
{ {
$locales->addLocale($locale, $title); $locales->addLocale($locale, $title);
@ -75,8 +81,17 @@ class LanguagePack implements ExtenderInterface, LifecycleInterface
$locales->addCssFile($locale, $file); $locales->addCssFile($locale, $file);
} }
/** @var ExtensionManager $extensions */
$extensions = $container->make(ExtensionManager::class);
foreach (new DirectoryIterator($directory) as $file) { foreach (new DirectoryIterator($directory) as $file) {
if ($file->isFile() && in_array($file->getExtension(), ['yml', 'yaml'])) { $slug = $file->getBasename(".{$file->getExtension()}");
if (
$file->isFile()
&& in_array($file->getExtension(), ['yml', 'yaml'], true)
&& (in_array($slug, self::CORE_LOCALE_FILES, true) || $extensions->isEnabled($slug))
) {
$locales->addTranslations($locale, $file->getPathname()); $locales->addTranslations($locale, $file->getPathname());
} }
} }