From 1c8b43c0b909e8acea4eb25835f889ab795ea4ee Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Mon, 2 Nov 2015 18:51:12 +1030 Subject: [PATCH] Automatically include the appropriate translations from extensions --- .../src/Admin/Controller/ClientController.php | 2 +- .../core/src/Event/ConfigureClientView.php | 24 +++---------- .../src/Forum/Controller/ClientController.php | 2 +- .../Controller/AbstractClientController.php | 36 ++++++++----------- 4 files changed, 21 insertions(+), 43 deletions(-) diff --git a/framework/core/src/Admin/Controller/ClientController.php b/framework/core/src/Admin/Controller/ClientController.php index efa3636f5..a9da61dbc 100644 --- a/framework/core/src/Admin/Controller/ClientController.php +++ b/framework/core/src/Admin/Controller/ClientController.php @@ -32,7 +32,7 @@ class ClientController extends BaseClientController /** * {@inheritdoc} */ - protected $translationKeys = ['core.admin', 'core.lib']; + protected $translations = '/[^\.]\.(admin|lib)\.*+/'; /** * @var ExtensionManager diff --git a/framework/core/src/Event/ConfigureClientView.php b/framework/core/src/Event/ConfigureClientView.php index d492a7de4..81c7ddc22 100644 --- a/framework/core/src/Event/ConfigureClientView.php +++ b/framework/core/src/Event/ConfigureClientView.php @@ -18,30 +18,23 @@ use Flarum\Forum\Controller\ClientController as ForumClientAction; class ConfigureClientView { /** - * @var \Flarum\Http\Controller\AbstractClientController + * @var AbstractClientController */ public $action; /** - * @var \Flarum\Http\\Flarum\Http\Controller\ClientView + * @var ClientView */ public $view; /** - * @var array + * @param AbstractClientController $action + * @param ClientView $view */ - public $keys; - - /** - * @param \Flarum\Http\\Flarum\Http\Controller\AbstractClientController $action - * @param \Flarum\Http\\Flarum\Http\Controller\ClientView $view - * @param array $keys - */ - public function __construct(AbstractClientController $action, ClientView $view, array &$keys) + public function __construct(AbstractClientController $action, ClientView $view) { $this->action = $action; $this->view = $view; - $this->keys = &$keys; } public function isForum() @@ -63,11 +56,4 @@ class ConfigureClientView { $this->view->addBootstrapper($bootstrapper); } - - public function addTranslations($keys) - { - foreach ((array) $keys as $key) { - $this->keys[] = $key; - } - } } diff --git a/framework/core/src/Forum/Controller/ClientController.php b/framework/core/src/Forum/Controller/ClientController.php index 2dbce6454..feee0d573 100644 --- a/framework/core/src/Forum/Controller/ClientController.php +++ b/framework/core/src/Forum/Controller/ClientController.php @@ -29,7 +29,7 @@ class ClientController extends AbstractClientController /** * {@inheritdoc} */ - protected $translationKeys = ['core.forum', 'core.lib']; + protected $translations = '/[^\.]\.(forum|lib)\.*+/'; /** * @var Formatter diff --git a/framework/core/src/Http/Controller/AbstractClientController.php b/framework/core/src/Http/Controller/AbstractClientController.php index a69299fff..f3d8891ef 100644 --- a/framework/core/src/Http/Controller/AbstractClientController.php +++ b/framework/core/src/Http/Controller/AbstractClientController.php @@ -15,7 +15,6 @@ use Flarum\Asset\AssetManager; use Flarum\Asset\JsCompiler; use Flarum\Asset\LessCompiler; use Flarum\Core; -use Flarum\Core\User; use Flarum\Event\ConfigureClientView; use Flarum\Foundation\Application; use Flarum\Locale\JsCompiler as LocaleJsCompiler; @@ -59,12 +58,12 @@ abstract class AbstractClientController extends AbstractHtmlController protected $layout; /** - * The keys of the translations that should be included in the compiled - * locale file. + * A regex matching the keys of the translations that should be included in + * the compiled locale file. * - * @var array + * @var string */ - protected $translationKeys = []; + protected $translations; /** * @var \Flarum\Foundation\Application @@ -144,22 +143,14 @@ abstract class AbstractClientController extends AbstractHtmlController $view->setVariable('locales', $this->locales->getLocales()); $view->setVariable('locale', $locale); - // Now that we've set up the ClientView instance, we can fire an event - // to give extensions the opportunity to add their own assets and - // translations. We will pass an array to the event which specifies - // which translations should be included in the locale file. Afterwards, - // we will filter all of the translations for the actor's locale and - // compile only the ones we need. - $keys = $this->translationKeys; - $this->events->fire( - new ConfigureClientView($this, $view, $keys) + new ConfigureClientView($this, $view) ); if ($localeCompiler) { $translations = array_get($this->locales->getTranslator()->getMessages(), 'messages', []); - $translations = $this->filterTranslations($translations, $keys); + $translations = $this->filterTranslations($translations); $localeCompiler->setTranslations($translations); } @@ -309,16 +300,17 @@ abstract class AbstractClientController extends AbstractHtmlController * Take a selection of keys from a collection of translations. * * @param array $translations - * @param array $keys * @return array */ - protected function filterTranslations(array $translations, array $keys) + protected function filterTranslations(array $translations) { - $filtered = array_filter(array_keys($translations), function ($id) use ($keys) { - foreach ($keys as $key) { - if (substr($id, 0, strlen($key)) === $key) { - return true; - } + if (! $this->translations) { + return []; + } + + $filtered = array_filter(array_keys($translations), function ($id) { + if (preg_match($this->translations, $id)) { + return true; } });