RFR: 8357275: Locale.Builder.setLanguageTag should explicitly state extlangs are allowed

Justin Lu jlu at openjdk.org
Mon May 19 23:55:51 UTC 2025


On Mon, 19 May 2025 23:41:25 GMT, Naoto Sato <naoto at openjdk.org> wrote:

>> It is not clear that `Locale.Builder.setLanguageTag(String)` accepts _extlang_ subtags in the input as well as what behavior occurs. Additionally, both this method and `Locale.forLanguageTag(String)` should mention their behavior when more than three _extlang_ subtags are provided. This PR clarifies the lack of context in the specification.
>
> src/java.base/share/classes/java/util/Locale.java line 2792:
> 
>> 2790:          * tag). {@code languageTag} may contain up to three extlang subtags.
>> 2791:          + For such occurrences, the first extlang subtag is used as the language,
>> 2792:          * and the primary language subtag and other extlang subtags are ignored.
> 
> Should we throw an exeption here? I had an impression that the new description actually contradicts to the previous sentense, which asserts exception on ill-formed tag.

An exception is thrown when more than three extlang subtags are provided (ill-formed). The new wording is when an allowed amount of extlang subtags are provided (not ill-formed). For example,

`new Locale.Builder().setLanguageTag("zh-yue-gan-cmn-czh-CN").build()` // throws
`new Locale.Builder().setLanguageTag("zh-yue-gan-cmn-czh-CN").build()` // ==> yue_CN

i.e. if one to three extlang subtags occur, discard extras. If more than 3 occur, then it is ill-formed, and the method is already specified to throw on ill-formed tags.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/25309#discussion_r2096619811


More information about the core-libs-dev mailing list