diff options
author | ArthurHoaro <arthur@hoa.ro> | 2016-08-07 11:52:49 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2016-08-07 11:54:39 +0200 |
commit | edf3ff5a53b353ed4a5d9d617bfd06a6c13b3bac (patch) | |
tree | 04bdab4db1ef5e8c68ea161e198bf2aef7ae9396 | |
parent | 65b2c795d00f638d6ca08519e1435efd989c8117 (diff) | |
download | Shaarli-edf3ff5a53b353ed4a5d9d617bfd06a6c13b3bac.tar.gz Shaarli-edf3ff5a53b353ed4a5d9d617bfd06a6c13b3bac.tar.zst Shaarli-edf3ff5a53b353ed4a5d9d617bfd06a6c13b3bac.zip |
Initialize a translation function
It matches the API of ngettext().
-rw-r--r-- | application/Languages.php | 21 | ||||
-rw-r--r-- | index.php | 1 | ||||
-rw-r--r-- | tests/LanguagesTest.php | 41 |
3 files changed, 63 insertions, 0 deletions
diff --git a/application/Languages.php b/application/Languages.php new file mode 100644 index 00000000..c8b0a25a --- /dev/null +++ b/application/Languages.php | |||
@@ -0,0 +1,21 @@ | |||
1 | <?php | ||
2 | |||
3 | /** | ||
4 | * Wrapper function for translation which match the API | ||
5 | * of gettext()/_() and ngettext(). | ||
6 | * | ||
7 | * Not doing translation for now. | ||
8 | * | ||
9 | * @param string $text Text to translate. | ||
10 | * @param string $nText The plural message ID. | ||
11 | * @param int $nb The number of items for plural forms. | ||
12 | * | ||
13 | * @return String Text translated. | ||
14 | */ | ||
15 | function t($text, $nText = '', $nb = 0) { | ||
16 | if (empty($nText)) { | ||
17 | return $text; | ||
18 | } | ||
19 | $actualForm = $nb > 1 ? $nText : $text; | ||
20 | return sprintf($actualForm, $nb); | ||
21 | } | ||
@@ -53,6 +53,7 @@ require_once 'application/config/ConfigPlugin.php'; | |||
53 | require_once 'application/FeedBuilder.php'; | 53 | require_once 'application/FeedBuilder.php'; |
54 | require_once 'application/FileUtils.php'; | 54 | require_once 'application/FileUtils.php'; |
55 | require_once 'application/HttpUtils.php'; | 55 | require_once 'application/HttpUtils.php'; |
56 | require_once 'application/Languages.php'; | ||
56 | require_once 'application/LinkDB.php'; | 57 | require_once 'application/LinkDB.php'; |
57 | require_once 'application/LinkFilter.php'; | 58 | require_once 'application/LinkFilter.php'; |
58 | require_once 'application/LinkUtils.php'; | 59 | require_once 'application/LinkUtils.php'; |
diff --git a/tests/LanguagesTest.php b/tests/LanguagesTest.php new file mode 100644 index 00000000..79c136c8 --- /dev/null +++ b/tests/LanguagesTest.php | |||
@@ -0,0 +1,41 @@ | |||
1 | <?php | ||
2 | |||
3 | require_once 'application/Languages.php'; | ||
4 | |||
5 | /** | ||
6 | * Class LanguagesTest. | ||
7 | */ | ||
8 | class LanguagesTest extends PHPUnit_Framework_TestCase | ||
9 | { | ||
10 | /** | ||
11 | * Test t() with a simple non identified value. | ||
12 | */ | ||
13 | public function testTranslateSingleNotID() | ||
14 | { | ||
15 | $text = 'abcdé 564 fgK'; | ||
16 | $this->assertEquals($text, t($text)); | ||
17 | } | ||
18 | |||
19 | /** | ||
20 | * Test t() with a non identified plural form. | ||
21 | */ | ||
22 | public function testTranslatePluralNotID() | ||
23 | { | ||
24 | $text = '%s sandwich'; | ||
25 | $nText = '%s sandwiches'; | ||
26 | $this->assertEquals('0 sandwich', t($text, $nText)); | ||
27 | $this->assertEquals('1 sandwich', t($text, $nText, 1)); | ||
28 | $this->assertEquals('2 sandwiches', t($text, $nText, 2)); | ||
29 | } | ||
30 | |||
31 | /** | ||
32 | * Test t() with a non identified invalid plural form. | ||
33 | */ | ||
34 | public function testTranslatePluralNotIDInvalid() | ||
35 | { | ||
36 | $text = 'sandwich'; | ||
37 | $nText = 'sandwiches'; | ||
38 | $this->assertEquals('sandwich', t($text, $nText, 1)); | ||
39 | $this->assertEquals('sandwiches', t($text, $nText, 2)); | ||
40 | } | ||
41 | } | ||