LocalesTest.php 2.4 KB

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