summaryrefslogtreecommitdiff
path: root/intl/locale/LocaleService.cpp
blob: 7a84ae24494c83113da512e2bd71dba5b03b012e (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
/* -*- 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;

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::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(uint32_t* aCount, char*** aOutArray)
{
  if (mAppLocales.IsEmpty()) {
    ReadAppLocales(mAppLocales);
  }

  *aCount = mAppLocales.Length();
  *aOutArray = static_cast<char**>(moz_xmalloc(*aCount * sizeof(char*)));

  for (uint32_t i = 0; i < *aCount; i++) {
    (*aOutArray)[i] = moz_xstrdup(mAppLocales[i].get());
  }

  return NS_OK;
}

NS_IMETHODIMP
LocaleService::GetAppLocale(nsACString& aRetVal)
{
  if (mAppLocales.IsEmpty()) {
    ReadAppLocales(mAppLocales);
  }
  aRetVal = mAppLocales[0];
  return NS_OK;
}