1
0
mirror of https://github.com/flarum/core.git synced 2025-08-24 09:03:05 +02:00

Allow locale to be selected in footer

This commit is contained in:
Toby Zerner
2015-08-05 09:50:57 +09:30
parent 48df9bb678
commit f49d0e5341
11 changed files with 87 additions and 18 deletions

View File

@@ -31,7 +31,6 @@ class ClientAction extends BaseClientAction
$view = parent::render($request, $routeParams);
$view->setVariable('config', $this->settings->all());
$view->setVariable('locales', app('flarum.localeManager')->getLocales());
$view->setVariable('permissions', Permission::map());
$view->setVariable('extensions', app('flarum.extensions')->getInfo());

View File

@@ -24,6 +24,7 @@ class UsersServiceProvider extends ServiceProvider
$events->listen(RegisterUserPreferences::class, function (RegisterUserPreferences $event) {
$event->register('discloseOnline', 'boolval', true);
$event->register('indexProfile', 'boolval', true);
$event->register('locale');
});
$events->listen(ModelAllow::class, function (ModelAllow $event) {

View File

@@ -20,6 +20,11 @@ class LocaleManager
return $this->locales;
}
public function hasLocale($locale)
{
return isset($this->locales[$locale]);
}
public function addTranslations($locale, $translations)
{
if (! isset($this->translations[$locale])) {

View File

@@ -89,31 +89,35 @@ abstract class ClientAction extends HtmlAction
{
$actor = app('flarum.actor');
$assets = $this->getAssets();
$locale = $this->getLocaleCompiler($actor);
$locale = $this->getLocale($actor, $request);
$localeCompiler = $this->getLocaleCompiler($locale);
$view = new ClientView(
$this->apiClient,
$request,
$actor,
$assets,
$locale,
$localeCompiler,
$this->layout
);
$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.
$translations = $this->locales->getTranslations($actor->locale);
$translations = $this->locales->getTranslations($locale);
$keys = $this->translationKeys;
event(new BuildClientView($this, $view, $keys));
$translations = $this->filterTranslations($translations, $keys);
$locale->setTranslations($translations);
$localeCompiler->setTranslations($translations);
return $view;
}
@@ -202,15 +206,13 @@ abstract class ClientAction extends HtmlAction
}
/**
* Set up the locale compiler for the given user's locale.
* Set up the locale compiler for the given locale.
*
* @param User $actor
* @param string $locale
* @return LocaleJsCompiler
*/
protected function getLocaleCompiler(User $actor)
protected function getLocaleCompiler($locale)
{
$locale = $actor->locale;
$compiler = new LocaleJsCompiler($this->getAssetDirectory(), "$this->clientName-$locale.js");
foreach ($this->locales->getJsFiles($locale) as $file) {
@@ -220,6 +222,28 @@ abstract class ClientAction extends HtmlAction
return $compiler;
}
/**
* Get the name of the locale to use.
*
* @param User $actor
* @param Request $request
* @return string
*/
protected function getLocale(User $actor, Request $request)
{
if ($actor->exists) {
$locale = $actor->getPreference('locale');
} else {
$locale = array_get($request->getCookieParams(), 'locale');
}
if (! $locale || ! $this->locales->hasLocale($locale)) {
return $this->settings->get('default_locale', 'en');
}
return $locale;
}
/**
* Get the path to the directory where assets should be written.
*