trptr.java-wrapper.locale

Clojure wrapper for java.util.Locale.

The main fn: locale.

Note: wherever a Java method requires a Locale object, not only such an object, but also a “locale specification” is accepted. In such cases the Java method (called by the Clojure wrapper) will get the Locale object “specified” by the “specification”. This means, that (unless we pass a Locale object already to the Clojure wrapper), the needed Locale object is created “on the fly”, “behind the scenes”, inside the Clojure wrapper.

Below are the possibilities for such “locale specifications” and their interpretations.

  • nil
    In this case the Locale object, “specified” by this “specification” is the one returned by Java’s Locale.getDefault().

  • a Java Locale object
    In this case the resulting Locale object is the same as the “specification”.

  • a map with keys corresponding to the Locale.Builder class’ setXXX instance methods, i.e. :language, :region, :script, :variant, etc.
    The resulting Locale object is the one returned by a corresponding build call. See examples at build.

  • a keyword or string
    In this case the “specification” is splitted at “-” and "_" characters;
    the resulting Locale object is the one returned by make-locale applied on the result of the splitting.

For more info see locale.

available-locales

Wrapper of the Java method ‘getAvailableLocales’, as a set.
Can be used to check e.g. if a freshly made Locale object is ‘valid’ on the system:
(available-locales (locale :-an)) is usually nil, while there is a good chance that e.g. (available-locales (locale :en)) is truethy…

build

(build {:keys [language language-tag locale region script variant]})

Wrapper of Java’s Locale.Builder/build.

Notes:
* sets locale first (if any), then the others. So the properties of locale might be overwritten in the result.
* language and region can be strings or keywords and are case insensitive.
* use empty strings for the removal of settings.

Examples:

(get-display-country (build {:region "029"})) ;; → Caribbean
(get-display-script {:script "Arab"}) ;; → Arabic
(build {:language-tag "de-POSIX-x-URP-lvariant-Abc-Def"})
;; → #object[java.util.Locale 0x68230234 "de__POSIX_Abc_Def_#x-urp"]

See also to-language-tag!

categories

Java Locale.Category constants in a map with keys:locale.category/display and :locale.category/format.

country-code

Turns its argument into a “normalized” country code string.

for-language-tag

(for-language-tag language-tag)

Direct wrapper of the Java method. See also to-language-tag!
Example: (for-language-tag "fr-FR")#object[java.util.Locale 0x163fd507 "fr_FR"].

get-country

(get-country loc__3244__auto__)

Wrapper of the Java method getCountry, but accepts “locale specifications” as well (not only Locale objects). See the docstring of this ns (above), for the description of “locale specifications”.

get-default

(get-default)(get-default locale-category)

Direct wrapper of the Java method.
locale-category is a value from the map categories.

get-display-country

(get-display-country loc__3260__auto__)(get-display-country loc__3260__auto__ in-locale__3261__auto__)

Wrapper of the Java method getDisplayCountry, but accepts “locale specifications” as well (not only Locale objects). See the docstring of this ns (above), for the description of “locale specifications”.

get-display-language

(get-display-language loc__3260__auto__)(get-display-language loc__3260__auto__ in-locale__3261__auto__)

Wrapper of the Java method getDisplayLanguage, but accepts “locale specifications” as well (not only Locale objects). See the docstring of this ns (above), for the description of “locale specifications”.

get-display-name

(get-display-name loc__3260__auto__)(get-display-name loc__3260__auto__ in-locale__3261__auto__)

Wrapper of the Java method getDisplayName, but accepts “locale specifications” as well (not only Locale objects). See the docstring of this ns (above), for the description of “locale specifications”.

get-display-script

(get-display-script loc__3260__auto__)(get-display-script loc__3260__auto__ in-locale__3261__auto__)

Wrapper of the Java method getDisplayScript, but accepts “locale specifications” as well (not only Locale objects). See the docstring of this ns (above), for the description of “locale specifications”.

get-display-variant

(get-display-variant loc__3260__auto__)(get-display-variant loc__3260__auto__ in-locale__3261__auto__)

Wrapper of the Java method getDisplayVariant, but accepts “locale specifications” as well (not only Locale objects). See the docstring of this ns (above), for the description of “locale specifications”.

get-iso-3-country

(get-iso-3-country loc)

