diff --git a/src/Http/WebApp/AbstractWebApp.php b/src/Http/WebApp/AbstractWebApp.php index c992d79da..d71ca7205 100644 --- a/src/Http/WebApp/AbstractWebApp.php +++ b/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/src/Locale/LocaleManager.php b/src/Locale/LocaleManager.php index 89e0a8dc7..f1255fdad 100644 --- a/src/Locale/LocaleManager.php +++ b/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/src/Locale/LocaleServiceProvider.php b/src/Locale/LocaleServiceProvider.php index 019296259..488190af0 100644 --- a/src/Locale/LocaleServiceProvider.php +++ b/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/src/Locale/PrefixedYamlFileLoader.php b/src/Locale/PrefixedYamlFileLoader.php new file mode 100644 index 000000000..bba493291 --- /dev/null +++ b/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; + } +}