[Translation] Allow use of UTF-8 encoded catalogues into non-UTF-8 applications
This commit is contained in:
parent
deb6dea76d
commit
5473d3b6c9
@ -35,6 +35,7 @@
|
|||||||
<argument type="collection">
|
<argument type="collection">
|
||||||
<argument key="cache_dir">%kernel.cache_dir%/translations</argument>
|
<argument key="cache_dir">%kernel.cache_dir%/translations</argument>
|
||||||
<argument key="debug">%kernel.debug%</argument>
|
<argument key="debug">%kernel.debug%</argument>
|
||||||
|
<argument key="charset">%kernel.charset%</argument>
|
||||||
</argument>
|
</argument>
|
||||||
<argument type="service" id="session" on-invalid="ignore" />
|
<argument type="service" id="session" on-invalid="ignore" />
|
||||||
</service>
|
</service>
|
||||||
|
@ -46,8 +46,6 @@ class Translator extends BaseTranslator
|
|||||||
*/
|
*/
|
||||||
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array(), Session $session = null)
|
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array(), Session $session = null)
|
||||||
{
|
{
|
||||||
parent::__construct(null, $selector);
|
|
||||||
|
|
||||||
$this->session = $session;
|
$this->session = $session;
|
||||||
$this->container = $container;
|
$this->container = $container;
|
||||||
$this->loaderIds = $loaderIds;
|
$this->loaderIds = $loaderIds;
|
||||||
@ -55,6 +53,7 @@ class Translator extends BaseTranslator
|
|||||||
$this->options = array(
|
$this->options = array(
|
||||||
'cache_dir' => null,
|
'cache_dir' => null,
|
||||||
'debug' => false,
|
'debug' => false,
|
||||||
|
'charset' => null,
|
||||||
);
|
);
|
||||||
|
|
||||||
// check option names
|
// check option names
|
||||||
@ -63,6 +62,11 @@ class Translator extends BaseTranslator
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->options = array_merge($this->options, $options);
|
$this->options = array_merge($this->options, $options);
|
||||||
|
|
||||||
|
if ($this->options['charset'] === 'UTF-8') {
|
||||||
|
$this->options['charset'] = null;
|
||||||
|
}
|
||||||
|
parent::__construct(null, $selector, $this->options['charset']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -28,16 +28,18 @@ class Translator implements TranslatorInterface
|
|||||||
private $loaders;
|
private $loaders;
|
||||||
private $resources;
|
private $resources;
|
||||||
private $selector;
|
private $selector;
|
||||||
|
private $charset;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param string $locale The locale
|
* @param string $locale The locale
|
||||||
* @param MessageSelector $selector The message selector for pluralization
|
* @param MessageSelector $selector The message selector for pluralization
|
||||||
|
* @param string $charset Application charset
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
public function __construct($locale, MessageSelector $selector)
|
public function __construct($locale, MessageSelector $selector, $charset = null)
|
||||||
{
|
{
|
||||||
$this->locale = $locale;
|
$this->locale = $locale;
|
||||||
$this->selector = $selector;
|
$this->selector = $selector;
|
||||||
@ -45,6 +47,7 @@ class Translator implements TranslatorInterface
|
|||||||
$this->resources = array();
|
$this->resources = array();
|
||||||
$this->catalogues = array();
|
$this->catalogues = array();
|
||||||
$this->fallbackLocales = array();
|
$this->fallbackLocales = array();
|
||||||
|
$this->charset = $charset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -173,7 +176,18 @@ class Translator implements TranslatorInterface
|
|||||||
if (!isset($this->loaders[$resource[0]])) {
|
if (!isset($this->loaders[$resource[0]])) {
|
||||||
throw new \RuntimeException(sprintf('The "%s" translation loader is not registered.', $resource[0]));
|
throw new \RuntimeException(sprintf('The "%s" translation loader is not registered.', $resource[0]));
|
||||||
}
|
}
|
||||||
$this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2]));
|
$catalogue = $this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2]);
|
||||||
|
if (null !== $this->charset && extension_loaded('mbstring')) {
|
||||||
|
foreach ($catalogue->all() as $domain => $messages) {
|
||||||
|
foreach ($messages as $key => $translation) {
|
||||||
|
$srcCharset = mb_detect_encoding($translation);
|
||||||
|
if ($srcCharset !== $this->charset) {
|
||||||
|
$catalogue->set($key, mb_convert_encoding($translation, $this->charset, $srcCharset), $domain);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->catalogues[$locale]->addCatalogue($catalogue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user