mirror of
https://github.com/flarum/core.git
synced 2025-08-20 23:31:27 +02:00
Extract English translations into a language pack
To make this work, we add support for the client working without any locale. Also fixes #412.
This commit is contained in:
@@ -24,31 +24,15 @@ class LocaleServiceProvider extends ServiceProvider
|
||||
{
|
||||
$manager = $this->app->make('flarum.localeManager');
|
||||
|
||||
$this->registerLocale($manager, 'en', 'English');
|
||||
|
||||
event(new RegisterLocales($manager));
|
||||
}
|
||||
|
||||
public function registerLocale(LocaleManager $manager, $locale, $title)
|
||||
{
|
||||
$path = __DIR__.'/../../locale/'.$locale;
|
||||
|
||||
$manager->addLocale($locale, $title);
|
||||
$manager->addTranslations($locale, $path.'.yml');
|
||||
$manager->addConfig($locale, $path.'.php');
|
||||
$manager->addJsFile($locale, $path.'.js');
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
$this->app->singleton('Flarum\Locale\LocaleManager');
|
||||
|
||||
$this->app->alias('Flarum\Locale\LocaleManager', 'flarum.localeManager');
|
||||
|
||||
$this->app->bind('translator', function ($app) {
|
||||
$locales = $app->make('flarum.localeManager');
|
||||
|
||||
return new Translator($locales->getTranslations('en'), $locales->getConfig('en')['plural']);
|
||||
});
|
||||
$this->app->instance('translator', new Translator);
|
||||
}
|
||||
}
|
||||
|
@@ -15,21 +15,27 @@ use Closure;
|
||||
|
||||
class Translator implements TranslatorInterface
|
||||
{
|
||||
protected $translations;
|
||||
protected $translations = [];
|
||||
|
||||
protected $plural;
|
||||
|
||||
public function __construct(array $translations, Closure $plural)
|
||||
public function setTranslations(array $translations)
|
||||
{
|
||||
$this->translations = $translations;
|
||||
}
|
||||
|
||||
public function setPlural(callable $plural)
|
||||
{
|
||||
$this->plural = $plural;
|
||||
}
|
||||
|
||||
protected function plural($count)
|
||||
{
|
||||
$plural = $this->plural;
|
||||
if ($this->plural) {
|
||||
$plural = $this->plural;
|
||||
|
||||
return $plural($count);
|
||||
return $plural($count);
|
||||
}
|
||||
}
|
||||
|
||||
public function getLocale()
|
||||
@@ -47,7 +53,11 @@ class Translator implements TranslatorInterface
|
||||
$translation = array_get($this->translations, $id);
|
||||
|
||||
if (is_array($translation) && isset($parameters['count'])) {
|
||||
$translation = $translation[$this->plural($parameters['count'])];
|
||||
$plural = $this->plural($parameters['count']);
|
||||
|
||||
if ($plural) {
|
||||
$translation = $translation[$plural];
|
||||
}
|
||||
}
|
||||
|
||||
if (is_string($translation)) {
|
||||
|
@@ -100,15 +100,15 @@ abstract class ClientAction extends HtmlAction
|
||||
$actor = app('flarum.actor');
|
||||
$assets = $this->getAssets();
|
||||
$locale = $this->getLocale($actor, $request);
|
||||
$localeCompiler = $this->getLocaleCompiler($locale);
|
||||
$localeCompiler = $locale ? $this->getLocaleCompiler($locale) : null;
|
||||
|
||||
$view = new ClientView(
|
||||
$this->apiClient,
|
||||
$request,
|
||||
$actor,
|
||||
$assets,
|
||||
$localeCompiler,
|
||||
$this->layout
|
||||
$this->layout,
|
||||
$localeCompiler
|
||||
);
|
||||
|
||||
$view->setVariable('locales', $this->locales->getLocales());
|
||||
@@ -120,14 +120,17 @@ abstract class ClientAction extends HtmlAction
|
||||
// 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($locale);
|
||||
$keys = $this->translationKeys;
|
||||
|
||||
event(new BuildClientView($this, $view, $keys));
|
||||
|
||||
$translations = $this->filterTranslations($translations, $keys);
|
||||
if ($localeCompiler) {
|
||||
$translations = $this->locales->getTranslations($locale);
|
||||
|
||||
$localeCompiler->setTranslations($translations);
|
||||
$translations = $this->filterTranslations($translations, $keys);
|
||||
|
||||
$localeCompiler->setTranslations($translations);
|
||||
}
|
||||
|
||||
return $view;
|
||||
}
|
||||
@@ -141,6 +144,12 @@ abstract class ClientAction extends HtmlAction
|
||||
public function flushAssets()
|
||||
{
|
||||
$this->getAssets()->flush();
|
||||
|
||||
$locales = array_keys($this->locales->getLocales());
|
||||
|
||||
foreach ($locales as $locale) {
|
||||
$this->getLocaleCompiler($locale)->flush();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -251,11 +260,9 @@ abstract class ClientAction extends HtmlAction
|
||||
$locale = $this->settings->get('default_locale', 'en');
|
||||
}
|
||||
|
||||
if (! $this->locales->hasLocale($locale)) {
|
||||
return 'en';
|
||||
if ($this->locales->hasLocale($locale)) {
|
||||
return $locale;
|
||||
}
|
||||
|
||||
return $locale;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -110,23 +110,23 @@ class ClientView implements Renderable
|
||||
* @param Request $request
|
||||
* @param User $actor
|
||||
* @param AssetManager $assets
|
||||
* @param JsCompiler $locale
|
||||
* @param string $layout
|
||||
* @param JsCompiler $locale
|
||||
*/
|
||||
public function __construct(
|
||||
Client $apiClient,
|
||||
Request $request,
|
||||
User $actor,
|
||||
AssetManager $assets,
|
||||
JsCompiler $locale,
|
||||
$layout
|
||||
$layout,
|
||||
JsCompiler $locale = null
|
||||
) {
|
||||
$this->apiClient = $apiClient;
|
||||
$this->request = $request;
|
||||
$this->actor = $actor;
|
||||
$this->assets = $assets;
|
||||
$this->locale = $locale;
|
||||
$this->layout = $layout;
|
||||
$this->locale = $locale;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -262,7 +262,11 @@ class ClientView implements Renderable
|
||||
$view->noJs = $noJs;
|
||||
|
||||
$view->styles = [$this->assets->getCssFile()];
|
||||
$view->scripts = [$this->assets->getJsFile(), $this->locale->getFile()];
|
||||
$view->scripts = [$this->assets->getJsFile()];
|
||||
|
||||
if ($this->locale) {
|
||||
$view->scripts[] = $this->locale->getFile();
|
||||
}
|
||||
|
||||
$view->head = implode("\n", $this->headStrings);
|
||||
$view->foot = implode("\n", $this->footStrings);
|
||||
|
Reference in New Issue
Block a user