LocalesTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. require_once('gettext.inc');
  3. class LocaleTest extends PHPUnit_Framework_TestCase
  4. {
  5. public function test_setlocale()
  6. {
  7. putenv("LC_ALL=");
  8. // _setlocale defaults to a locale name from environment variable LANG.
  9. putenv("LANG=sr_RS");
  10. $this->assertEquals('sr_RS', _setlocale(LC_MESSAGES, 0));
  11. }
  12. public function test_setlocale_system()
  13. {
  14. putenv("LC_ALL=");
  15. // For an existing locale, it never needs emulation.
  16. putenv("LANG=C");
  17. _setlocale(LC_MESSAGES, "");
  18. $this->assertEquals(0, locale_emulation());
  19. }
  20. public function test_setlocale_emulation()
  21. {
  22. putenv("LC_ALL=");
  23. // If we set it to a non-existent locale, it still works, but uses
  24. // emulation.
  25. _setlocale(LC_MESSAGES, "xxx_XXX");
  26. $this->assertEquals('xxx_XXX', _setlocale(LC_MESSAGES, 0));
  27. $this->assertEquals(1, locale_emulation());
  28. }
  29. public function test_get_list_of_locales()
  30. {
  31. // For a locale containing country code, we prefer
  32. // full locale name, but if that's not found, fall back
  33. // to the language only locale name.
  34. $this->assertEquals(
  35. array("sr_RS", "sr"),
  36. get_list_of_locales("sr_RS")
  37. );
  38. // If language code is used, it's the only thing returned.
  39. $this->assertEquals(
  40. array("sr"),
  41. get_list_of_locales("sr")
  42. );
  43. // There is support for language and charset only.
  44. $this->assertEquals(
  45. array("sr.UTF-8", "sr"),
  46. get_list_of_locales("sr.UTF-8")
  47. );
  48. // It can also split out character set from the full locale name.
  49. $this->assertEquals(
  50. array("sr_RS.UTF-8", "sr_RS", "sr"),
  51. get_list_of_locales("sr_RS.UTF-8")
  52. );
  53. // There is support for @modifier in locale names as well.
  54. $this->assertEquals(
  55. array("sr_RS.UTF-8@latin", "sr_RS@latin", "sr@latin",
  56. "sr_RS.UTF-8", "sr_RS", "sr"),
  57. get_list_of_locales("sr_RS.UTF-8@latin")
  58. );
  59. // We can pass in only language and modifier.
  60. $this->assertEquals(
  61. array("sr@latin", "sr"),
  62. get_list_of_locales("sr@latin")
  63. );
  64. // If locale name is not following the regular POSIX pattern,
  65. // it's used verbatim.
  66. $this->assertEquals(
  67. array("something"),
  68. get_list_of_locales("something")
  69. );
  70. // Passing in an empty string returns an empty array.
  71. $this->assertEquals(
  72. array(),
  73. get_list_of_locales("")
  74. );
  75. }
  76. }