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_ICUUtils_h__
7 #define mozilla_ICUUtils_h__
9 // We only build the ICU utils if we're building ICU:
10 #ifdef ENABLE_INTL_API
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
16 #include "mozilla/Scoped.h"
17 #include "nsStringGlue.h"
18 #include "unicode/unum.h" // for UNumberFormat
23 struct ScopedUNumberFormatTraits
{
24 typedef UNumberFormat
* type
;
25 static type
empty() { return nullptr; }
26 static void release(type handle
) { if (handle
) unum_close(handle
); }
29 typedef mozilla::Scoped
<ScopedUNumberFormatTraits
> AutoCloseUNumberFormat
;
36 * This class is used to encapsulate an nsIContent object to allow lazy
37 * iteration over its primary and fallback BCP 47 language tags.
39 class LanguageTagIterForContent
{
41 explicit LanguageTagIterForContent(nsIContent
* aContent
)
43 , mCurrentFallbackIndex(-1)
47 * Used to iterate over the nsIContent object's primary language tag and
48 * its fallbacks tags. The following sources of language tag information
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.
57 * Once all fallbacks have been exhausted then this function will set
58 * aBCP47LangTag to the empty string.
60 void GetNext(nsACString
& aBCP47LangTag
);
62 bool IsAtStart() const {
63 return mCurrentFallbackIndex
< 0;
68 int8_t mCurrentFallbackIndex
;
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.
76 static bool LocalizeNumber(double aValue
,
77 LanguageTagIterForContent
& aLangTags
,
78 nsAString
& aLocalizedValue
);
81 * Parses the localized number that is serialized in aValue using aLangTags
82 * and returns the result as a double. Returns NaN on failure.
84 static double ParseNumber(nsAString
& aValue
,
85 LanguageTagIterForContent
& aLangTags
);
87 static void AssignUCharArrayToString(UChar
* aICUString
,
89 nsAString
& aMozString
);
92 // Currently disabled because using C++ API doesn't play nicely with enabling
96 * Converts an IETF BCP 47 language code to an ICU Locale.
98 static Locale
BCP47CodeToLocale(const nsAString
& aBCP47Code
);
100 static void ToMozString(UnicodeString
& aICUString
, nsAString
& aMozString
);
101 static void ToICUString(nsAString
& aMozString
, UnicodeString
& aICUString
);
105 #endif /* ENABLE_INTL_API */
106 #endif /* MOZILLA_INTERNAL_API */
108 #endif /* mozilla_ICUUtils_h__ */