summaryrefslogtreecommitdiff
path: root/intl/locale/LocaleService.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'intl/locale/LocaleService.cpp')
-rw-r--r--intl/locale/LocaleService.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/intl/locale/LocaleService.cpp b/intl/locale/LocaleService.cpp
new file mode 100644
index 0000000000..55542dc1a9
--- /dev/null
+++ b/intl/locale/LocaleService.cpp
@@ -0,0 +1,80 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "LocaleService.h"
+#include "mozilla/ClearOnShutdown.h"
+#include "mozilla/Services.h"
+#include "nsIObserverService.h"
+#include "nsIToolkitChromeRegistry.h"
+
+using namespace mozilla::intl;
+
+/**
+ * This function performs the actual language negotiation for the API.
+ *
+ * Currently it collects the locale ID used by nsChromeRegistry and
+ * adds hardcoded "en-US" locale as a fallback.
+ */
+static void
+ReadAppLocales(nsTArray<nsCString>& aRetVal)
+{
+ nsAutoCString uaLangTag;
+ nsCOMPtr<nsIToolkitChromeRegistry> cr =
+ mozilla::services::GetToolkitChromeRegistryService();
+ if (cr) {
+ cr->GetSelectedLocale(NS_LITERAL_CSTRING("global"), true, uaLangTag);
+ }
+ if (!uaLangTag.IsEmpty()) {
+ aRetVal.AppendElement(uaLangTag);
+ }
+
+ if (!uaLangTag.EqualsLiteral("en-US")) {
+ aRetVal.AppendElement(NS_LITERAL_CSTRING("en-US"));
+ }
+}
+
+mozilla::StaticAutoPtr<LocaleService> LocaleService::sInstance;
+
+LocaleService* LocaleService::GetInstance()
+{
+ if (!sInstance) {
+ sInstance = new LocaleService();
+ ClearOnShutdown(&sInstance);
+ }
+ return sInstance;
+}
+
+void
+LocaleService::GetAppLocales(nsTArray<nsCString>& aRetVal)
+{
+ if (mAppLocales.IsEmpty()) {
+ ReadAppLocales(mAppLocales);
+ }
+ aRetVal = mAppLocales;
+}
+
+void
+LocaleService::GetAppLocale(nsACString& aRetVal)
+{
+ if (mAppLocales.IsEmpty()) {
+ ReadAppLocales(mAppLocales);
+ }
+ aRetVal = mAppLocales[0];
+}
+
+void
+LocaleService::Refresh()
+{
+ nsTArray<nsCString> newLocales;
+ ReadAppLocales(newLocales);
+
+ if (mAppLocales != newLocales) {
+ mAppLocales = Move(newLocales);
+ nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
+ if (obs) {
+ obs->NotifyObservers(nullptr, "intl:app-locales-changed", nullptr);
+ }
+ }
+} \ No newline at end of file