michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_ICUUtils_h__ michael@0: #define mozilla_ICUUtils_h__ michael@0: michael@0: // We only build the ICU utils if we're building ICU: michael@0: #ifdef ENABLE_INTL_API michael@0: michael@0: // The ICU utils implementation needs internal things like XPCOM strings and michael@0: // nsGkAtom, so we only build when included into internal libs: michael@0: #ifdef MOZILLA_INTERNAL_API michael@0: michael@0: #include "mozilla/Scoped.h" michael@0: #include "nsStringGlue.h" michael@0: #include "unicode/unum.h" // for UNumberFormat michael@0: michael@0: class nsIContent; michael@0: michael@0: namespace { michael@0: struct ScopedUNumberFormatTraits { michael@0: typedef UNumberFormat* type; michael@0: static type empty() { return nullptr; } michael@0: static void release(type handle) { if (handle) unum_close(handle); } michael@0: }; michael@0: }; michael@0: typedef mozilla::Scoped AutoCloseUNumberFormat; michael@0: michael@0: class ICUUtils michael@0: { michael@0: public: michael@0: michael@0: /** michael@0: * This class is used to encapsulate an nsIContent object to allow lazy michael@0: * iteration over its primary and fallback BCP 47 language tags. michael@0: */ michael@0: class LanguageTagIterForContent { michael@0: public: michael@0: LanguageTagIterForContent(nsIContent* aContent) michael@0: : mContent(aContent) michael@0: , mCurrentFallbackIndex(-1) michael@0: {} michael@0: michael@0: /** michael@0: * Used to iterate over the nsIContent object's primary language tag and michael@0: * its fallbacks tags. The following sources of language tag information michael@0: * are tried in turn: michael@0: * michael@0: * 1) the "lang" of the nsIContent object (which is based on the 'lang'/ michael@0: * 'xml:lang' attribute on itself or the nearest ancestor to have such michael@0: * an attribute, if any); michael@0: * 2) the Content-Language HTTP pragma directive or HTTP header; michael@0: * 3) the configured language tag of the user-agent. michael@0: * michael@0: * Once all fallbacks have been exhausted then this function will set michael@0: * aBCP47LangTag to the empty string. michael@0: */ michael@0: void GetNext(nsACString& aBCP47LangTag); michael@0: michael@0: bool IsAtStart() const { michael@0: return mCurrentFallbackIndex < 0; michael@0: } michael@0: michael@0: private: michael@0: nsIContent* mContent; michael@0: int8_t mCurrentFallbackIndex; michael@0: }; michael@0: michael@0: /** michael@0: * Attempts to localize aValue and return the result via the aLocalizedValue michael@0: * outparam. Returns true on success. Returns false on failure, in which michael@0: * case aLocalizedValue will be untouched. michael@0: */ michael@0: static bool LocalizeNumber(double aValue, michael@0: LanguageTagIterForContent& aLangTags, michael@0: nsAString& aLocalizedValue); michael@0: michael@0: /** michael@0: * Parses the localized number that is serialized in aValue using aLangTags michael@0: * and returns the result as a double. Returns NaN on failure. michael@0: */ michael@0: static double ParseNumber(nsAString& aValue, michael@0: LanguageTagIterForContent& aLangTags); michael@0: michael@0: static void AssignUCharArrayToString(UChar* aICUString, michael@0: int32_t aLength, michael@0: nsAString& aMozString); michael@0: michael@0: #if 0 michael@0: // Currently disabled because using C++ API doesn't play nicely with enabling michael@0: // system ICU. michael@0: michael@0: /** michael@0: * Converts an IETF BCP 47 language code to an ICU Locale. michael@0: */ michael@0: static Locale BCP47CodeToLocale(const nsAString& aBCP47Code); michael@0: michael@0: static void ToMozString(UnicodeString& aICUString, nsAString& aMozString); michael@0: static void ToICUString(nsAString& aMozString, UnicodeString& aICUString); michael@0: #endif michael@0: }; michael@0: michael@0: #endif /* ENABLE_INTL_API */ michael@0: #endif /* MOZILLA_INTERNAL_API */ michael@0: michael@0: #endif /* mozilla_ICUUtils_h__ */ michael@0: