ICUUtils.h 3.5 KB

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