localebuilder.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // © 2018 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html#License
  3. #ifndef __LOCALEBUILDER_H__
  4. #define __LOCALEBUILDER_H__
  5. #include "unicode/utypes.h"
  6. #if U_SHOW_CPLUSPLUS_API
  7. #include "unicode/locid.h"
  8. #include "unicode/localematcher.h"
  9. #include "unicode/stringpiece.h"
  10. #include "unicode/uobject.h"
  11. /**
  12. * \file
  13. * \brief C++ API: Builder API for Locale
  14. */
  15. U_NAMESPACE_BEGIN
  16. class CharString;
  17. /**
  18. * <code>LocaleBuilder</code> is used to build instances of <code>Locale</code>
  19. * from values configured by the setters. Unlike the <code>Locale</code>
  20. * constructors, the <code>LocaleBuilder</code> checks if a value configured by a
  21. * setter satisfies the syntax requirements defined by the <code>Locale</code>
  22. * class. A <code>Locale</code> object created by a <code>LocaleBuilder</code> is
  23. * well-formed and can be transformed to a well-formed IETF BCP 47 language tag
  24. * without losing information.
  25. *
  26. * <p>The following example shows how to create a <code>Locale</code> object
  27. * with the <code>LocaleBuilder</code>.
  28. * <blockquote>
  29. * <pre>
  30. * UErrorCode status = U_ZERO_ERROR;
  31. * Locale aLocale = LocaleBuilder()
  32. * .setLanguage("sr")
  33. * .setScript("Latn")
  34. * .setRegion("RS")
  35. * .build(status);
  36. * if (U_SUCCESS(status)) {
  37. * // ...
  38. * }
  39. * </pre>
  40. * </blockquote>
  41. *
  42. * <p>LocaleBuilders can be reused; <code>clear()</code> resets all
  43. * fields to their default values.
  44. *
  45. * <p>LocaleBuilder tracks errors in an internal UErrorCode. For all setters,
  46. * except setLanguageTag and setLocale, LocaleBuilder will return immediately
  47. * if the internal UErrorCode is in error state.
  48. * To reset internal state and error code, call clear method.
  49. * The setLanguageTag and setLocale method will first clear the internal
  50. * UErrorCode, then track the error of the validation of the input parameter
  51. * into the internal UErrorCode.
  52. *
  53. * @stable ICU 64
  54. */
  55. class U_COMMON_API LocaleBuilder : public UObject {
  56. public:
  57. /**
  58. * Constructs an empty LocaleBuilder. The default value of all
  59. * fields, extensions, and private use information is the
  60. * empty string.
  61. *
  62. * @stable ICU 64
  63. */
  64. LocaleBuilder();
  65. /**
  66. * Destructor
  67. * @stable ICU 64
  68. */
  69. virtual ~LocaleBuilder();
  70. /**
  71. * Resets the <code>LocaleBuilder</code> to match the provided
  72. * <code>locale</code>. Existing state is discarded.
  73. *
  74. * <p>All fields of the locale must be well-formed.
  75. * <p>This method clears the internal UErrorCode.
  76. *
  77. * @param locale the locale
  78. * @return This builder.
  79. *
  80. * @stable ICU 64
  81. */
  82. LocaleBuilder& setLocale(const Locale& locale);
  83. /**
  84. * Resets the LocaleBuilder to match the provided
  85. * [Unicode Locale Identifier](http://www.unicode.org/reports/tr35/tr35.html#unicode_locale_id) .
  86. * Discards the existing state. the empty string cause the builder to be
  87. * reset, like {@link #clear}. Grandfathered tags are converted to their
  88. * canonical form before being processed. Otherwise, the <code>language
  89. * tag</code> must be well-formed, or else the build() method will later
  90. * report an U_ILLEGAL_ARGUMENT_ERROR.
  91. *
  92. * <p>This method clears the internal UErrorCode.
  93. *
  94. * @param tag the language tag, defined as
  95. * [unicode_locale_id](http://www.unicode.org/reports/tr35/tr35.html#unicode_locale_id).
  96. * @return This builder.
  97. * @stable ICU 64
  98. */
  99. LocaleBuilder& setLanguageTag(StringPiece tag);
  100. /**
  101. * Sets the language. If <code>language</code> is the empty string, the
  102. * language in this <code>LocaleBuilder</code> is removed. Otherwise, the
  103. * <code>language</code> must be well-formed, or else the build() method will
  104. * later report an U_ILLEGAL_ARGUMENT_ERROR.
  105. *
  106. * <p>The syntax of language value is defined as
  107. * [unicode_language_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_language_subtag).
  108. *
  109. * @param language the language
  110. * @return This builder.
  111. * @stable ICU 64
  112. */
  113. LocaleBuilder& setLanguage(StringPiece language);
  114. /**
  115. * Sets the script. If <code>script</code> is the empty string, the script in
  116. * this <code>LocaleBuilder</code> is removed.
  117. * Otherwise, the <code>script</code> must be well-formed, or else the build()
  118. * method will later report an U_ILLEGAL_ARGUMENT_ERROR.
  119. *
  120. * <p>The script value is a four-letter script code as
  121. * [unicode_script_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_script_subtag)
  122. * defined by ISO 15924
  123. *
  124. * @param script the script
  125. * @return This builder.
  126. * @stable ICU 64
  127. */
  128. LocaleBuilder& setScript(StringPiece script);
  129. /**
  130. * Sets the region. If region is the empty string, the region in this
  131. * <code>LocaleBuilder</code> is removed. Otherwise, the <code>region</code>
  132. * must be well-formed, or else the build() method will later report an
  133. * U_ILLEGAL_ARGUMENT_ERROR.
  134. *
  135. * <p>The region value is defined by
  136. * [unicode_region_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_region_subtag)
  137. * as a two-letter ISO 3166 code or a three-digit UN M.49 area code.
  138. *
  139. * <p>The region value in the <code>Locale</code> created by the
  140. * <code>LocaleBuilder</code> is always normalized to upper case.
  141. *
  142. * @param region the region
  143. * @return This builder.
  144. * @stable ICU 64
  145. */
  146. LocaleBuilder& setRegion(StringPiece region);
  147. /**
  148. * Sets the variant. If variant is the empty string, the variant in this
  149. * <code>LocaleBuilder</code> is removed. Otherwise, the <code>variant</code>
  150. * must be well-formed, or else the build() method will later report an
  151. * U_ILLEGAL_ARGUMENT_ERROR.
  152. *
  153. * <p><b>Note:</b> This method checks if <code>variant</code>
  154. * satisfies the
  155. * [unicode_variant_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_variant_subtag)
  156. * syntax requirements, and normalizes the value to lowercase letters. However,
  157. * the <code>Locale</code> class does not impose any syntactic
  158. * restriction on variant. To set an ill-formed variant, use a Locale constructor.
  159. * If there are multiple unicode_variant_subtag, the caller must concatenate
  160. * them with '-' as separator (ex: "foobar-fibar").
  161. *
  162. * @param variant the variant
  163. * @return This builder.
  164. * @stable ICU 64
  165. */
  166. LocaleBuilder& setVariant(StringPiece variant);
  167. /**
  168. * Sets the extension for the given key. If the value is the empty string,
  169. * the extension is removed. Otherwise, the <code>key</code> and
  170. * <code>value</code> must be well-formed, or else the build() method will
  171. * later report an U_ILLEGAL_ARGUMENT_ERROR.
  172. *
  173. * <p><b>Note:</b> The key ('u') is used for the Unicode locale extension.
  174. * Setting a value for this key replaces any existing Unicode locale key/type
  175. * pairs with those defined in the extension.
  176. *
  177. * <p><b>Note:</b> The key ('x') is used for the private use code. To be
  178. * well-formed, the value for this key needs only to have subtags of one to
  179. * eight alphanumeric characters, not two to eight as in the general case.
  180. *
  181. * @param key the extension key
  182. * @param value the extension value
  183. * @return This builder.
  184. * @stable ICU 64
  185. */
  186. LocaleBuilder& setExtension(char key, StringPiece value);
  187. /**
  188. * Sets the Unicode locale keyword type for the given key. If the type
  189. * StringPiece is constructed with a nullptr, the keyword is removed.
  190. * If the type is the empty string, the keyword is set without type subtags.
  191. * Otherwise, the key and type must be well-formed, or else the build()
  192. * method will later report an U_ILLEGAL_ARGUMENT_ERROR.
  193. *
  194. * <p>Keys and types are converted to lower case.
  195. *
  196. * <p><b>Note</b>:Setting the 'u' extension via {@link #setExtension}
  197. * replaces all Unicode locale keywords with those defined in the
  198. * extension.
  199. *
  200. * @param key the Unicode locale key
  201. * @param type the Unicode locale type
  202. * @return This builder.
  203. * @stable ICU 64
  204. */
  205. LocaleBuilder& setUnicodeLocaleKeyword(
  206. StringPiece key, StringPiece type);
  207. /**
  208. * Adds a unicode locale attribute, if not already present, otherwise
  209. * has no effect. The attribute must not be empty string and must be
  210. * well-formed or U_ILLEGAL_ARGUMENT_ERROR will be set to status
  211. * during the build() call.
  212. *
  213. * @param attribute the attribute
  214. * @return This builder.
  215. * @stable ICU 64
  216. */
  217. LocaleBuilder& addUnicodeLocaleAttribute(StringPiece attribute);
  218. /**
  219. * Removes a unicode locale attribute, if present, otherwise has no
  220. * effect. The attribute must not be empty string and must be well-formed
  221. * or U_ILLEGAL_ARGUMENT_ERROR will be set to status during the build() call.
  222. *
  223. * <p>Attribute comparison for removal is case-insensitive.
  224. *
  225. * @param attribute the attribute
  226. * @return This builder.
  227. * @stable ICU 64
  228. */
  229. LocaleBuilder& removeUnicodeLocaleAttribute(StringPiece attribute);
  230. /**
  231. * Resets the builder to its initial, empty state.
  232. * <p>This method clears the internal UErrorCode.
  233. *
  234. * @return this builder
  235. * @stable ICU 64
  236. */
  237. LocaleBuilder& clear();
  238. /**
  239. * Resets the extensions to their initial, empty state.
  240. * Language, script, region and variant are unchanged.
  241. *
  242. * @return this builder
  243. * @stable ICU 64
  244. */
  245. LocaleBuilder& clearExtensions();
  246. /**
  247. * Returns an instance of <code>Locale</code> created from the fields set
  248. * on this builder.
  249. * If any set methods or during the build() call require memory allocation
  250. * but fail U_MEMORY_ALLOCATION_ERROR will be set to status.
  251. * If any of the fields set by the setters are not well-formed, the status
  252. * will be set to U_ILLEGAL_ARGUMENT_ERROR. The state of the builder will
  253. * not change after the build() call and the caller is free to keep using
  254. * the same builder to build more locales.
  255. *
  256. * @return a new Locale
  257. * @stable ICU 64
  258. */
  259. Locale build(UErrorCode& status);
  260. #ifndef U_HIDE_DRAFT_API
  261. /**
  262. * Sets the UErrorCode if an error occurred while recording sets.
  263. * Preserves older error codes in the outErrorCode.
  264. * @param outErrorCode Set to an error code that occurred while setting subtags.
  265. * Unchanged if there is no such error or if outErrorCode
  266. * already contained an error.
  267. * @return TRUE if U_FAILURE(outErrorCode)
  268. * @draft ICU 65
  269. */
  270. UBool copyErrorTo(UErrorCode &outErrorCode) const;
  271. #endif /* U_HIDE_DRAFT_API */
  272. private:
  273. friend class LocaleMatcher::Result;
  274. void copyExtensionsFrom(const Locale& src, UErrorCode& errorCode);
  275. UErrorCode status_;
  276. char language_[9];
  277. char script_[5];
  278. char region_[4];
  279. CharString *variant_; // Pointer not object so we need not #include internal charstr.h.
  280. icu::Locale *extensions_; // Pointer not object. Storage for all other fields.
  281. };
  282. U_NAMESPACE_END
  283. #endif /* U_SHOW_CPLUSPLUS_API */
  284. #endif // __LOCALEBUILDER_H__