Shaarli's translation

* translation system and unit tests
 * Translations everywhere

Dont use translation merge

It is not available with PHP builtin gettext, so it would have lead to inconsistency.
This commit is contained in:
ArthurHoaro 2017-05-09 18:12:15 +02:00
parent 72cfe44436
commit 12266213d0
51 changed files with 2252 additions and 246 deletions

View file

@ -1,41 +1,201 @@
<?php
require_once 'application/Languages.php';
namespace Shaarli;
use Shaarli\Config\ConfigManager;
/**
* Class LanguagesTest.
*/
class LanguagesTest extends PHPUnit_Framework_TestCase
class LanguagesTest extends \PHPUnit_Framework_TestCase
{
/**
* @var string Config file path (without extension).
*/
protected static $configFile = 'tests/utils/config/configJson';
/**
* @var ConfigManager
*/
protected $conf;
/**
*
*/
public function setUp()
{
$this->conf = new ConfigManager(self::$configFile);
}
/**
* Test t() with a simple non identified value.
*/
public function testTranslateSingleNotID()
public function testTranslateSingleNotIDGettext()
{
$this->conf->set('translation.mode', 'gettext');
new Languages('en', $this->conf);
$text = 'abcdé 564 fgK';
$this->assertEquals($text, t($text));
}
/**
* Test t() with a non identified plural form.
* Test t() with a simple identified value in gettext mode.
*/
public function testTranslatePluralNotID()
public function testTranslateSingleIDGettext()
{
$text = '%s sandwich';
$nText = '%s sandwiches';
$this->assertEquals('0 sandwich', t($text, $nText));
$this->assertEquals('1 sandwich', t($text, $nText, 1));
$this->assertEquals('2 sandwiches', t($text, $nText, 2));
$this->conf->set('translation.mode', 'gettext');
new Languages('en', $this->conf);
$text = 'permalink';
$this->assertEquals($text, t($text));
}
/**
* Test t() with a non identified invalid plural form.
* Test t() with a non identified plural form in gettext mode.
*/
public function testTranslatePluralNotIDInvalid()
public function testTranslatePluralNotIDGettext()
{
$this->conf->set('translation.mode', 'gettext');
new Languages('en', $this->conf);
$text = 'sandwich';
$nText = 'sandwiches';
$this->assertEquals('sandwiches', t($text, $nText, 0));
$this->assertEquals('sandwich', t($text, $nText, 1));
$this->assertEquals('sandwiches', t($text, $nText, 2));
}
/**
* Test t() with an identified plural form in gettext mode.
*/
public function testTranslatePluralIDGettext()
{
$this->conf->set('translation.mode', 'gettext');
new Languages('en', $this->conf);
$text = 'shaare';
$nText = 'shaares';
// In english, zero is followed by plural form
$this->assertEquals('shaares', t($text, $nText, 0));
$this->assertEquals('shaare', t($text, $nText, 1));
$this->assertEquals('shaares', t($text, $nText, 2));
}
/**
* Test t() with a simple non identified value.
*/
public function testTranslateSingleNotIDPhp()
{
$this->conf->set('translation.mode', 'php');
new Languages('en', $this->conf);
$text = 'abcdé 564 fgK';
$this->assertEquals($text, t($text));
}
/**
* Test t() with a simple identified value in PHP mode.
*/
public function testTranslateSingleIDPhp()
{
$this->conf->set('translation.mode', 'php');
new Languages('en', $this->conf);
$text = 'permalink';
$this->assertEquals($text, t($text));
}
/**
* Test t() with a non identified plural form in PHP mode.
*/
public function testTranslatePluralNotIDPhp()
{
$this->conf->set('translation.mode', 'php');
new Languages('en', $this->conf);
$text = 'sandwich';
$nText = 'sandwiches';
$this->assertEquals('sandwiches', t($text, $nText, 0));
$this->assertEquals('sandwich', t($text, $nText, 1));
$this->assertEquals('sandwiches', t($text, $nText, 2));
}
/**
* Test t() with an identified plural form in PHP mode.
*/
public function testTranslatePluralIDPhp()
{
$this->conf->set('translation.mode', 'php');
new Languages('en', $this->conf);
$text = 'shaare';
$nText = 'shaares';
// In english, zero is followed by plural form
$this->assertEquals('shaares', t($text, $nText, 0));
$this->assertEquals('shaare', t($text, $nText, 1));
$this->assertEquals('shaares', t($text, $nText, 2));
}
/**
* Test t() with an invalid language set in the configuration in gettext mode.
*/
public function testTranslateWithInvalidConfLanguageGettext()
{
$this->conf->set('translation.mode', 'gettext');
$this->conf->set('translation.language', 'nope');
new Languages('fr', $this->conf);
$text = 'grumble';
$this->assertEquals($text, t($text));
}
/**
* Test t() with an invalid language set in the configuration in PHP mode.
*/
public function testTranslateWithInvalidConfLanguagePhp()
{
$this->conf->set('translation.mode', 'php');
$this->conf->set('translation.language', 'nope');
new Languages('fr', $this->conf);
$text = 'grumble';
$this->assertEquals($text, t($text));
}
/**
* Test t() with an invalid language set with auto language in gettext mode.
*/
public function testTranslateWithInvalidAutoLanguageGettext()
{
$this->conf->set('translation.mode', 'gettext');
new Languages('nope', $this->conf);
$text = 'grumble';
$this->assertEquals($text, t($text));
}
/**
* Test t() with an invalid language set with auto language in PHP mode.
*/
public function testTranslateWithInvalidAutoLanguagePhp()
{
$this->conf->set('translation.mode', 'php');
new Languages('nope', $this->conf);
$text = 'grumble';
$this->assertEquals($text, t($text));
}
/**
* Test t() with an extension language file in gettext mode
*/
public function testTranslationExtensionGettext()
{
$this->conf->set('translation.mode', 'gettext');
$this->conf->set('translation.extensions.test', 'tests/utils/languages/');
new Languages('en', $this->conf);
$this->assertEquals('car', t('car', 'car', 1, 'test'));
$this->assertEquals('Search', t('Search', 'Search', 1, 'test'));
}
/**
* Test t() with an extension language file in PHP mode
*/
public function testTranslationExtensionPhp()
{
$this->conf->set('translation.mode', 'php');
$this->conf->set('translation.extensions.test', 'tests/utils/languages/');
new Languages('en', $this->conf);
$this->assertEquals('car', t('car', 'car', 1, 'test'));
$this->assertEquals('Search', t('Search', 'Search', 1, 'test'));
}
}

