1
0
mirror of https://github.com/flarum/core.git synced 2025-07-23 09:41:26 +02:00

Automatically include the appropriate translations from extensions

This commit is contained in:
Toby Zerner
2015-11-02 18:51:12 +10:30
parent be18298687
commit 1c8b43c0b9
4 changed files with 21 additions and 43 deletions

View File

@@ -32,7 +32,7 @@ class ClientController extends BaseClientController
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected $translationKeys = ['core.admin', 'core.lib']; protected $translations = '/[^\.]\.(admin|lib)\.*+/';
/** /**
* @var ExtensionManager * @var ExtensionManager

View File

@@ -18,30 +18,23 @@ use Flarum\Forum\Controller\ClientController as ForumClientAction;
class ConfigureClientView class ConfigureClientView
{ {
/** /**
* @var \Flarum\Http\Controller\AbstractClientController * @var AbstractClientController
*/ */
public $action; public $action;
/** /**
* @var \Flarum\Http\\Flarum\Http\Controller\ClientView * @var ClientView
*/ */
public $view; public $view;
/** /**
* @var array * @param AbstractClientController $action
* @param ClientView $view
*/ */
public $keys; public function __construct(AbstractClientController $action, ClientView $view)
/**
* @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)
{ {
$this->action = $action; $this->action = $action;
$this->view = $view; $this->view = $view;
$this->keys = &$keys;
} }
public function isForum() public function isForum()
@@ -63,11 +56,4 @@ class ConfigureClientView
{ {
$this->view->addBootstrapper($bootstrapper); $this->view->addBootstrapper($bootstrapper);
} }
public function addTranslations($keys)
{
foreach ((array) $keys as $key) {
$this->keys[] = $key;
}
}
} }

View File

@@ -29,7 +29,7 @@ class ClientController extends AbstractClientController
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected $translationKeys = ['core.forum', 'core.lib']; protected $translations = '/[^\.]\.(forum|lib)\.*+/';
/** /**
* @var Formatter * @var Formatter

View File

@@ -15,7 +15,6 @@ use Flarum\Asset\AssetManager;
use Flarum\Asset\JsCompiler; use Flarum\Asset\JsCompiler;
use Flarum\Asset\LessCompiler; use Flarum\Asset\LessCompiler;
use Flarum\Core; use Flarum\Core;
use Flarum\Core\User;
use Flarum\Event\ConfigureClientView; use Flarum\Event\ConfigureClientView;
use Flarum\Foundation\Application; use Flarum\Foundation\Application;
use Flarum\Locale\JsCompiler as LocaleJsCompiler; use Flarum\Locale\JsCompiler as LocaleJsCompiler;
@@ -59,12 +58,12 @@ abstract class AbstractClientController extends AbstractHtmlController
protected $layout; protected $layout;
/** /**
* The keys of the translations that should be included in the compiled * A regex matching the keys of the translations that should be included in
* locale file. * the compiled locale file.
* *
* @var array * @var string
*/ */
protected $translationKeys = []; protected $translations;
/** /**
* @var \Flarum\Foundation\Application * @var \Flarum\Foundation\Application
@@ -144,22 +143,14 @@ abstract class AbstractClientController extends AbstractHtmlController
$view->setVariable('locales', $this->locales->getLocales()); $view->setVariable('locales', $this->locales->getLocales());
$view->setVariable('locale', $locale); $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( $this->events->fire(
new ConfigureClientView($this, $view, $keys) new ConfigureClientView($this, $view)
); );
if ($localeCompiler) { if ($localeCompiler) {
$translations = array_get($this->locales->getTranslator()->getMessages(), 'messages', []); $translations = array_get($this->locales->getTranslator()->getMessages(), 'messages', []);
$translations = $this->filterTranslations($translations, $keys); $translations = $this->filterTranslations($translations);
$localeCompiler->setTranslations($translations); $localeCompiler->setTranslations($translations);
} }
@@ -309,16 +300,17 @@ abstract class AbstractClientController extends AbstractHtmlController
* Take a selection of keys from a collection of translations. * Take a selection of keys from a collection of translations.
* *
* @param array $translations * @param array $translations
* @param array $keys
* @return array * @return array
*/ */
protected function filterTranslations(array $translations, array $keys) protected function filterTranslations(array $translations)
{ {
$filtered = array_filter(array_keys($translations), function ($id) use ($keys) { if (! $this->translations) {
foreach ($keys as $key) { return [];
if (substr($id, 0, strlen($key)) === $key) {
return true;
} }
$filtered = array_filter(array_keys($translations), function ($id) {
if (preg_match($this->translations, $id)) {
return true;
} }
}); });