test-l10n-locale.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. const { getPreferedLocales, findClosestLocale } = require("sdk/l10n/locale");
  5. const prefs = require("sdk/preferences/service");
  6. const { Cc, Ci, Cu } = require("chrome");
  7. const { Services } = Cu.import("resource://gre/modules/Services.jsm");
  8. const BundleService = Cc["@mozilla.org/intl/stringbundle;1"].getService(Ci.nsIStringBundleService);
  9. const PREF_MATCH_OS_LOCALE = "intl.locale.matchOS";
  10. const PREF_SELECTED_LOCALE = "general.useragent.locale";
  11. const PREF_ACCEPT_LANGUAGES = "intl.accept_languages";
  12. function assertPrefered(assert, expected, msg) {
  13. assert.equal(JSON.stringify(getPreferedLocales()), JSON.stringify(expected),
  14. msg);
  15. }
  16. exports.testGetPreferedLocales = function(assert) {
  17. prefs.set(PREF_MATCH_OS_LOCALE, false);
  18. prefs.set(PREF_SELECTED_LOCALE, "");
  19. prefs.set(PREF_ACCEPT_LANGUAGES, "");
  20. assertPrefered(assert, ["en-us"],
  21. "When all preferences are empty, we only have en-us");
  22. prefs.set(PREF_SELECTED_LOCALE, "fr");
  23. prefs.set(PREF_ACCEPT_LANGUAGES, "jp");
  24. assertPrefered(assert, ["fr", "jp", "en-us"],
  25. "We first have useragent locale, then web one and finally en-US");
  26. prefs.set(PREF_SELECTED_LOCALE, "en-US");
  27. prefs.set(PREF_ACCEPT_LANGUAGES, "en-US");
  28. assertPrefered(assert, ["en-us"],
  29. "We do not have duplicates");
  30. prefs.set(PREF_SELECTED_LOCALE, "en-US");
  31. prefs.set(PREF_ACCEPT_LANGUAGES, "fr");
  32. assertPrefered(assert, ["en-us", "fr"],
  33. "en-US can be first if specified by higher priority preference");
  34. // Reset what we changed
  35. prefs.reset(PREF_MATCH_OS_LOCALE);
  36. prefs.reset(PREF_SELECTED_LOCALE);
  37. prefs.reset(PREF_ACCEPT_LANGUAGES);
  38. }
  39. // In some cases, mainly on Fennec and on Linux version,
  40. // `general.useragent.locale` is a special 'localized' value, like:
  41. // "chrome://global/locale/intl.properties"
  42. exports.testPreferedLocalizedLocale = function(assert) {
  43. prefs.set(PREF_MATCH_OS_LOCALE, false);
  44. let bundleURL = "chrome://global/locale/intl.properties";
  45. prefs.setLocalized(PREF_SELECTED_LOCALE, bundleURL);
  46. let contentLocale = "ja";
  47. prefs.set(PREF_ACCEPT_LANGUAGES, contentLocale);
  48. // Read manually the expected locale value from the property file
  49. let expectedLocale = BundleService.createBundle(bundleURL).
  50. GetStringFromName(PREF_SELECTED_LOCALE).
  51. toLowerCase();
  52. // First add the useragent locale
  53. let expectedLocaleList = [expectedLocale];
  54. // Then the content locale
  55. if (expectedLocaleList.indexOf(contentLocale) == -1)
  56. expectedLocaleList.push(contentLocale);
  57. // Add default "en-us" fallback if the main language is not already en-us
  58. if (expectedLocaleList.indexOf("en-us") == -1)
  59. expectedLocaleList.push("en-us");
  60. assertPrefered(assert, expectedLocaleList, "test localized pref value");
  61. // Reset what we have changed
  62. prefs.reset(PREF_MATCH_OS_LOCALE);
  63. prefs.reset(PREF_SELECTED_LOCALE);
  64. prefs.reset(PREF_ACCEPT_LANGUAGES);
  65. }
  66. exports.testPreferedOsLocale = function(assert) {
  67. prefs.set(PREF_MATCH_OS_LOCALE, true);
  68. prefs.set(PREF_SELECTED_LOCALE, "");
  69. prefs.set(PREF_ACCEPT_LANGUAGES, "");
  70. let expectedLocale = Services.locale.getLocaleComponentForUserAgent().
  71. toLowerCase();
  72. let expectedLocaleList = [expectedLocale];
  73. // Add default "en-us" fallback if the main language is not already en-us
  74. if (expectedLocale != "en-us")
  75. expectedLocaleList.push("en-us");
  76. assertPrefered(assert, expectedLocaleList, "Ensure that we select OS locale when related preference is set");
  77. // Reset what we have changed
  78. prefs.reset(PREF_MATCH_OS_LOCALE);
  79. prefs.reset(PREF_SELECTED_LOCALE);
  80. prefs.reset(PREF_ACCEPT_LANGUAGES);
  81. }
  82. exports.testFindClosestLocale = function(assert) {
  83. // Second param of findClosestLocale (aMatchLocales) have to be in lowercase
  84. assert.equal(findClosestLocale([], []), null,
  85. "When everything is empty we get null");
  86. assert.equal(findClosestLocale(["en", "en-US"], ["en"]),
  87. "en", "We always accept exact match first 1/5");
  88. assert.equal(findClosestLocale(["en-US", "en"], ["en"]),
  89. "en", "We always accept exact match first 2/5");
  90. assert.equal(findClosestLocale(["en", "en-US"], ["en-us"]),
  91. "en-US", "We always accept exact match first 3/5");
  92. assert.equal(findClosestLocale(["ja-JP-mac", "ja", "ja-JP"], ["ja-jp"]),
  93. "ja-JP", "We always accept exact match first 4/5");
  94. assert.equal(findClosestLocale(["ja-JP-mac", "ja", "ja-JP"], ["ja-jp-mac"]),
  95. "ja-JP-mac", "We always accept exact match first 5/5");
  96. assert.equal(findClosestLocale(["en", "en-GB"], ["en-us"]),
  97. "en", "We accept more generic locale, when there is no exact match 1/2");
  98. assert.equal(findClosestLocale(["en-ZA", "en"], ["en-gb"]),
  99. "en", "We accept more generic locale, when there is no exact match 2/2");
  100. assert.equal(findClosestLocale(["ja-JP"], ["ja"]),
  101. "ja-JP", "We accept more specialized locale, when there is no exact match 1/2");
  102. // Better to select "ja" in this case but behave same as current AddonManager
  103. assert.equal(findClosestLocale(["ja-JP-mac", "ja"], ["ja-jp"]),
  104. "ja-JP-mac", "We accept more specialized locale, when there is no exact match 2/2");
  105. assert.equal(findClosestLocale(["en-US"], ["en-us"]),
  106. "en-US", "We keep the original one as result 1/2");
  107. assert.equal(findClosestLocale(["en-us"], ["en-us"]),
  108. "en-us", "We keep the original one as result 2/2");
  109. assert.equal(findClosestLocale(["ja-JP-mac"], ["ja-jp-mac"]),
  110. "ja-JP-mac", "We accept locale with 3 parts");
  111. assert.equal(findClosestLocale(["ja-JP"], ["ja-jp-mac"]),
  112. "ja-JP", "We accept locale with 2 parts from locale with 3 parts");
  113. assert.equal(findClosestLocale(["ja"], ["ja-jp-mac"]),
  114. "ja", "We accept locale with 1 part from locale with 3 parts");
  115. };
  116. require('sdk/test').run(exports);