View file

@ -384,18 +384,18 @@ class UtilsTest extends PHPUnit_Framework_TestCase
*/
public function testHumanBytes()
{
$this->assertEquals('2kiB', human_bytes(2 * 1024));
$this->assertEquals('2kiB', human_bytes(strval(2 * 1024)));
$this->assertEquals('2MiB', human_bytes(2 * (pow(1024, 2))));
$this->assertEquals('2MiB', human_bytes(strval(2 * (pow(1024, 2)))));
$this->assertEquals('2GiB', human_bytes(2 * (pow(1024, 3))));
$this->assertEquals('2GiB', human_bytes(strval(2 * (pow(1024, 3)))));
$this->assertEquals('374B', human_bytes(374));
$this->assertEquals('374B', human_bytes('374'));
$this->assertEquals('232kiB', human_bytes(237481));
$this->assertEquals('Unlimited', human_bytes('0'));
$this->assertEquals('Unlimited', human_bytes(0));
$this->assertEquals('Setting not set', human_bytes(''));
$this->assertEquals('2'. t('kiB'), human_bytes(2 * 1024));
$this->assertEquals('2'. t('kiB'), human_bytes(strval(2 * 1024)));
$this->assertEquals('2'. t('MiB'), human_bytes(2 * (pow(1024, 2))));
$this->assertEquals('2'. t('MiB'), human_bytes(strval(2 * (pow(1024, 2)))));
$this->assertEquals('2'. t('GiB'), human_bytes(2 * (pow(1024, 3))));
$this->assertEquals('2'. t('GiB'), human_bytes(strval(2 * (pow(1024, 3)))));
$this->assertEquals('374'. t('B'), human_bytes(374));
$this->assertEquals('374'. t('B'), human_bytes('374'));
$this->assertEquals('232'. t('kiB'), human_bytes(237481));
$this->assertEquals(t('Unlimited'), human_bytes('0'));
$this->assertEquals(t('Unlimited'), human_bytes(0));
$this->assertEquals(t('Setting not set'), human_bytes(''));
}
/**
@ -403,9 +403,9 @@ class UtilsTest extends PHPUnit_Framework_TestCase
*/
public function testGetMaxUploadSize()
{
$this->assertEquals('1MiB', get_max_upload_size(2097152, '1024k'));
$this->assertEquals('1MiB', get_max_upload_size('1m', '2m'));
$this->assertEquals('100B', get_max_upload_size(100, 100));
$this->assertEquals('1'. t('MiB'), get_max_upload_size(2097152, '1024k'));
$this->assertEquals('1'. t('MiB'), get_max_upload_size('1m', '2m'));
$this->assertEquals('100'. t('B'), get_max_upload_size(100, 100));
}
/**

6
tests/bootstrap.php Normal file
View file

@ -0,0 +1,6 @@
<?php
require_once 'vendor/autoload.php';
$conf = new \Shaarli\Config\ConfigManager('tests/utils/config/configJson');
new \Shaarli\Languages('en', $conf);

View file

@ -1,7 +1,6 @@
<?php
if (! empty('UT_LOCALE')) {
require_once 'tests/bootstrap.php';
if (! empty(getenv('UT_LOCALE'))) {
setlocale(LC_ALL, getenv('UT_LOCALE'));
}
require_once 'vendor/autoload.php';

View file

@ -0,0 +1,173 @@
<?php
namespace Shaarli;
use Shaarli\Config\ConfigManager;
/**
* Class LanguagesFrTest
*
* Test the translation system in PHP and gettext mode with French language.
*
* @package Shaarli
*/
class LanguagesFrTest extends \PHPUnit_Framework_TestCase
{
/**
* @var string Config file path (without extension).
*/
protected static $configFile = 'tests/utils/config/configJson';
/**
* @var ConfigManager
*/
protected $conf;
/**
* Init: force French
*/
public function setUp()
{
$this->conf = new ConfigManager(self::$configFile);
$this->conf->set('translation.language', 'fr');
}
/**
* Reset the locale since gettext seems to mess with it, making it too long
*/
public static function tearDownAfterClass()
{
if (! empty(getenv('UT_LOCALE'))) {
setlocale(LC_ALL, getenv('UT_LOCALE'));
}
}
/**
* Test t() with a simple non identified value.
*/
public function testTranslateSingleNotIDGettext()
{
$this->conf->set('translation.mode', 'gettext');
new Languages('en', $this->conf);
$text = 'abcdé 564 fgK';
$this->assertEquals($text, t($text));
}
/**
* Test t() with a simple identified value in gettext mode.
*/
public function testTranslateSingleIDGettext()
{
$this->conf->set('translation.mode', 'gettext');
new Languages('en', $this->conf);
$text = 'permalink';
$this->assertEquals('permalien', t($text));
}
/**
* Test t() with a non identified plural form in gettext mode.
*/
public function testTranslatePluralNotIDGettext()
{
$this->conf->set('translation.mode', 'gettext');
new Languages('en', $this->conf);
$text = 'sandwich';
$nText = 'sandwiches';
// Not ID, so English fallback, and in english, plural 0
$this->assertEquals('sandwiches', t($text, $nText, 0));
$this->assertEquals('sandwich', t($text, $nText, 1));
$this->assertEquals('sandwiches', t($text, $nText, 2));
}
/**
* Test t() with an identified plural form in gettext mode.
*/
public function testTranslatePluralIDGettext()
{
$this->conf->set('translation.mode', 'gettext');
new Languages('en', $this->conf);
$text = 'shaare';
$nText = 'shaares';
$this->assertEquals('shaare', t($text, $nText, 0));
$this->assertEquals('shaare', t($text, $nText, 1));
$this->assertEquals('shaares', t($text, $nText, 2));
}
/**
* Test t() with a simple non identified value.
*/
public function testTranslateSingleNotIDPhp()
{
$this->conf->set('translation.mode', 'php');
new Languages('en', $this->conf);
$text = 'abcdé 564 fgK';
$this->assertEquals($text, t($text));
}
/**
* Test t() with a simple identified value in PHP mode.
*/
public function testTranslateSingleIDPhp()
{
$this->conf->set('translation.mode', 'php');
new Languages('en', $this->conf);
$text = 'permalink';
$this->assertEquals('permalien', t($text));
}
/**
* Test t() with a non identified plural form in PHP mode.
*/
public function testTranslatePluralNotIDPhp()
{
$this->conf->set('translation.mode', 'php');
new Languages('en', $this->conf);
$text = 'sandwich';
$nText = 'sandwiches';
// Not ID, so English fallback, and in english, plural 0
$this->assertEquals('sandwiches', t($text, $nText, 0));
$this->assertEquals('sandwich', t($text, $nText, 1));
$this->assertEquals('sandwiches', t($text, $nText, 2));
}
/**
* Test t() with an identified plural form in PHP mode.
*/
public function testTranslatePluralIDPhp()
{
$this->conf->set('translation.mode', 'php');
new Languages('en', $this->conf);
$text = 'shaare';
$nText = 'shaares';
// In english, zero is followed by plural form
$this->assertEquals('shaare', t($text, $nText, 0));
$this->assertEquals('shaare', t($text, $nText, 1));
$this->assertEquals('shaares', t($text, $nText, 2));
}
/**
* Test t() with an extension language file in gettext mode
*/
public function testTranslationExtensionGettext()
{
$this->conf->set('translation.mode', 'gettext');
$this->conf->set('translation.extensions.test', 'tests/utils/languages/');
new Languages('en', $this->conf);
$this->assertEquals('voiture', t('car', 'car', 1, 'test'));
$this->assertEquals('Fouille', t('Search', 'Search', 1, 'test'));
}
/**
* Test t() with an extension language file in PHP mode
*/
public function testTranslationExtensionPhp()
{
$this->conf->set('translation.mode', 'php');
$this->conf->set('translation.extensions.test', 'tests/utils/languages/');
new Languages('en', $this->conf);
$this->assertEquals('voiture', t('car', 'car', 1, 'test'));
$this->assertEquals('Fouille', t('Search', 'Search', 1, 'test'));
}
}

Binary file not shown.

View file

@ -0,0 +1,19 @@
msgid ""
msgstr ""
"Project-Id-Version: Extension test\n"
"POT-Creation-Date: 2017-05-20 13:54+0200\n"
"PO-Revision-Date: 2017-05-20 14:16+0200\n"
"Last-Translator: \n"
"Language-Team: Shaarli\n"
"Language: fr_FR\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
"X-Generator: Poedit 2.0.1\n"
msgid "car"
msgstr "voiture"
msgid "Search"
msgstr "Fouille"