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