Wrapper of the Java method getIso3Country, but accepts “locale specifications” as well (not only Locale objects). See the docstring of this ns (above), for the description of “locale specifications”.
Note: if the argument is a string or a keyword, then the country comes from the second subtag. So use e.g. :-an or “-an” instead of :an or “an”.

get-iso-3-language

(get-iso-3-language loc)

Wrapper of the Java method getIso3Language, but accepts “locale specifications” as well (not only Locale objects). See the docstring of this ns (above), for the description of “locale specifications”.

get-iso-countries

Direct wrapper of the Java method. Consider using iso-countries instead.

get-iso-languages

Direct wrapper of the Java method. Consider using iso-languages instead.

get-language

(get-language loc__3244__auto__)

Wrapper of the Java method getLanguage, but accepts “locale specifications” as well (not only Locale objects). See the docstring of this ns (above), for the description of “locale specifications”.

get-script

(get-script loc__3244__auto__)

Wrapper of the Java method getScript, but accepts “locale specifications” as well (not only Locale objects). See the docstring of this ns (above), for the description of “locale specifications”.

get-variant

(get-variant loc__3244__auto__)

Wrapper of the Java method getVariant, but accepts “locale specifications” as well (not only Locale objects). See the docstring of this ns (above), for the description of “locale specifications”.

has-extensions

(has-extensions loc__3244__auto__)

Wrapper of the Java method hasExtensions, but accepts “locale specifications” as well (not only Locale objects). See the docstring of this ns (above), for the description of “locale specifications”.

iso-countries

All Java ISO country codes as Clojure keywords, in a set.

iso-country-kw

Turns its argument into an ISO country code keyword OR nil.

iso-language-kw

Turns its argument into an ISO language code keyword OR nil.

iso-languages

All Java ISO language codes as Clojure keywords, in a set.

language-code

Turns its argument into a “normalized” language code string.

locale

(locale)(locale spec)(locale lang & others)

The main Locale creator API: returns a Java Locale object if called:
* without arguments;
* with 1 “locale specification” (see the docstring of this ns, above);
* a series of proper string or keyword subtags.

Returns the defualt locale in case of no arguments or if its single argument is nil.

Returns its single argument untouched if it is already a Locale object.

If it gets 2 or more arguments, they are passed untouched to make-locale.

If it gets a single map, it is passed untouched to build.

Otherwise it turns the got string or keyword (you choose!) into a series of strings (by split-locale) and then passes the result to make-locale.

Valid spec examples: "fr-ar", "EN_CA-variaNt", :ar--vari2, :-ng, :ru_CN, {:variant "varIAnt"}.

low-kw

Turns its argument into a keyword of lowercase letters.

make-locale

(make-locale lang)(make-locale lang country)(make-locale lang country variant)

Wrapper of java.util.Locale’s constructor: creates a Java Locale object from:
* a language code OR keyword (“en” or :en) and
* an optional country code OR keyword (“DE” or :de) and
* an optional vendor-specific variant code.
Case insensitive.

predef-locales

The constants from the Java class’ static fields in a map with keys :locale/english, :locale/uk, etc. Not all Java constants are wrapped yet.

set-default

(set-default new-locale)(set-default locale-category new-locale)

Wrapper of the Java method setDefault, but accepts “locale specifications” as well (not only Locale objects). See the docstring of this ns (above), for the description of “locale specifications”.

split-locale

Accepts a string or a keyword and returns a vector of strings created from the argument’s parts.
The argument’s parts must be separated by either a dash (“-”) or an underscore ("_").
Keywords’ namespaces are ignored.

strip-extensions

(strip-extensions loc__3244__auto__)

Wrapper of the Java method stripExtensions, but accepts “locale specifications” as well (not only Locale objects). See the docstring of this ns (above), for the description of “locale specifications”.

to-language-tag

(to-language-tag loc)

Wrapper of the Java method, but accepts “locale specifications” as well.

Can be used to ‘serialize’ a Locale object as a string, that can later be used to recreate the object: (build {:language-tag (to-language-tag <some-locale>)}) should result <some-locale>.

See also Java doc for Compatibility of Locale: "toLanguageTag cannot represent the state of locales whose language, country, or variant do not conform to BCP 47.

[…] it is recommended that clients migrate away from constructing non-conforming locales and use the forLanguageTag and Locale.Builder APIs instead.

Clients desiring a string representation of the complete locale can then always rely on toLanguageTag for this purpose."

unicode-locale-extension

The Java constant.