mirror of
https://github.com/flarum/core.git
synced 2025-08-14 20:34:10 +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)) {
|
||||
|
Reference in New Issue
Block a user