summaryrefslogtreecommitdiff
path: root/intl/locale/LocaleService.cpp
blob: 2d3429d688b5df9b128c66576be02d79847f153f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* -*- 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 "jsapi.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/Services.h"
#include "nsIObserverService.h"
#include "nsIToolkitChromeRegistry.h"

using namespace mozilla::intl;

NS_IMPL_ISUPPORTS(LocaleService, mozILocaleService)

mozilla::StaticRefPtr<LocaleService> LocaleService::sInstance;

/**
 * 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"));
  }
}

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);
    }
  }
}

/**
 * mozILocaleService methods
 */
NS_IMETHODIMP
LocaleService::GetAppLocales(JSContext* aCtx, JS::MutableHandleValue aRetVal)
{
  if (mAppLocales.IsEmpty()) {
    ReadAppLocales(mAppLocales);
  }

  uint32_t appLocalesNum = mAppLocales.Length();

  JS::RootedObject locales(aCtx, JS_NewArrayObject(aCtx, appLocalesNum));
  JS::Rooted<JS::Value> value(aCtx);

  for (size_t i = 0; i < appLocalesNum; i++) {
    const nsCString& loc = mAppLocales[i];
    JSString* str = JS_NewStringCopyN(aCtx, loc.get(), loc.Length());
    value.setString(str);
    JS_DefineElement(aCtx, locales, i, value, JSPROP_ENUMERATE);
  }

  aRetVal.setObject(*locales);
  return NS_OK;
}

NS_IMETHODIMP
LocaleService::GetAppLocale(JSContext* aCtx, nsACString& aRetVal)
{
  GetAppLocale(aRetVal);
  return NS_OK;
}