From e1f95393a1ac94e15707e97717841e432a0b5269 Mon Sep 17 00:00:00 2001 From: Moonchild Date: Thu, 19 May 2022 22:37:20 +0000 Subject: Issue #1210 - Expose getAppLocales() to Chrome and XBL This allows JS to access the application locales. --- dom/base/nsGlobalWindow.cpp | 12 ++++++++++++ dom/base/nsGlobalWindow.h | 3 +++ dom/webidl/Window.webidl | 15 +++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/dom/base/nsGlobalWindow.cpp b/dom/base/nsGlobalWindow.cpp index c6b35a9bf0..be1c2463e5 100644 --- a/dom/base/nsGlobalWindow.cpp +++ b/dom/base/nsGlobalWindow.cpp @@ -49,6 +49,7 @@ #include "nsNetUtil.h" #include "nsVariant.h" #include "nsPrintfCString.h" +#include "mozilla/intl/LocaleService.h" // Helper Classes #include "nsJSUtils.h" @@ -14794,6 +14795,17 @@ nsGlobalWindow::CreateWorklet(ErrorResult& aRv) return worklet.forget(); } +void +nsGlobalWindow::GetAppLocales(nsTArray& aLocales) +{ + nsTArray appLocales; + mozilla::intl::LocaleService::GetInstance()->GetAppLocales(appLocales); + + for (uint32_t i = 0; i < appLocales.Length(); i++) { + aLocales.AppendElement(NS_ConvertUTF8toUTF16(appLocales[i])); + } +} + template class nsPIDOMWindow; template class nsPIDOMWindow; template class nsPIDOMWindow; diff --git a/dom/base/nsGlobalWindow.h b/dom/base/nsGlobalWindow.h index e42835a0b2..43270e80d9 100644 --- a/dom/base/nsGlobalWindow.h +++ b/dom/base/nsGlobalWindow.h @@ -897,6 +897,9 @@ public: already_AddRefed CreateWorklet(mozilla::ErrorResult& aRv); + void + GetAppLocales(nsTArray& aLocales); + protected: bool AlertOrConfirm(bool aAlert, const nsAString& aMessage, nsIPrincipal& aSubjectPrincipal, diff --git a/dom/webidl/Window.webidl b/dom/webidl/Window.webidl index 1b98042473..8a6b05a88e 100644 --- a/dom/webidl/Window.webidl +++ b/dom/webidl/Window.webidl @@ -486,3 +486,18 @@ callback IdleRequestCallback = void (IdleDeadline deadline); partial interface Window { [ChromeOnly] readonly attribute boolean isSecureContextIfOpenerIgnored; }; + +partial interface Window { +/** + * Returns a list of locales that the application should be localized to. + * + * The result is a sorted 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. + * + * Example: ["en-US", "de", "pl", "sr-Cyrl", "zh-Hans-HK"] + */ + [Func="IsChromeOrXBL"] + sequence getAppLocales(); +}; \ No newline at end of file -- cgit v1.2.3