X Tutup
Skip to content

Commit 8d7670e

Browse files
janjan
authored andcommitted
adapted solution for 5xx error on i10n vsprintf
1 parent b793163 commit 8d7670e

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

changelog/unreleased/38103

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Bugfix: Prevent server error when loading invalid/corrupt translations
2+
3+
This fix prevents server errors when loading invalid or corrupt translations
4+
from Transifex. This is critical as every user is able to contribute to
5+
the ownCloud translations.
6+
7+
https://github.com/owncloud/core/issues/37799
8+
https://github.com/owncloud/core/pull/38103

lib/private/legacy/l10n/string.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ public function __toString() {
6666

6767
// Replace %n first (won't interfere with vsprintf)
6868
$text = \str_replace('%n', $this->count, $text);
69-
return \vsprintf($text, $this->parameters);
69+
$text = @\vsprintf($text, $this->parameters);
70+
71+
// If vsprintf fails, return untranslated string
72+
return $text === false ? $this->text : $text;
7073
}
7174

7275
public function jsonSerialize() {

tests/lib/L10N/L10nTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public function testGermanPluralTranslations() {
4646
$this->assertEquals('2 Dateien', (string) $l->n('%n file', '%n files', 2));
4747
}
4848

49+
public function testMalformedTranslations() {
50+
$lMock = $this->createMock('OC\L10N\L10N');
51+
$lMock->method('getTranslations')->willReturn(['malformed' => 'malformed %']);
52+
53+
$lString = new \OC_L10N_String($lMock, "malformed", []);
54+
self::assertEquals('malformed', $lString->__toString());
55+
}
56+
4957
public function testRussianPluralTranslations() {
5058
$transFile = \OC::$SERVERROOT.'/tests/data/l10n/ru.json';
5159
$l = new L10N($this->getFactory(), 'test', 'ru', [$transFile]);

0 commit comments

Comments
 (0)
X Tutup