1
0
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:
Toby Zerner
2015-09-25 16:12:09 +09:30
parent 02e40f7c47
commit 3efbffdcec
7 changed files with 42 additions and 286 deletions

View File

@@ -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);
}
}

View File

@@ -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)) {