mirror of
https://github.com/flarum/core.git
synced 2025-08-18 06:11:23 +02:00
Decouple from Laravel, implement translator
This commit is contained in:
@@ -27,28 +27,16 @@ class LocaleManager
|
||||
|
||||
public function addTranslations($locale, $translations)
|
||||
{
|
||||
if (! isset($this->translations[$locale])) {
|
||||
$this->translations[$locale] = [];
|
||||
}
|
||||
|
||||
$this->translations[$locale][] = $translations;
|
||||
}
|
||||
|
||||
public function addJsFile($locale, $js)
|
||||
{
|
||||
if (! isset($this->js[$locale])) {
|
||||
$this->js[$locale] = [];
|
||||
}
|
||||
|
||||
$this->js[$locale][] = $js;
|
||||
}
|
||||
|
||||
public function addConfig($locale, $config)
|
||||
{
|
||||
if (! isset($this->config[$locale])) {
|
||||
$this->config[$locale] = [];
|
||||
}
|
||||
|
||||
$this->config[$locale][] = $config;
|
||||
}
|
||||
|
||||
@@ -79,4 +67,19 @@ class LocaleManager
|
||||
|
||||
return $files;
|
||||
}
|
||||
|
||||
public function getConfig($locale)
|
||||
{
|
||||
if (empty($this->config[$locale])) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$config = [];
|
||||
|
||||
foreach ($this->config[$locale] as $file) {
|
||||
$config = array_merge($config, include $file);
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
}
|
||||
|
@@ -34,5 +34,11 @@ class LocaleServiceProvider extends ServiceProvider
|
||||
$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']);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -1,11 +1,9 @@
|
||||
<?php namespace Flarum\Locale;
|
||||
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Closure;
|
||||
|
||||
/**
|
||||
* @todo implement Symfony\Component\Translation\TranslatorInterface
|
||||
*/
|
||||
class Translator
|
||||
class Translator implements TranslatorInterface
|
||||
{
|
||||
protected $translations;
|
||||
|
||||
@@ -17,27 +15,46 @@ class Translator
|
||||
$this->plural = $plural;
|
||||
}
|
||||
|
||||
public function plural($count)
|
||||
protected function plural($count)
|
||||
{
|
||||
return {$this->plural}($count);
|
||||
$plural = $this->plural;
|
||||
|
||||
return $plural($count);
|
||||
}
|
||||
|
||||
public function translate($key, array $input = [])
|
||||
public function getLocale()
|
||||
{
|
||||
$translation = array_get($this->translations, $key);
|
||||
//
|
||||
}
|
||||
|
||||
if (is_array($translation) && isset($input['count'])) {
|
||||
$translation = $translation[$this->plural($input['count'])];
|
||||
public function setLocale($locale)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function trans($id, array $parameters = [], $domain = null, $locale = null)
|
||||
{
|
||||
$translation = array_get($this->translations, $id);
|
||||
|
||||
if (is_array($translation) && isset($parameters['count'])) {
|
||||
$translation = $translation[$this->plural($parameters['count'])];
|
||||
}
|
||||
|
||||
if (is_string($translation)) {
|
||||
foreach ($input as $k => $v) {
|
||||
foreach ($parameters as $k => $v) {
|
||||
$translation = str_replace('{'.$k.'}', $v, $translation);
|
||||
}
|
||||
|
||||
return $translation;
|
||||
} else {
|
||||
return $key;
|
||||
}
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null)
|
||||
{
|
||||
$parameters['count'] = $number;
|
||||
|
||||
return $this->trans($id, $parameters, $domain, $locale);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user