From ee7a4627d88a6d725806de5f0dd870ad9371e04b Mon Sep 17 00:00:00 2001 From: Robert Korulczyk Date: Sun, 19 Apr 2020 16:29:45 +0200 Subject: [PATCH] Load only translations for enabled extensions from language packs (#2020) fix #1837 Co-authored-by: Daniel Klabbers --- src/Extend/LanguagePack.php | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Extend/LanguagePack.php b/src/Extend/LanguagePack.php index 13599acad..cb74d1cac 100644 --- a/src/Extend/LanguagePack.php +++ b/src/Extend/LanguagePack.php @@ -11,6 +11,7 @@ namespace Flarum\Extend; use DirectoryIterator; use Flarum\Extension\Extension; +use Flarum\Extension\ExtensionManager; use Flarum\Locale\LocaleManager; use Illuminate\Contracts\Container\Container; use InvalidArgumentException; @@ -18,6 +19,11 @@ use RuntimeException; class LanguagePack implements ExtenderInterface, LifecycleInterface { + private const CORE_LOCALE_FILES = [ + 'core', + 'validation', + ]; + private $path; /** @@ -49,13 +55,13 @@ class LanguagePack implements ExtenderInterface, LifecycleInterface $container->resolving( LocaleManager::class, - function (LocaleManager $locales) use ($extension, $locale, $title) { - $this->registerLocale($locales, $extension, $locale, $title); + function (LocaleManager $locales) use ($extension, $locale, $title, $container) { + $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); @@ -75,8 +81,17 @@ class LanguagePack implements ExtenderInterface, LifecycleInterface $locales->addCssFile($locale, $file); } + /** @var ExtensionManager $extensions */ + $extensions = $container->make(ExtensionManager::class); + 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()); } }