8000 Allow use of UTF-8 catalogues in non-UTF-8 applications by Seldaek · Pull Request #2339 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Allow use of UTF-8 catalogues in non-UTF-8 applications #2339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 7, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations 8000
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<argument type="collection">
<argument key="cache_dir">%kernel.cache_dir%/translations</argument>
<argument key="debug">%kernel.debug%</argument>
<argument key="charset">%kernel.charset%</argument>
</argument>
<argument type="service" id="session" on-invalid="ignore" />
</service>
Expand Down
8 changes: 6 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@ class Translator extends BaseTranslator
*/
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array(), Session $session = null)
{
parent::__construct(null, $selector);

$this->session = $session;
$this->container = $container;
$this->loaderIds = $loaderIds;

$this->options = array(
'cache_dir' => null,
'debug' => false,
'charset' => null,
);

// check option names
Expand All @@ -63,6 +62,11 @@ public function __construct(ContainerInterface $container, MessageSelector $sele
}

$this->options = array_merge($this->options, $options);

if ($this->options['charset'] === 'UTF-8') {
$this->options['charset'] = null;
}
parent::__construct(null, $selector, $this->options['charset']);
}

/**
Expand Down
18 changes: 16 additions & 2 deletions src/Symfony/Component/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,26 @@ class Translator implements TranslatorInterface
private $loaders;
private $resources;
private $selector;
private $charset;

/**
* Constructor.
*
* @param string $locale The locale
* @param MessageSelector $selector The message selector for pluralization
* @param string $charset Application charset
*
* @api
*/
public function __construct($locale, MessageSelector $selector)
public function __construct($locale, MessageSelector $selector, $charset = null)
{
$this->locale = $locale;
$this->selector = $selector;
$this->loaders = array();
$this->resources = array();
$this->catalogues = array();
$this->fallbackLocales = array();
$this->charset = $charset;
}

/**
Expand Down Expand Up @@ -173,7 +176,18 @@ private function doLoadCatalogue($locale)
if (!isset($this->loaders[$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);
}
}
}
Expand Down
36 changes: 36 additions & 0 deletions tests/Symfony/Tests/Component/Translation/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,27 @@ public function testFlattenedTrans($expected, $messages, $id)
$this->assertEquals($expected, $translator->trans($id, array(), '', 'fr'));
}

/**
* @dataProvider getLoadCatalogueTests
*/
public function testLoadCatalogueConvertsEncoding($translation, $charset)
{
if (!extension_loaded('mbstring')) {
$this->markTestSkipped('This test relies on the mbstring extension');
}
$translator = new Translator('en', new MessageSelector(), $charset);
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', array('id' => $translation), 'en', 'messages');

if (null !== $charset && mb_detect_encoding($translation) !== $charset) {
$expected = mb_convert_encoding($translation, $charset, mb_detect_encoding($translation));
} else {
$expected = $translation;
}

$this->assertEquals($expected, $translator->trans('id', array(), 'messages', 'en'));
}

/**
* @dataProvider getTransChoiceTests
*/
Expand Down Expand Up @@ -199,6 +220,21 @@ public function getTransChoiceTests()
);
}

public function getLoadCatalogueTests()
{
return array(
array('oia', null),
array('oia', 'UTF-8'),
array('öïä', 'UTF-8'),
array('oia', 'ISO-8859-1'),
array('öïä', 'ISO-8859-1'),
array('цфЭ', 'UTF-8'),
array('цфЭ', 'KOI8-R'),
array('ヨラリ', 'UTF-8'),
array('ヨラリ', 'SJIS'),
);
}

public function testTransChoiceFallback()
{
$translator = new Translator('ru', new MessageSelector());
Expand Down
0