summaryrefslogtreecommitdiff
path: root/intl/locale/LocaleService.h
blob: 42f6c481672702415406289366be48766d309864 (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: 2; indent-tabs-mode:nil; c-basic-offset: 2 -*- */
/* 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/. */

#ifndef mozilla_intl_LocaleService_h__
#define mozilla_intl_LocaleService_h__

#include "mozilla/StaticPtr.h"
#include "nsString.h"
#include "nsTArray.h"

#include "mozILocaleService.h"

namespace mozilla {
namespace intl {


/**
 * LocaleService is a manager of language negotiation in Gecko.
 *
 * It's intended to be the core place for collecting available and
 * requested languages and negotiating them to produce a fallback
 * chain of locales for the application.
 */
class LocaleService : public mozILocaleService
{
public:
  NS_DECL_ISUPPORTS
  NS_DECL_MOZILOCALESERVICE

  /**
   * Create (if necessary) and return a raw pointer to the singleton instance.
   * Use this accessor in C++ code that just wants to call a method on the
   * instance, but does not need to hold a reference, as in
   *    nsAutoCString str;
   *    LocaleService::GetInstance()->GetAppLocale(str);
   */
  static LocaleService* GetInstance();

  /**
   * Return an addRef'd pointer to the singleton instance. This is used by the
   * XPCOM constructor that exists to support usage from JS.
   */
  static already_AddRefed<LocaleService> GetInstanceAddRefed()
  {
    return RefPtr<LocaleService>(GetInstance()).forget();
  }

  /**
   * 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"]
   *
   * Usage:
   * nsTArray<nsCString> appLocales;
   * LocaleService::GetInstance()->GetAppLocales(appLocales);
   */
  void GetAppLocales(nsTArray<nsCString>& aRetVal);

  /**
   * Returns the best locale that the application should be localized to.
   *
   * The result is a valid locale IDs and it should be
   * used for all APIs that do not handle language negotiation.
   *
   * Where possible, GetAppLocales should be preferred over this API and
   * all callsites should handle some form of "best effort" language
   * negotiation to respect user preferences in case the use case does
   * not have data for the first locale in the list.
   *
   * Example: "zh-Hans-HK"
   *
   * Usage:
   * nsAutoCString appLocale;
   * LocaleService::GetInstance()->GetAppLocale(appLocale);
   */
  void GetAppLocale(nsACString& aRetVal);

  /**
   * Triggers a refresh of the language negotiation process.
   *
   * If the result differs from the previous list, it will additionally
   * trigger a global event "intl:app-locales-changed".
   */
  void Refresh();

protected:
  nsTArray<nsCString> mAppLocales;

private:
  virtual ~LocaleService() {};

  static StaticRefPtr<LocaleService> sInstance;
};

} // intl
} // namespace mozilla

#endif /* mozilla_intl_LocaleService_h__ */