From 39adb0f223cb8a98f7d6609520be93cd88119c68 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Mon, 28 Nov 2016 11:28:10 +1030 Subject: [PATCH] Support module prefixing of locale resources In preparation for upcoming changes, allow locale resources to have a module prefix added when they are loaded from a file. --- .../core/src/Http/WebApp/AbstractWebApp.php | 2 +- framework/core/src/Locale/LocaleManager.php | 6 ++-- .../core/src/Locale/LocaleServiceProvider.php | 2 +- .../src/Locale/PrefixedYamlFileLoader.php | 36 +++++++++++++++++++ 4 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 framework/core/src/Locale/PrefixedYamlFileLoader.php diff --git a/framework/core/src/Http/WebApp/AbstractWebApp.php b/framework/core/src/Http/WebApp/AbstractWebApp.php index c992d79da..d71ca7205 100644 --- a/framework/core/src/Http/WebApp/AbstractWebApp.php +++ b/framework/core/src/Http/WebApp/AbstractWebApp.php @@ -95,7 +95,7 @@ abstract class AbstractWebApp */ protected function getTranslationFilter() { - return '/^[^\.]+\.(?:'.$this->getName().'|lib)\./'; + return '/^.+(?:\.|::)(?:'.$this->getName().'|lib)\./'; } /** diff --git a/framework/core/src/Locale/LocaleManager.php b/framework/core/src/Locale/LocaleManager.php index 89e0a8dc7..f1255fdad 100644 --- a/framework/core/src/Locale/LocaleManager.php +++ b/framework/core/src/Locale/LocaleManager.php @@ -58,9 +58,11 @@ class LocaleManager return isset($this->locales[$locale]); } - public function addTranslations($locale, $file) + public function addTranslations($locale, $file, $module = null) { - $this->translator->addResource('yaml', $file, $locale); + $prefix = $module ? $module.'::' : ''; + + $this->translator->addResource('prefixed_yaml', compact('file', 'prefix'), $locale); } public function addJsFile($locale, $js) diff --git a/framework/core/src/Locale/LocaleServiceProvider.php b/framework/core/src/Locale/LocaleServiceProvider.php index 019296259..488190af0 100644 --- a/framework/core/src/Locale/LocaleServiceProvider.php +++ b/framework/core/src/Locale/LocaleServiceProvider.php @@ -43,7 +43,7 @@ class LocaleServiceProvider extends AbstractServiceProvider $translator = new Translator($defaultLocale, new MessageSelector()); $translator->setFallbackLocales([$defaultLocale, 'en']); - $translator->addLoader('yaml', new YamlFileLoader()); + $translator->addLoader('prefixed_yaml', new PrefixedYamlFileLoader()); return $translator; }); diff --git a/framework/core/src/Locale/PrefixedYamlFileLoader.php b/framework/core/src/Locale/PrefixedYamlFileLoader.php new file mode 100644 index 000000000..bba493291 --- /dev/null +++ b/framework/core/src/Locale/PrefixedYamlFileLoader.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Locale; + +use Symfony\Component\Translation\Loader\YamlFileLoader; + +class PrefixedYamlFileLoader extends YamlFileLoader +{ + /** + * {@inheritdoc} + */ + public function load($resource, $locale, $domain = 'messages') + { + $catalogue = parent::load($resource['file'], $locale, $domain); + + if (! empty($resource['prefix'])) { + $messages = $catalogue->all($domain); + + $prefixedKeys = array_map(function ($k) use ($resource) { + return $resource['prefix'].$k; + }, array_keys($messages)); + + $catalogue->replace(array_combine($prefixedKeys, $messages)); + } + + return $catalogue; + } +}