Correctly replace all PHP placeholders with the parameters
Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
parent
439457d2b4
commit
5a514a9a41
|
@ -61,10 +61,6 @@ class L10NString implements \JsonSerializable {
|
||||||
$translations = $this->l10n->getTranslations();
|
$translations = $this->l10n->getTranslations();
|
||||||
$identityTranslator = $this->l10n->getIdentityTranslator();
|
$identityTranslator = $this->l10n->getIdentityTranslator();
|
||||||
|
|
||||||
$parameters = $this->parameters;
|
|
||||||
// Add $count as %count% as per \Symfony\Contracts\Translation\TranslatorInterface
|
|
||||||
$parameters['%count%'] = $this->count;
|
|
||||||
|
|
||||||
// Use the indexed version as per \Symfony\Contracts\Translation\TranslatorInterface
|
// Use the indexed version as per \Symfony\Contracts\Translation\TranslatorInterface
|
||||||
$identity = $this->text;
|
$identity = $this->text;
|
||||||
if (array_key_exists($this->text, $translations)) {
|
if (array_key_exists($this->text, $translations)) {
|
||||||
|
@ -84,7 +80,10 @@ class L10NString implements \JsonSerializable {
|
||||||
|
|
||||||
$identity = str_replace('%n', '%count%', $identity);
|
$identity = str_replace('%n', '%count%', $identity);
|
||||||
|
|
||||||
return $identityTranslator->trans($identity, $parameters);
|
// $count as %count% as per \Symfony\Contracts\Translation\TranslatorInterface
|
||||||
|
$text = $identityTranslator->trans($identity, ['%count%' => $this->count]);
|
||||||
|
|
||||||
|
return vsprintf($text, $this->parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
{
|
{
|
||||||
"translations" : {
|
"translations" : {
|
||||||
"_%n file_::_%n files_": ["%n Datei", "%n Dateien"]
|
"_%n file_::_%n files_": ["%n Datei", "%n Dateien"],
|
||||||
|
"Ordered placeholders one %s two %s": "Placeholder one %s two %s",
|
||||||
|
"Reordered placeholders one %s two %s": "Placeholder two %2$s one %1$s",
|
||||||
|
"Reordered placeholders one %1$s two %2$s": "Placeholder two %2$s one %1$s"
|
||||||
},
|
},
|
||||||
"pluralForm" : "nplurals=2; plural=(n != 1);"
|
"pluralForm" : "nplurals=2; plural=(n != 1);"
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,6 +76,27 @@ class L10nTest extends TestCase {
|
||||||
$this->assertEquals('5 oken', (string)$l->n('%n window', '%n windows', 5));
|
$this->assertEquals('5 oken', (string)$l->n('%n window', '%n windows', 5));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function dataPlaceholders(): array {
|
||||||
|
return [
|
||||||
|
['Ordered placeholders one %s two %s', 'Placeholder one 1 two 2'],
|
||||||
|
['Reordered placeholders one %s two %s', 'Placeholder two 2 one 1'],
|
||||||
|
['Reordered placeholders one %1$s two %2$s', 'Placeholder two 2 one 1'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider dataPlaceholders
|
||||||
|
*
|
||||||
|
* @param $string
|
||||||
|
* @param $expected
|
||||||
|
*/
|
||||||
|
public function testPlaceholders($string, $expected): void {
|
||||||
|
$transFile = \OC::$SERVERROOT.'/tests/data/l10n/de.json';
|
||||||
|
$l = new L10N($this->getFactory(), 'test', 'de', 'de_AT', [$transFile]);
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $l->t($string, ['1', '2']));
|
||||||
|
}
|
||||||
|
|
||||||
public function localizationData() {
|
public function localizationData() {
|
||||||
return [
|
return [
|
||||||
// timestamp as string
|
// timestamp as string
|
||||||
|
|
Loading…
Reference in New Issue