mirror of
https://github.com/flarum/core.git
synced 2025-08-17 22:01:44 +02:00
Lay the groundwork for translation & refactor asset compilation
Ditched the idea of having language packs as extensions. Reasoning: 1. Because we use machine keys for translations (rather than English keys), extensions need to be able to define default translations. If English translations are to be included in extensions and not in a language pack extension, then it doesn’t make sense to have other languages as language pack extensions. Inconsistency → complexity. 2. Translations should maintain version parity with their respective extensions. There’s no way to do this if extension translations are external to the extension. Instead, localisation will be a core effort, as well as a per-extension effort. Translators will be encouraged to send PRs to core + extensions. In core, each locale has a directory containing three files: - translations.yml - config.js: contains pluralisation logic for the JS app, as well as moment.js localisation if necessary - config.php: contains pluralisation logic for the PHP app Extensions can use the Flarum\Extend\Locale extender to add/override translations/config to a locale. Asset compilation has been completely refactored with a better architecture. Translations + config.js are compiled and cached for the currently active locale.
This commit is contained in:
24
src/Locale/JsCompiler.php
Normal file
24
src/Locale/JsCompiler.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php namespace Flarum\Locale;
|
||||
|
||||
use Flarum\Assets\RevisionCompiler;
|
||||
|
||||
class JsCompiler extends RevisionCompiler
|
||||
{
|
||||
protected $translations = [];
|
||||
|
||||
public function setTranslations(array $translations)
|
||||
{
|
||||
$this->translations = $translations;
|
||||
}
|
||||
|
||||
public function compile()
|
||||
{
|
||||
$output = "var app = require('flarum/app')['default']; app.translator.translations = ".json_encode($this->translations).";";
|
||||
|
||||
foreach ($this->files as $filename) {
|
||||
$output .= file_get_contents($filename);
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
65
src/Locale/LocaleManager.php
Normal file
65
src/Locale/LocaleManager.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php namespace Flarum\Locale;
|
||||
|
||||
class LocaleManager
|
||||
{
|
||||
protected $translations = [];
|
||||
|
||||
protected $js = [];
|
||||
|
||||
protected $config = [];
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public function getTranslations($locale)
|
||||
{
|
||||
$files = array_get($this->translations, $locale, []);
|
||||
|
||||
$parts = explode('-', $locale);
|
||||
|
||||
if (count($parts) > 1) {
|
||||
$files = array_merge(array_get($this->translations, $parts[0], []), $files);
|
||||
}
|
||||
|
||||
$compiler = new TranslationCompiler($locale, $files);
|
||||
|
||||
return $compiler->getTranslations();
|
||||
}
|
||||
|
||||
public function getJsFiles($locale)
|
||||
{
|
||||
$files = array_get($this->js, $locale, []);
|
||||
|
||||
$parts = explode('-', $locale);
|
||||
|
||||
if (count($parts) > 1) {
|
||||
$files = array_merge(array_get($this->js, $parts[0], []), $files);
|
||||
}
|
||||
|
||||
return $files;
|
||||
}
|
||||
}
|
29
src/Locale/TranslationCompiler.php
Normal file
29
src/Locale/TranslationCompiler.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php namespace Flarum\Locale;
|
||||
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
class TranslationCompiler
|
||||
{
|
||||
protected $locale;
|
||||
|
||||
protected $filenames;
|
||||
|
||||
public function __construct($locale, array $filenames)
|
||||
{
|
||||
$this->locale = $locale;
|
||||
$this->filenames = $filenames;
|
||||
}
|
||||
|
||||
public function getTranslations()
|
||||
{
|
||||
// @todo caching
|
||||
|
||||
$translations = [];
|
||||
|
||||
foreach ($this->filenames as $filename) {
|
||||
$translations = array_replace_recursive($translations, Yaml::parse(file_get_contents($filename)));
|
||||
}
|
||||
|
||||
return $translations;
|
||||
}
|
||||
}
|
42
src/Locale/Translator.php
Normal file
42
src/Locale/Translator.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php namespace Flarum\Locale;
|
||||
|
||||
use Closure;
|
||||
|
||||
class Translator
|
||||
{
|
||||
protected $translations;
|
||||
|
||||
protected $plural;
|
||||
|
||||
public function __construct(array $translations, Closure $plural)
|
||||
{
|
||||
$this->translations = $translations;
|
||||
$this->plural = $plural;
|
||||
}
|
||||
|
||||
public function plural($count)
|
||||
{
|
||||
$callback = $this->plural;
|
||||
|
||||
return $callback($count);
|
||||
}
|
||||
|
||||
public function translate($key, array $input = [])
|
||||
{
|
||||
$translation = array_get($this->translations, $key);
|
||||
|
||||
if (is_array($translation) && isset($input['count'])) {
|
||||
$translation = $translation[$this->plural($input['count'])];
|
||||
}
|
||||
|
||||
if (is_string($translation)) {
|
||||
foreach ($input as $k => $v) {
|
||||
$translation = str_replace('{'.$k.'}', $v, $translation);
|
||||
}
|
||||
|
||||
return $translation;
|
||||
} else {
|
||||
return $key;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user