(chibi locale)

A lightweight library for representing locale information and serializing to and from strings. A Locale record is an immutable object representing the information in IETF BCP 47. This can be used for distinctions necessary in written and spoken language, but does not provide the full locale information specified by POSIX LC_* variables such as collation and numeric and time formatting, except for reasonable defaults that can be inferred from the region.


















Useful predefined locales representing just a language with no other fields, except for locale:root which is the empty locale.

(make-locale language [region variant script extensions])

Returns an immutable locale object for the given language, with optional region, variant, script and extensions. The language should be a 2 or 3 letter symbol such as 'en or 'ain. The region is an ISO 3166 code, either an uppercased 2 letter symbol such as 'US or 'FR or a 3 digit code from ISO 3166-1 or UN M49 such as 029 (Caribbean). The variant is a 5-8 letter language variant such as 'polyton for Polytonic (Ancient) Greek. The script is a 4 letter titlecased symbol such as 'Latn (Latin), 'Cyrl (Cyrillic), or 'Jpan (Han + Hiragana + Katakana). The extensions are an alist whose keys are single letter symbols (currently only 'u and 'x are defined) with subtag string values. The implementation may intern or otherwise coalesce locale objects with the same values. Note the order of the arguments differs from the serialized string representation (moving script after region and variant) to make the common case construction more convenient. Example:
(locale->string (make-locale 'ja 'JP))
=> "ja-JP"

(locale= a b)

Returns #t iff all components of a and b are equal.

(locale-includes? a b)

Returns #t iff a includes b, i.e. it a is a more general locale, differing only in components specified in b which are unspecific in a.

(locale-generalize locale)

Returns the next more general locale than locale by ablating one of its components, or #f if locale can't be made more general (i.e. is already locale:root). The order of generalization follows that of the serialized string representation, first removing the extensions if present, then the variant, then the region, then script.

(string->locale str)

Returns the Locale record corresponding to the given serialized string representation, signalling an error on invalid format. Allows underscores as well as hyphens as tag separators. Note that the LANG environment variable should not be passed to this directly as it typically has a .<encoding> suffix. Example:
(locale-language (string->locale "ja-JP"))
=> ja

(locale->string locale)

Returns the serialized string representation of locale.