|
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/. */ |
|
5 |
|
6 #ifndef mozilla_ICUUtils_h__ |
|
7 #define mozilla_ICUUtils_h__ |
|
8 |
|
9 // We only build the ICU utils if we're building ICU: |
|
10 #ifdef ENABLE_INTL_API |
|
11 |
|
12 // The ICU utils implementation needs internal things like XPCOM strings and |
|
13 // nsGkAtom, so we only build when included into internal libs: |
|
14 #ifdef MOZILLA_INTERNAL_API |
|
15 |
|
16 #include "mozilla/Scoped.h" |
|
17 #include "nsStringGlue.h" |
|
18 #include "unicode/unum.h" // for UNumberFormat |
|
19 |
|
20 class nsIContent; |
|
21 |
|
22 namespace { |
|
23 struct ScopedUNumberFormatTraits { |
|
24 typedef UNumberFormat* type; |
|
25 static type empty() { return nullptr; } |
|
26 static void release(type handle) { if (handle) unum_close(handle); } |
|
27 }; |
|
28 }; |
|
29 typedef mozilla::Scoped<ScopedUNumberFormatTraits> AutoCloseUNumberFormat; |
|
30 |
|
31 class ICUUtils |
|
32 { |
|
33 public: |
|
34 |
|
35 /** |
|
36 * This class is used to encapsulate an nsIContent object to allow lazy |
|
37 * iteration over its primary and fallback BCP 47 language tags. |
|
38 */ |
|
39 class LanguageTagIterForContent { |
|
40 public: |
|
41 LanguageTagIterForContent(nsIContent* aContent) |
|
42 : mContent(aContent) |
|
43 , mCurrentFallbackIndex(-1) |
|
44 {} |
|
45 |
|
46 /** |
|
47 * Used to iterate over the nsIContent object's primary language tag and |
|
48 * its fallbacks tags. The following sources of language tag information |
|
49 * are tried in turn: |
|
50 * |
|
51 * 1) the "lang" of the nsIContent object (which is based on the 'lang'/ |
|
52 * 'xml:lang' attribute on itself or the nearest ancestor to have such |
|
53 * an attribute, if any); |
|
54 * 2) the Content-Language HTTP pragma directive or HTTP header; |
|
55 * 3) the configured language tag of the user-agent. |
|
56 * |
|
57 * Once all fallbacks have been exhausted then this function will set |
|
58 * aBCP47LangTag to the empty string. |
|
59 */ |
|
60 void GetNext(nsACString& aBCP47LangTag); |
|
61 |
|
62 bool IsAtStart() const { |
|
63 return mCurrentFallbackIndex < 0; |
|
64 } |
|
65 |
|
66 private: |
|
67 nsIContent* mContent; |
|
68 int8_t mCurrentFallbackIndex; |
|
69 }; |
|
70 |
|
71 /** |
|
72 * Attempts to localize aValue and return the result via the aLocalizedValue |
|
73 * outparam. Returns true on success. Returns false on failure, in which |
|
74 * case aLocalizedValue will be untouched. |
|
75 */ |
|
76 static bool LocalizeNumber(double aValue, |
|
77 LanguageTagIterForContent& aLangTags, |
|
78 nsAString& aLocalizedValue); |
|
79 |
|
80 /** |
|
81 * Parses the localized number that is serialized in aValue using aLangTags |
|
82 * and returns the result as a double. Returns NaN on failure. |
|
83 */ |
|
84 static double ParseNumber(nsAString& aValue, |
|
85 LanguageTagIterForContent& aLangTags); |
|
86 |
|
87 static void AssignUCharArrayToString(UChar* aICUString, |
|
88 int32_t aLength, |
|
89 nsAString& aMozString); |
|
90 |
|
91 #if 0 |
|
92 // Currently disabled because using C++ API doesn't play nicely with enabling |
|
93 // system ICU. |
|
94 |
|
95 /** |
|
96 * Converts an IETF BCP 47 language code to an ICU Locale. |
|
97 */ |
|
98 static Locale BCP47CodeToLocale(const nsAString& aBCP47Code); |
|
99 |
|
100 static void ToMozString(UnicodeString& aICUString, nsAString& aMozString); |
|
101 static void ToICUString(nsAString& aMozString, UnicodeString& aICUString); |
|
102 #endif |
|
103 }; |
|
104 |
|
105 #endif /* ENABLE_INTL_API */ |
|
106 #endif /* MOZILLA_INTERNAL_API */ |
|
107 |
|
108 #endif /* mozilla_ICUUtils_h__ */ |
|
109 |