1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode:nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_intl_MozLocale_h__
7 #define mozilla_intl_MozLocale_h__
16 * Locale class is a core representation of a single locale.
18 * A locale is a data object representing a combination of language, script,
19 * region, variant and a set of regional extension preferences that may further specify
20 * particular user choices like calendar, numbering system, etc.
22 * A locale can be expressed as a language tag string, like a simple "fr" for French,
23 * or a more specific "sr-Cyrl-RS-u-hc-h12" for Serbian in Russia with a Cyrylic script,
24 * and hour cycle selected to be `h12`.
26 * The format of the language tag follows BCP47 standard and implements a subset of it.
27 * In the future we expect to extend this class to cover more subtags and extensions.
29 * BCP47: https://tools.ietf.org/html/bcp47
31 * The aim of this class it aid in validation, parsing and canonicalization of the
34 * It allows the user to input any well-formed BCP47 language tag and operate
35 * on its subtags in a canonicalized form.
37 * It should be used for all operations on language tags, and together with
38 * LocaleService::NegotiateLanguages for language negotiation.
42 * Locale loc = Locale("de-at");
44 * ASSERT_TRUE(loc.GetLanguage().Equals("de"));
45 * ASSERT_TRUE(loc.GetScript().IsEmpty());
46 * ASSERT_TRUE(loc.GetRegion().Equals("AT")); // canonicalized to upper case
49 * Note: The file name is `MozLocale` to avoid compilation problems on case-insensitive
50 * Windows. The class name is `Locale`.
54 explicit Locale(const nsACString
& aLocale
);
55 explicit Locale(const char* aLocale
)
56 : Locale(nsDependentCString(aLocale
))
59 const nsACString
& GetLanguage() const;
60 const nsACString
& GetScript() const;
61 const nsACString
& GetRegion() const;
62 const nsTArray
<nsCString
>& GetVariants() const;
64 bool IsValid() const {
68 const nsCString
AsString() const;
70 bool Matches(const Locale
& aOther
, bool aThisRange
, bool aOtherRange
) const;
71 bool AddLikelySubtags();
75 // Mark the object as invalid, meaning we shouldn't use it any more.
80 bool operator== (const Locale
& aOther
) {
81 // Note: invalid Locale objects are never treated as equal to anything
82 // (even other invalid ones).
85 mLanguage
.Equals(aOther
.mLanguage
) &&
86 mScript
.Equals(aOther
.mScript
) &&
87 mRegion
.Equals(aOther
.mRegion
) &&
88 mVariants
== aOther
.mVariants
&&
89 mPrivateUse
== aOther
.mPrivateUse
;
93 nsAutoCStringN
<3> mLanguage
;
94 nsAutoCStringN
<4> mScript
;
95 nsAutoCStringN
<2> mRegion
;
96 nsTArray
<nsCString
> mVariants
;
97 nsTArray
<nsCString
> mPrivateUse
;
102 } // namespace mozilla
104 DECLARE_USE_COPY_CONSTRUCTORS(mozilla::intl::Locale
)
106 #endif /* mozilla_intl_MozLocale_h__ */