localebuilder.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // © 2018 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  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 IETF BCP 47 language tag.
  85. * Discards the existing state.
  86. * The empty string causes the builder to be reset, like {@link #clear}.
  87. * Legacy language tags (marked as “Type: grandfathered” in BCP 47)
  88. * are converted to their canonical form before being processed.
  89. * Otherwise, the <code>language tag</code> must be well-formed,
  90. * or else the build() method will later report an U_ILLEGAL_ARGUMENT_ERROR.
  91. *
  92. * <p>This method clears the internal UErrorCode.
  93. *
  94. * @param tag the language tag, defined as IETF BCP 47 language tag.
  95. * @return This builder.
  96. * @stable ICU 64
  97. */
  98. LocaleBuilder& setLanguageTag(StringPiece tag);
  99. /**
  100. * Sets the language. If <code>language</code> is the empty string, the
  101. * language in this <code>LocaleBuilder</code> is removed. Otherwise, the
  102. * <code>language</code> must be well-formed, or else the build() method will
  103. * later report an U_ILLEGAL_ARGUMENT_ERROR.
  104. *
  105. * <p>The syntax of language value is defined as
  106. * [unicode_language_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_language_subtag).
  107. *
  108. * @param language the language
  109. * @return This builder.
  110. * @stable ICU 64
  111. */
  112. LocaleBuilder& setLanguage(StringPiece language);
  113. /**
  114. * Sets the script. If <code>script</code> is the empty string, the script in
  115. * this <code>LocaleBuilder</code> is removed.
  116. * Otherwise, the <code>script</code> must be well-formed, or else the build()
  117. * method will later report an U_ILLEGAL_ARGUMENT_ERROR.
  118. *
  119. * <p>The script value is a four-letter script code as
  120. * [unicode_script_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_script_subtag)
  121. * defined by ISO 15924
  122. *
  123. * @param script the script
  124. * @return This builder.
  125. * @stable ICU 64
  126. */
  127. LocaleBuilder& setScript(StringPiece script);
  128. /**
  129. * Sets the region. If region is the empty string, the region in this
  130. * <code>LocaleBuilder</code> is removed. Otherwise, the <code>region</code>
  131. * must be well-formed, or else the build() method will later report an
  132. * U_ILLEGAL_ARGUMENT_ERROR.
  133. *
  134. * <p>The region value is defined by
  135. * [unicode_region_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_region_subtag)
  136. * as a two-letter ISO 3166 code or a three-digit UN M.49 area code.
  137. *
  138. * <p>The region value in the <code>Locale</code> created by the
  139. * <code>LocaleBuilder</code> is always normalized to upper case.
  140. *
  141. * @param region the region
  142. * @return This builder.
  143. * @stable ICU 64
  144. */
  145. LocaleBuilder& setRegion(StringPiece region);
  146. /**
  147. * Sets the variant. If variant is the empty string, the variant in this
  148. * <code>LocaleBuilder</code> is removed. Otherwise, the <code>variant</code>
  149. * must be well-formed, or else the build() method will later report an
  150. * U_ILLEGAL_ARGUMENT_ERROR.
  151. *
  152. * <p><b>Note:</b> This method checks if <code>variant</code>
  153. * satisfies the
  154. * [unicode_variant_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_variant_subtag)
  155. * syntax requirements, and normalizes the value to lowercase letters. However,
  156. * the <code>Locale</code> class does not impose any syntactic
  157. * restriction on variant. To set an ill-formed variant, use a Locale constructor.
  158. * If there are multiple unicode_variant_subtag, the caller must concatenate
  159. * them with '-' as separator (ex: "foobar-fibar").
  160. *
  161. * @param variant the variant
  162. * @return This builder.
  163. * @stable ICU 64
  164. */
  165. LocaleBuilder& setVariant(StringPiece variant);
  166. /**
  167. * Sets the extension for the given key. If the value is the empty string,
  168. * the extension is removed. Otherwise, the <code>key</code> and
  169. * <code>value</code> must be well-formed, or else the build() method will
  170. * later report an U_ILLEGAL_ARGUMENT_ERROR.
  171. *
  172. * <p><b>Note:</b> The key ('u') is used for the Unicode locale extension.
  173. * Setting a value for this key replaces any existing Unicode locale key/type
  174. * pairs with those defined in the extension.
  175. *
  176. * <p><b>Note:</b> The key ('x') is used for the private use code. To be
  177. * well-formed, the value for this key needs only to have subtags of one to
  178. * eight alphanumeric characters, not two to eight as in the general case.
  179. *
  180. * @param key the extension key
  181. * @param value the extension value
  182. * @return This builder.
  183. * @stable ICU 64
  184. */
  185. LocaleBuilder& setExtension(char key, StringPiece value);
  186. /**
  187. * Sets the Unicode locale keyword type for the given key. If the type
  188. * StringPiece is constructed with a nullptr, the keyword is removed.
  189. * If the type is the empty string, the keyword is set without type subtags.
  190. * Otherwise, the key and type must be well-formed, or else the build()
  191. * method will later report an U_ILLEGAL_ARGUMENT_ERROR.
  192. *
  193. * <p>Keys and types are converted to lower case.
  194. *
  195. * <p><b>Note</b>:Setting the 'u' extension via {@link #setExtension}
  196. * replaces all Unicode locale keywords with those defined in the
  197. * extension.
  198. *
  199. * @param key the Unicode locale key
  200. * @param type the Unicode locale type
  201. * @return This builder.
  202. * @stable ICU 64
  203. */
  204. LocaleBuilder& setUnicodeLocaleKeyword(
  205. StringPiece key, StringPiece type);
  206. /**
  207. * Adds a unicode locale attribute, if not already present, otherwise
  208. * has no effect. The attribute must not be empty string and must be
  209. * well-formed or U_ILLEGAL_ARGUMENT_ERROR will be set to status
  210. * during the build() call.
  211. *
  212. * @param attribute the attribute
  213. * @return This builder.
  214. * @stable ICU 64
  215. */
  216. LocaleBuilder& addUnicodeLocaleAttribute(StringPiece attribute);
  217. /**
  218. * Removes a unicode locale attribute, if present, otherwise has no
  219. * effect. The attribute must not be empty string and must be well-formed
  220. * or U_ILLEGAL_ARGUMENT_ERROR will be set to status during the build() call.
  221. *
  222. * <p>Attribute comparison for removal is case-insensitive.
  223. *
  224. * @param attribute the attribute
  225. * @return This builder.
  226. * @stable ICU 64
  227. */
  228. LocaleBuilder& removeUnicodeLocaleAttribute(StringPiece attribute);
  229. /**
  230. * Resets the builder to its initial, empty state.
  231. * <p>This method clears the internal UErrorCode.
  232. *
  233. * @return this builder
  234. * @stable ICU 64
  235. */
  236. LocaleBuilder& clear();
  237. /**
  238. * Resets the extensions to their initial, empty state.
  239. * Language, script, region and variant are unchanged.
  240. *
  241. * @return this builder
  242. * @stable ICU 64
  243. */
  244. LocaleBuilder& clearExtensions();
  245. /**
  246. * Returns an instance of <code>Locale</code> created from the fields set
  247. * on this builder.
  248. * If any set methods or during the build() call require memory allocation
  249. * but fail U_MEMORY_ALLOCATION_ERROR will be set to status.
  250. * If any of the fields set by the setters are not well-formed, the status
  251. * will be set to U_ILLEGAL_ARGUMENT_ERROR. The state of the builder will
  252. * not change after the build() call and the caller is free to keep using
  253. * the same builder to build more locales.
  254. *
  255. * @return a new Locale
  256. * @stable ICU 64
  257. */
  258. Locale build(UErrorCode& status);
  259. /**
  260. * Sets the UErrorCode if an error occurred while recording sets.
  261. * Preserves older error codes in the outErrorCode.
  262. * @param outErrorCode Set to an error code that occurred while setting subtags.
  263. * Unchanged if there is no such error or if outErrorCode
  264. * already contained an error.
  265. * @return true if U_FAILURE(outErrorCode)
  266. * @stable ICU 65
  267. */
  268. UBool copyErrorTo(UErrorCode &outErrorCode) const;
  269. private:
  270. friend class LocaleMatcher::Result;
  271. void copyExtensionsFrom(const Locale& src, UErrorCode& errorCode);
  272. UErrorCode status_;
  273. char language_[9];
  274. char script_[5];
  275. char region_[4];
  276. CharString *variant_; // Pointer not object so we need not #include internal charstr.h.
  277. icu::Locale *extensions_; // Pointer not object. Storage for all other fields.
  278. };
  279. U_NAMESPACE_END
  280. #endif /* U_SHOW_CPLUSPLUS_API */
  281. #endif // __LOCALEBUILDER_H__