mirror of
https://github.com/flarum/framework.git
synced 2025-02-22 13:08:40 +08:00
Decouple from Laravel, implement translator
This commit is contained in:
parent
8e860d452e
commit
8c9089d718
@ -9,7 +9,19 @@
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.4.0",
|
||||
"illuminate/bus": "5.1.*",
|
||||
"illuminate/cache": "5.1.*",
|
||||
"illuminate/config": "5.1.*",
|
||||
"illuminate/container": "5.1.*",
|
||||
"illuminate/contracts": "5.1.*",
|
||||
"illuminate/database": "5.1.*",
|
||||
"illuminate/events": "5.1.*",
|
||||
"illuminate/filesystem": "5.1.*",
|
||||
"illuminate/hashing": "5.1.*",
|
||||
"illuminate/mail": "5.1.*",
|
||||
"illuminate/support": "5.1.*",
|
||||
"illuminate/validation": "5.1.*",
|
||||
"illuminate/view": "5.1.*",
|
||||
"tobscure/json-api": "^0.1.1",
|
||||
"oyejorge/less.php": "~1.5",
|
||||
"intervention/image": "^2.3.0",
|
||||
@ -20,13 +32,15 @@
|
||||
"dflydev/fig-cookies": "^1.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"fzaninotto/faker": "1.4.0",
|
||||
"squizlabs/php_codesniffer": "2.*",
|
||||
"phpspec/phpspec": "^2.2"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Flarum\\": "src/"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"src/helpers.php"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -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);
|
||||
}
|
||||
}
|
||||
|
88
framework/core/src/helpers.php
Normal file
88
framework/core/src/helpers.php
Normal file
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Container\Container;
|
||||
|
||||
if (! function_exists('app')) {
|
||||
/**
|
||||
* Get the available container instance.
|
||||
*
|
||||
* @param string $make
|
||||
* @param array $parameters
|
||||
* @return mixed|\Illuminate\Foundation\Application
|
||||
*/
|
||||
function app($make = null, $parameters = [])
|
||||
{
|
||||
if (is_null($make)) {
|
||||
return Container::getInstance();
|
||||
}
|
||||
|
||||
return Container::getInstance()->make($make, $parameters);
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('app_path')) {
|
||||
/**
|
||||
* Get the path to the application folder.
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
function app_path($path = '')
|
||||
{
|
||||
return app('path').($path ? DIRECTORY_SEPARATOR.$path : $path);
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('base_path')) {
|
||||
/**
|
||||
* Get the path to the base of the install.
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
function base_path($path = '')
|
||||
{
|
||||
return app()->basePath().($path ? DIRECTORY_SEPARATOR.$path : $path);
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('public_path')) {
|
||||
/**
|
||||
* Get the path to the public folder.
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
function public_path($path = '')
|
||||
{
|
||||
return app()->make('path.public').($path ? DIRECTORY_SEPARATOR.$path : $path);
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('storage_path')) {
|
||||
/**
|
||||
* Get the path to the storage folder.
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
function storage_path($path = '')
|
||||
{
|
||||
return app('path.storage').($path ? DIRECTORY_SEPARATOR.$path : $path);
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('event')) {
|
||||
/**
|
||||
* Fire an event and call the listeners.
|
||||
*
|
||||
* @param string|object $event
|
||||
* @param mixed $payload
|
||||
* @param bool $halt
|
||||
* @return array|null
|
||||
*/
|
||||
function event($event, $payload = [], $halt = false)
|
||||
{
|
||||
return app('events')->fire($event, $payload, $halt);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user