Bug 1346611 - Introduce LocaleService::GetDefaultLocale. r=jfkthame
MozReview-Commit-ID: 8aMkbrh0SqI
--- a/intl/locale/LocaleService.cpp
+++ b/intl/locale/LocaleService.cpp
@@ -377,16 +377,23 @@ CreateOutArray(const nsTArray<nsCString>
char** result = static_cast<char**>(moz_xmalloc(n * sizeof(char*)));
for (uint32_t i = 0; i < n; i++) {
result[i] = moz_xstrdup(aArray[i].get());
}
return result;
}
NS_IMETHODIMP
+LocaleService::GetDefaultLocale(nsACString& aRetVal)
+{
+ aRetVal.AssignLiteral("en-US");
+ return NS_OK;
+}
+
+NS_IMETHODIMP
LocaleService::GetAppLocalesAsLangTags(uint32_t* aCount, char*** aOutArray)
{
if (mAppLocales.IsEmpty()) {
ReadAppLocales(mAppLocales);
}
*aCount = mAppLocales.Length();
*aOutArray = CreateOutArray(mAppLocales);
--- a/intl/locale/mozILocaleService.idl
+++ b/intl/locale/mozILocaleService.idl
@@ -47,16 +47,25 @@ interface mozILocaleService : nsISupport
* Result:
* Supported: ['es-MX']
*/
const long langNegStrategyFiltering = 0;
const long langNegStrategyMatching = 1;
const long langNegStrategyLookup = 2;
/**
+ * Default locale of the browser. The locale we are guaranteed to have
+ * resources for that should be used as a last resort fallack in cases
+ * where requested locales do not match available locales.
+ *
+ * At the moment it returns `en-US`.
+ */
+ readonly attribute ACString defaultLocale;
+
+ /**
* Returns a list of locales that the application should be localized to.
*
* The result is a ordered list of valid locale IDs and it should be
* used for all APIs that accept list of locales, like ECMA402 and L10n APIs.
*
* This API always returns at least one locale.
*
* When retrieving the locales for language negotiation and matching
--- a/intl/locale/tests/unit/test_localeService.js
+++ b/intl/locale/tests/unit/test_localeService.js
@@ -17,16 +17,22 @@ const localeService =
/**
* Make sure the locale service can be instantiated.
*/
function run_test()
{
run_next_test();
}
+add_test(function test_defaultLocale() {
+ const defaultLocale = localeService.defaultLocale;
+ do_check_true(defaultLocale === "en-US", "Default locale is en-US");
+ run_next_test();
+});
+
add_test(function test_getAppLocalesAsLangTags() {
const appLocale = localeService.getAppLocaleAsLangTag();
do_check_true(appLocale != "", "appLocale is non-empty");
const appLocales = localeService.getAppLocalesAsLangTags();
do_check_true(Array.isArray(appLocales), "appLocales returns an array");
do_check_true(appLocale == appLocales[0], "appLocale matches first entry in appLocales");