123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- namespace Test\L10N;
- use DateTime;
- use OC\L10N\Factory;
- use OC\L10N\L10N;
- use OCP\App\IAppManager;
- use OCP\ICacheFactory;
- use OCP\IConfig;
- use OCP\IRequest;
- use OCP\IUserSession;
- use OCP\L10N\IFactory;
- use Test\TestCase;
- class L10nTest extends TestCase {
-
- protected function getFactory() {
-
- $config = $this->createMock(IConfig::class);
-
- $request = $this->createMock(IRequest::class);
-
- $userSession = $this->createMock(IUserSession::class);
- $cacheFactory = $this->createMock(ICacheFactory::class);
- $appManager = $this->createMock(IAppManager::class);
- return new Factory($config, $request, $userSession, $cacheFactory, \OC::$SERVERROOT, $appManager);
- }
- public function testSimpleTranslationWithTrailingColon(): void {
- $transFile = \OC::$SERVERROOT . '/tests/data/l10n/de.json';
- $l = new L10N($this->getFactory(), 'test', 'de', 'de_AT', [$transFile]);
- $this->assertEquals('Files:', $l->t('Files:'));
- }
- public function testGermanPluralTranslations(): void {
- $transFile = \OC::$SERVERROOT . '/tests/data/l10n/de.json';
- $l = new L10N($this->getFactory(), 'test', 'de', 'de_AT', [$transFile]);
- $this->assertEquals('1 Datei', (string)$l->n('%n file', '%n files', 1));
- $this->assertEquals('2 Dateien', (string)$l->n('%n file', '%n files', 2));
- }
- public function testRussianPluralTranslations(): void {
- $transFile = \OC::$SERVERROOT . '/tests/data/l10n/ru.json';
- $l = new L10N($this->getFactory(), 'test', 'ru', 'ru_UA', [$transFile]);
- $this->assertEquals('1 файл', (string)$l->n('%n file', '%n files', 1));
- $this->assertEquals('2 файла', (string)$l->n('%n file', '%n files', 2));
- $this->assertEquals('6 файлов', (string)$l->n('%n file', '%n files', 6));
- $this->assertEquals('21 файл', (string)$l->n('%n file', '%n files', 21));
- $this->assertEquals('22 файла', (string)$l->n('%n file', '%n files', 22));
- $this->assertEquals('26 файлов', (string)$l->n('%n file', '%n files', 26));
-
- }
- public function testCzechPluralTranslations(): void {
- $transFile = \OC::$SERVERROOT . '/tests/data/l10n/cs.json';
- $l = new L10N($this->getFactory(), 'test', 'cs', 'cs_CZ', [$transFile]);
- $this->assertEquals('1 okno', (string)$l->n('%n window', '%n windows', 1));
- $this->assertEquals('2 okna', (string)$l->n('%n window', '%n windows', 2));
- $this->assertEquals('5 oken', (string)$l->n('%n window', '%n windows', 5));
- }
- public function testGermanPluralWithCzechLocaleTranslations(): void {
- $transFile = \OC::$SERVERROOT . '/tests/data/l10n/de.json';
- $l = new L10N($this->getFactory(), 'test', 'de', 'cs_CZ', [$transFile]);
- $this->assertEquals('1 Datei', (string)$l->n('%n file', '%n files', 1));
- $this->assertEquals('2 Dateien', (string)$l->n('%n file', '%n files', 2));
- $this->assertEquals('5 Dateien', (string)$l->n('%n file', '%n files', 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'],
- ];
- }
-
- 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() {
- return [
-
- ["February 13, 2009, 11:31:30\xE2\x80\xAFPM UTC", 'en', 'en_US', 'datetime', '1234567890'],
- ['13. Februar 2009, 23:31:30 UTC', 'de', 'de_DE', 'datetime', '1234567890'],
- ['February 13, 2009', 'en', 'en_US', 'date', '1234567890'],
- ['13. Februar 2009', 'de', 'de_DE', 'date', '1234567890'],
- ["11:31:30\xE2\x80\xAFPM UTC", 'en', 'en_US', 'time', '1234567890'],
- ['23:31:30 UTC', 'de', 'de_DE', 'time', '1234567890'],
-
- ["February 13, 2009, 11:31:30\xE2\x80\xAFPM UTC", 'en', 'en_US', 'datetime', 1234567890],
- ['13. Februar 2009, 23:31:30 UTC', 'de', 'de_DE', 'datetime', 1234567890],
- ['February 13, 2009', 'en', 'en_US', 'date', 1234567890],
- ['13. Februar 2009', 'de', 'de_DE', 'date', 1234567890],
- ["11:31:30\xE2\x80\xAFPM UTC", 'en', 'en_US', 'time', 1234567890],
- ['23:31:30 UTC', 'de', 'de_DE', 'time', 1234567890],
-
- ["February 13, 2009, 11:31:30\xE2\x80\xAFPM GMT+0", 'en', 'en_US', 'datetime', new DateTime('@1234567890')],
- ['13. Februar 2009, 23:31:30 GMT+0', 'de', 'de_DE', 'datetime', new DateTime('@1234567890')],
- ['February 13, 2009', 'en', 'en_US', 'date', new DateTime('@1234567890')],
- ['13. Februar 2009', 'de', 'de_DE', 'date', new DateTime('@1234567890')],
- ["11:31:30\xE2\x80\xAFPM GMT+0", 'en', 'en_US', 'time', new DateTime('@1234567890')],
- ['23:31:30 GMT+0', 'de', 'de_DE', 'time', new DateTime('@1234567890')],
-
- ['13 February 2009, 23:31:30 GMT+0', 'en_GB', 'en_GB', 'datetime', new DateTime('@1234567890')],
- ['13 February 2009', 'en_GB', 'en_GB', 'date', new DateTime('@1234567890')],
- ['23:31:30 GMT+0', 'en_GB', 'en_GB', 'time', new DateTime('@1234567890')],
- ['13 February 2009, 23:31:30 GMT+0', 'en-GB', 'en_GB', 'datetime', new DateTime('@1234567890')],
- ['13 February 2009', 'en-GB', 'en_GB', 'date', new DateTime('@1234567890')],
- ['23:31:30 GMT+0', 'en-GB', 'en_GB', 'time', new DateTime('@1234567890')],
- ];
- }
-
- public function testNumericStringLocalization($expectedDate, $lang, $locale, $type, $value): void {
- $l = new L10N($this->getFactory(), 'test', $lang, $locale, []);
- $this->assertSame($expectedDate, $l->l($type, $value));
- }
- public function firstDayData() {
- return [
- [1, 'de', 'de_DE'],
- [0, 'en', 'en_US'],
- ];
- }
-
- public function testFirstWeekDay($expected, $lang, $locale): void {
- $l = new L10N($this->getFactory(), 'test', $lang, $locale, []);
- $this->assertSame($expected, $l->l('firstday', 'firstday'));
- }
- public function jsDateData() {
- return [
- ['dd.MM.yy', 'de', 'de_DE'],
- ['M/d/yy', 'en', 'en_US'],
- ];
- }
-
- public function testJSDate($expected, $lang, $locale): void {
- $l = new L10N($this->getFactory(), 'test', $lang, $locale, []);
- $this->assertSame($expected, $l->l('jsdate', 'jsdate'));
- }
- public function testFactoryGetLanguageCode(): void {
- $l = $this->getFactory()->get('lib', 'de');
- $this->assertEquals('de', $l->getLanguageCode());
- }
- public function testServiceGetLanguageCode(): void {
- $l = \OCP\Util::getL10N('lib', 'de');
- $this->assertEquals('de', $l->getLanguageCode());
- }
- public function testWeekdayName(): void {
- $l = \OCP\Util::getL10N('lib', 'de');
- $this->assertEquals('Mo.', $l->l('weekdayName', new \DateTime('2017-11-6'), ['width' => 'abbreviated']));
- }
-
- public function testFindLanguageFromLocale($locale, $language): void {
- $this->assertEquals(
- $language,
- \OC::$server->get(IFactory::class)->findLanguageFromLocale('lib', $locale)
- );
- }
-
- public function findLanguageFromLocaleData(): array {
- return [
- 'en_US' => ['en_US', 'en'],
- 'en_UK' => ['en_UK', 'en'],
- 'de_DE' => ['de_DE', 'de_DE'],
- 'de_AT' => ['de_AT', 'de'],
- 'es_EC' => ['es_EC', 'es_EC'],
- 'fi_FI' => ['fi_FI', 'fi'],
- 'zh_CN' => ['zh_CN', 'zh_CN'],
- ];
- }
- }
|