LanguagesFrTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace Shaarli;
  3. use Shaarli\Config\ConfigManager;
  4. /**
  5. * Class LanguagesFrTest
  6. *
  7. * Test the translation system in PHP and gettext mode with French language.
  8. *
  9. * @package Shaarli
  10. */
  11. class LanguagesFrTest extends \PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * @var string Config file path (without extension).
  15. */
  16. protected static $configFile = 'tests/utils/config/configJson';
  17. /**
  18. * @var ConfigManager
  19. */
  20. protected $conf;
  21. /**
  22. * Init: force French
  23. */
  24. public function setUp()
  25. {
  26. $this->conf = new ConfigManager(self::$configFile);
  27. $this->conf->set('translation.language', 'fr');
  28. }
  29. /**
  30. * Reset the locale since gettext seems to mess with it, making it too long
  31. */
  32. public static function tearDownAfterClass()
  33. {
  34. if (! empty(getenv('UT_LOCALE'))) {
  35. setlocale(LC_ALL, getenv('UT_LOCALE'));
  36. }
  37. }
  38. /**
  39. * Test t() with a simple non identified value.
  40. */
  41. public function testTranslateSingleNotIDGettext()
  42. {
  43. $this->conf->set('translation.mode', 'gettext');
  44. new Languages('en', $this->conf);
  45. $text = 'abcdé 564 fgK';
  46. $this->assertEquals($text, t($text));
  47. }
  48. /**
  49. * Test t() with a simple identified value in gettext mode.
  50. */
  51. public function testTranslateSingleIDGettext()
  52. {
  53. $this->conf->set('translation.mode', 'gettext');
  54. new Languages('en', $this->conf);
  55. $text = 'permalink';
  56. $this->assertEquals('permalien', t($text));
  57. }
  58. /**
  59. * Test t() with a non identified plural form in gettext mode.
  60. */
  61. public function testTranslatePluralNotIDGettext()
  62. {
  63. $this->conf->set('translation.mode', 'gettext');
  64. new Languages('en', $this->conf);
  65. $text = 'sandwich';
  66. $nText = 'sandwiches';
  67. // Not ID, so English fallback, and in english, plural 0
  68. $this->assertEquals('sandwiches', t($text, $nText, 0));
  69. $this->assertEquals('sandwich', t($text, $nText, 1));
  70. $this->assertEquals('sandwiches', t($text, $nText, 2));
  71. }
  72. /**
  73. * Test t() with an identified plural form in gettext mode.
  74. */
  75. public function testTranslatePluralIDGettext()
  76. {
  77. $this->conf->set('translation.mode', 'gettext');
  78. new Languages('en', $this->conf);
  79. $text = 'shaare';
  80. $nText = 'shaares';
  81. $this->assertEquals('shaare', t($text, $nText, 0));
  82. $this->assertEquals('shaare', t($text, $nText, 1));
  83. $this->assertEquals('shaares', t($text, $nText, 2));
  84. }
  85. /**
  86. * Test t() with a simple non identified value.
  87. */
  88. public function testTranslateSingleNotIDPhp()
  89. {
  90. $this->conf->set('translation.mode', 'php');
  91. new Languages('en', $this->conf);
  92. $text = 'abcdé 564 fgK';
  93. $this->assertEquals($text, t($text));
  94. }
  95. /**
  96. * Test t() with a simple identified value in PHP mode.
  97. */
  98. public function testTranslateSingleIDPhp()
  99. {
  100. $this->conf->set('translation.mode', 'php');
  101. new Languages('en', $this->conf);
  102. $text = 'permalink';
  103. $this->assertEquals('permalien', t($text));
  104. }
  105. /**
  106. * Test t() with a non identified plural form in PHP mode.
  107. */
  108. public function testTranslatePluralNotIDPhp()
  109. {
  110. $this->conf->set('translation.mode', 'php');
  111. new Languages('en', $this->conf);
  112. $text = 'sandwich';
  113. $nText = 'sandwiches';
  114. // Not ID, so English fallback, and in english, plural 0
  115. $this->assertEquals('sandwiches', t($text, $nText, 0));
  116. $this->assertEquals('sandwich', t($text, $nText, 1));
  117. $this->assertEquals('sandwiches', t($text, $nText, 2));
  118. }
  119. /**
  120. * Test t() with an identified plural form in PHP mode.
  121. */
  122. public function testTranslatePluralIDPhp()
  123. {
  124. $this->conf->set('translation.mode', 'php');
  125. new Languages('en', $this->conf);
  126. $text = 'shaare';
  127. $nText = 'shaares';
  128. // In english, zero is followed by plural form
  129. $this->assertEquals('shaare', t($text, $nText, 0));
  130. $this->assertEquals('shaare', t($text, $nText, 1));
  131. $this->assertEquals('shaares', t($text, $nText, 2));
  132. }
  133. /**
  134. * Test t() with an extension language file in gettext mode
  135. */
  136. public function testTranslationExtensionGettext()
  137. {
  138. $this->conf->set('translation.mode', 'gettext');
  139. $this->conf->set('translation.extensions.test', 'tests/utils/languages/');
  140. new Languages('en', $this->conf);
  141. $txt = 'car'; // ignore me poedit
  142. $this->assertEquals('voiture', t($txt, $txt, 1, 'test'));
  143. $this->assertEquals('Fouille', t('Search', 'Search', 1, 'test'));
  144. }
  145. /**
  146. * Test t() with an extension language file in PHP mode
  147. */
  148. public function testTranslationExtensionPhp()
  149. {
  150. $this->conf->set('translation.mode', 'php');
  151. $this->conf->set('translation.extensions.test', 'tests/utils/languages/');
  152. new Languages('en', $this->conf);
  153. $txt = 'car'; // ignore me poedit
  154. $this->assertEquals('voiture', t($txt, $txt, 1, 'test'));
  155. $this->assertEquals('Fouille', t('Search', 'Search', 1, 'test'));
  156. }
  157. /**
  158. * Test t() with an extension language file coming from the theme in gettext mode
  159. */
  160. public function testTranslationThemeExtensionGettext()
  161. {
  162. $this->conf->set('translation.mode', 'gettext');
  163. $this->conf->set('raintpl_tpl', 'tests/utils/customtpl/');
  164. $this->conf->set('theme', 'dummy');
  165. new Languages('en', $this->conf);
  166. $txt = 'rooster'; // ignore me poedit
  167. $this->assertEquals('coq', t($txt, $txt, 1, 'dummy'));
  168. }
  169. /**
  170. * Test t() with an extension language file coming from the theme in PHP mode
  171. */
  172. public function testTranslationThemeExtensionPhp()
  173. {
  174. $this->conf->set('translation.mode', 'php');
  175. $this->conf->set('raintpl_tpl', 'tests/utils/customtpl/');
  176. $this->conf->set('theme', 'dummy');
  177. new Languages('en', $this->conf);
  178. $txt = 'rooster'; // ignore me poedit
  179. $this->assertEquals('coq', t($txt, $txt, 1, 'dummy'));
  180. }
  181. }