1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
<?php
namespace Gettext\Tests;
use Gettext\Languages\Language;
use Gettext\Translations;
use Gettext\Translation;
use Gettext\Translator;
class TranslatorTest extends AbstractTestCase
{
public function testNoop()
{
$t = new Translator();
$original = 'original string';
$this->assertEquals($original, $t->noop($original));
}
public function testOne()
{
$t = new Translator();
$t->loadTranslations(static::get('po/Po'));
$t->loadTranslations(static::get('po2/Po'));
$this->assertEquals('test', $t->gettext('single'));
$this->assertEquals('test', $t->dgettext('', 'single'));
$this->assertEquals('Cijeo broj', $t->dgettext('testingdomain', 'Integer'));
$t->defaultDomain('testingdomain');
$this->assertEquals('Ovo polje ne može biti prazno.', $t->gettext('This field cannot be blank.'));
$this->assertEquals('Value %sr is not a valid choice.', $t->gettext('Value %sr is not a valid choice.'));
}
public function testOneFunction()
{
$t = new Translator();
$t->loadTranslations(static::get('po2/Po'));
$t->register();
$this->assertEquals('Cijeo broj', __('Integer'));
$this->assertEquals('Ovo polje ne može biti prazno.', __('This field cannot be blank.'));
$this->assertEquals('Value %sr is not a valid choice.', __('Value %sr is not a valid choice.'));
$this->assertEquals('Value hellor is not a valid choice.', __('Value %sr is not a valid choice.', 'hello'));
$this->assertEquals('Value 0r is not a valid choice.', __('Value %sr is not a valid choice.', 0));
$this->assertEquals('Value r is not a valid choice.', __('Value %sr is not a valid choice.', null));
$this->assertEquals('1s mora da bude jedinstven za 2s 3s.', __('%ss must be unique for %ss %ss.', '1', '2', '3'));
$this->assertEquals('Value hellor is not a valid choice.', __('Value %sr is not a valid choice.', ['%s' => 'hello']));
}
public function testPlural()
{
$t = new Translator();
$t->loadTranslations(static::get('po/Po'));
// Test that nplural=3 plural translation check comes up with the correct translation key.
$this->assertEquals('1 plik', $t->ngettext('one file', 'multiple files', 1));
$this->assertEquals('2,3,4 pliki', $t->ngettext('one file', 'multiple files', 2));
$this->assertEquals('2,3,4 pliki', $t->ngettext('one file', 'multiple files', 3));
$this->assertEquals('2,3,4 pliki', $t->ngettext('one file', 'multiple files', 4));
$this->assertEquals('5-21 plików', $t->ngettext('one file', 'multiple files', 5));
$this->assertEquals('5-21 plików', $t->ngettext('one file', 'multiple files', 6));
// Test that non-plural translations the fallback still works.
$this->assertEquals('more', $t->ngettext('single', 'more', 3));
$t = new Translator();
$t->loadTranslations(static::get('po2/Po'));
// Test that if the translation is unknown, English plural rules are applied
$this->assertEquals('more', $t->ngettext('single', 'more', 21));
}
public function testPluralFunction()
{
$translations = new Translations();
$translations[] =
(new Translation(null, 'One comment', '%s comments'))
->setTranslation('Un commentaire')
->setPluralTranslations(['%s commentaires']);
$t = new Translator();
$t->loadTranslations($translations);
$t->register();
$this->assertEquals('%s commentaires', n__('One comment', '%s comments', 3));
$this->assertEquals('beaucoup de commentaires', n__('One comment', '%s comments', 3, 'beaucoup de'));
$this->assertEquals('0 commentaires', n__('One comment', '%s comments', 3, 0));
$this->assertEquals(' commentaires', n__('One comment', '%s comments', 3, null));
$this->assertEquals('beaucoup de commentaires', n__('One comment', '%s comments', 3, ['%s' => 'beaucoup de']));
}
public function testPluralInjection()
{
$this->expectException('InvalidArgumentException');
$translations = new Translations();
$translations->setPluralForms(2, 'fuu_call()');
}
public function testPluralFromValidation()
{
$translations = new Translations();
$languages = Language::getAll();
foreach ($languages as $language) {
$result = $translations->setPluralForms(2, $language->formula);
$this->assertInstanceOf('Gettext\Translations', $result);
}
}
public function testContextFunction()
{
$translations = new Translations();
$translations[] =
(new Translation('daytime', 'Hello %s'))
->setTranslation('Bonjour %s');
$translations[] =
(new Translation('nightime', 'Hello %s'))
->setTranslation('Bonsoir %s');
$t = new Translator();
$t->loadTranslations($translations);
$t->register();
$this->assertEquals('Bonjour %s', p__('daytime','Hello %s'));
$this->assertEquals('Bonjour John', p__('daytime','Hello %s', 'John'));
$this->assertEquals('Bonjour 0', p__('daytime','Hello %s', 0));
$this->assertEquals('Bonjour ', p__('daytime','Hello %s', null));
$this->assertEquals('Bonjour John', p__('daytime','Hello %s',['%s' => 'John']));
$this->assertEquals('Bonsoir John', p__('nightime','Hello %s', 'John'));
$this->assertEquals('Bonsoir John', p__('nightime','Hello %s',['%s' => 'John']));
}
public function testDomainFunction()
{
$translations = new Translations();
$translations->setDomain('messages');
$translations[] =
(new Translation(null, 'Hello %s'))
->setTranslation('Bonjour %s');
$t = new Translator();
$t->loadTranslations($translations);
$t->register();
$this->assertEquals('Bonjour %s', d__('messages','Hello %s'));
$this->assertEquals('Bonjour John', d__('messages','Hello %s','John'));
$this->assertEquals('Bonjour 0', d__('messages','Hello %s',0));
$this->assertEquals('Bonjour ', d__('messages','Hello %s',null));
$this->assertEquals('Bonjour John', d__('messages','Hello %s',['%s' => 'John']));
$this->assertEquals('Hello %s', d__('errors','Hello %s'));
$this->assertEquals('Hello John', d__('errors','Hello %s',['%s' => 'John']));
}
public function testDomainPluralFunction()
{
$translations = new Translations();
$translations->setDomain('messages');
$translations[] =
(new Translation(null, 'One comment', '%s comments'))
->setTranslation('Un commentaire')
->setPluralTranslations(['%s commentaires']);
$t = new Translator();
$t->loadTranslations($translations);
$t->register();
$this->assertEquals('%s commentaires', dn__('messages', 'One comment', '%s comments', 3));
$this->assertEquals('beaucoup de commentaires', dn__('messages', 'One comment', '%s comments', 3, 'beaucoup de'));
$this->assertEquals('0 commentaires', dn__('messages', 'One comment', '%s comments', 3, 0));
$this->assertEquals(' commentaires', dn__('messages', 'One comment', '%s comments', 3, null));
$this->assertEquals('beaucoup de commentaires', dn__('messages', 'One comment', '%s comments', 3, ['%s' => 'beaucoup de']));
$this->assertEquals('One comment', dn__('messages-2', 'One comment', '%s comments', 1, 1));
$this->assertEquals('3 comments', dn__('messages-2', 'One comment', '%s comments', 3, 3));
}
public function testDomainAndContextFunction()
{
$translations = new Translations();
$translations->setDomain('messages');
$translations[] =
(new Translation('daytime', 'Hello %s'))
->setTranslation('Bonjour %s');
$translations[] =
(new Translation('nightime', 'Hello %s'))
->setTranslation('Bonsoir %s');
$t = new Translator();
$t->loadTranslations($translations);
$t->register();
$this->assertEquals('Bonjour %s', dp__('messages','daytime','Hello %s'));
$this->assertEquals('Bonjour John', dp__('messages','daytime','Hello %s', 'John'));
$this->assertEquals('Bonjour 0', dp__('messages','daytime','Hello %s', 0));
$this->assertEquals('Bonjour ', dp__('messages','daytime','Hello %s', null));
$this->assertEquals('Bonjour John', dp__('messages','daytime','Hello %s',['%s' => 'John']));
$this->assertEquals('Bonsoir John', dp__('messages','nightime','Hello %s', 'John'));
$this->assertEquals('Bonsoir John', dp__('messages','nightime','Hello %s',['%s' => 'John']));
$this->assertEquals('Hello John', dp__('errors','daytime','Hello %s',['%s' => 'John']));
}
public function testPluralAndContextFunction()
{
$translations = new Translations();
$translations[] =
(new Translation('comment', 'One comment', '%s comments'))
->setTranslation('Un commentaire')
->setPluralTranslations(['%s commentaires']);
$t = new Translator();
$t->loadTranslations($translations);
$t->register();
$this->assertEquals('%s commentaires', np__('comment', 'One comment', '%s comments', 3));
$this->assertEquals('0 commentaires', np__('comment', 'One comment', '%s comments', 3, 0));
$this->assertEquals(' commentaires', np__('comment', 'One comment', '%s comments', 3, null));
$this->assertEquals('beaucoup de commentaires', np__('comment', 'One comment', '%s comments', 3, 'beaucoup de'));
$this->assertEquals('beaucoup de commentaires', np__('comment', 'One comment', '%s comments', 3, ['%s' => 'beaucoup de']));
$this->assertEquals('3 comments', np__(null, 'One comment', '%s comments', 3, ['%s' => 3]));
}
public function testPluralAndContextAndDomainFunction()
{
$translations = new Translations();
$translations->setDomain('messages');
$translations[] =
(new Translation('comment', 'One comment', '%s comments'))
->setTranslation('Un commentaire')
->setPluralTranslations(['%s commentaires']);
$t = new Translator();
$t->loadTranslations($translations);
$t->register();
$this->assertEquals('%s commentaires', dnp__('messages', 'comment', 'One comment', '%s comments', 3));
$this->assertEquals('0 commentaires', dnp__('messages', 'comment', 'One comment', '%s comments', 3, 0));
$this->assertEquals(' commentaires', dnp__('messages', 'comment', 'One comment', '%s comments', 3, null));
$this->assertEquals('beaucoup de commentaires', dnp__('messages', 'comment', 'One comment', '%s comments', 3, 'beaucoup de'));
$this->assertEquals('beaucoup de commentaires', dnp__('messages', 'comment', 'One comment', '%s comments', 3, ['%s' => 'beaucoup de']));
$this->assertEquals('beaucoup de comments', dnp__('errors', 'comment', 'One comment', '%s comments', 3, ['%s' => 'beaucoup de']));
$this->assertEquals('beaucoup de comments', dnp__('messages', null, 'One comment', '%s comments', 3, ['%s' => 'beaucoup de']));
}
public function testNonLoadedTranslations()
{
$t = new Translator();
$this->assertEquals('hello', $t->gettext('hello'));
$this->assertEquals('worlds', $t->ngettext('world', 'worlds', 0));
$this->assertEquals('world', $t->ngettext('world', 'worlds', 1));
$this->assertEquals('worlds', $t->ngettext('world', 'worlds', 2));
}
public function testHeaders()
{
$po = (new Translator())->loadTranslations(static::get('po/Po'));
$mo = (new Translator())->loadTranslations(static::get('po/Mo'));
$array = (new Translator())->loadTranslations(static::get('po/PhpArray'));
$this->assertEmpty($po->gettext(''));
}
}
|