1 Module locale
(require locale) | package: racket-locale |
This module has functions for managing locale strings, for getting and setting locales and retrieving locale-specific conventions. It is important to understand that there isn’t a single locale value that encompasses all purposes, there are a set of categories such as date/time handling, number and currency formatting, and string/character encoding, each of which may have a separate locale identified. In this module there are functions of the form get-category-locale and set-category-locale for each supported category.
> (require locale) > (set-user-preferred-locale) "C"
> (get-locale) "C"
> (set-numeric-locale "C") "C"
> (get-locale) "C"
> (set-locale (make-locale-string "en" "GB" #:code-page (normalize-code-page 'utf-8))) #f
1.1 Locale Strings
procedure
(locale-string? str) → boolean?
str : string?
locale := "C" | "POSIX" ; well-known portable locales |
| (language) ("_" territory)? ("." code-page)? ("@" modifier) |
|
language := [a-z]{2,3} ; ISO 639 language code |
|
territory := [A-Z]{2,3} ; ISO 3166 country code |
|
code-page := [a-zA-Z0-9\-]+ |
|
modifier := [a-zA-Z0-9\-]+ |
The locale strings "C" and "POSIX" are defined for all systems and have well-known behavior.
procedure
(make-locale-string language [ country #:code-page code-page #:options modifier]) → (or/c string? #f) language : string? country : string? = "" code-page : string? = "" modifier : string? = ""
procedure
(normalize-code-page code-page) → string?
code-page : (or/c 'utf-8 'ascii 'iso-8859-1 'iso-8859-15)
> (require locale) > (get-known-locales) '#hash()
1.2 Locale Accessors
procedure
(set-minimal-locale) → (or/c locale-string? #f)
procedure
(set-user-preferred-locale) → (or/c locale-string? #f)
procedure
(get-locale) → (or/c locale-string? #f)
> (require locale) > (set-minimal-locale) "C"
> (set-numeric-locale (make-locale-string "en" "US" #:code-page (normalize-code-page 'utf-8))) #f
> (get-locale) "C"
procedure
(set-locale locale) → (or/c locale-string? #f)
locale : locale-string?
procedure
(get-collation-locale) → (or/c locale-string? #f)
procedure
(set-collation-locale locale) → (or/c locale-string? #f)
locale : locale-string?
procedure
(get-character-type-locale) → (or/c locale-string? #f)
procedure
(set-character-type-locale locale) → (or/c locale-string? #f)
locale : locale-string?
procedure
(get-monetary-locale) → (or/c locale-string? #f)
procedure
(set-monetary-locale locale) → (or/c locale-string? #f)
locale : locale-string?
procedure
(get-numeric-locale) → (or/c locale-string? #f)
procedure
(set-numeric-locale locale) → (or/c locale-string? #f)
locale : locale-string?
procedure
(get-time-locale) → (or/c locale-string? #f)
procedure
(set-time-locale locale) → (or/c locale-string? #f)
locale : locale-string?
procedure
(get-messages-locale) → (or/c locale-string? #f)
procedure
(set-messages-locale locale) → (or/c locale-string? #f)
locale : locale-string?
1.3 Locale Conventions
value
struct
(struct locale ( decimal-point thousands-separator grouping international-currency-symbol currency-symbol monetary-decimal-point monetary-thousands-separator monetary-grouping positive-sign negative-sign international-fractional-digits fractional-digits pos-cs-precedes neg-cs-precedes pos-sep-by-space neg-sep-by-space pos-sign-posn neg-sign-posn international-pos-cs-precedes international-neg-cs-precedes international-pos-sep-by-space international-neg-sep-by-space international-pos-sign-posn international-neg-sign-posn)) decimal-point : string? thousands-separator : string? grouping : string? international-currency-symbol : string? currency-symbol : string? monetary-decimal-point : string? monetary-thousands-separator : string? monetary-grouping : vector? positive-sign : string? negative-sign : string? international-fractional-digits : integer? fractional-digits : integer? pos-cs-precedes : integer? neg-cs-precedes : integer? pos-sep-by-space : integer? neg-sep-by-space : integer? pos-sign-posn : integer? neg-sign-posn : integer? international-pos-cs-precedes : integer? international-neg-cs-precedes : integer? international-pos-sep-by-space : integer? international-neg-sep-by-space : integer? international-pos-sign-posn : integer? international-neg-sign-posn : integer?
decimal-point - Decimal-point separator used for non-monetary quantities.
thousands-separator - Separators used to delimit groups of digits to the left of the decimal point for non-monetary quantities.
- grouping - Specifies the amount of digits that form each of the groups to be separated by thousands-separator separator for non-monetary quantities. This is a vector? of nonnegative-integer? values that may contain different grouping sizes for each successive group starting from the right, each number indicating the amount of digits for the group. If the last number in the vector is grouping-repeats the previous value will be used for all remaining groups. For example, assuming thousands-separator is set to "," and the number to represent is one million (1000000):
with grouping set to '#(3 grouping-repeats), the number would be represented as: 1,000,000
with grouping set to '#(1 2 3 grouping-repeats), the number would be represented as: 1,000,00,0
with grouping set to '#(3 1 grouping-repeats), the number would be represented as: 1,0,0,0,000
grouping-ends indicates that no further grouping is to be performed.
international-currency-symbol - International currency symbol. This is formed by the three-letter ISO-4217 entry code for the currency, like "USD" for U.S.-Dollar or "GBP" for Pound Sterling, followed by the character used to separate this symbol from the monetary quantity.
currency-symbol - Local currency symbol, like "$".
monetary-decimal-point - Decimal-point separator used for monetary quantities.
monetary-thousands-separator - Separators used to delimit groups of digits to the left of the decimal point for monetary quantities.
monetary-grouping - Specifies the amount of digits that form each of the groups to be separated by monetary-thousands-separator separator for monetary quantities. See grouping description above.
positive-sign - Sign to be used for nonnegative (positive or zero) monetary quantities.
negative-sign - Sign to be used for negative monetary quantities.
international-fractional-digits - Same as fractional-digits, but for the international format (instead of the local format).
fractional-digits - Amount of fractional digits to the right of the decimal point for monetary quantities in the local format.
pos-cs-precedes - Whether the currency symbol should precede nonnegative (positive or zero) monetary quantities. If this value is 1, the currency symbol should precede; if it is 0, it should follow.
neg-cs-precedes - Whether the currency symbol should precede negative monetary quantities. If this value is 1, the currency symbol should precede; if it is 0 it should follow.
pos-sep-by-space - Whether a space should appear between the currency symbol and nonnegative (positive or zero) monetary quantities. If this value is 1, a space should appear; if it is 0 it should not.
neg-sep-by-space - Whether a space should appear between the currency symbol and negative monetary quantities. If this value is 1, a space should appear; if it is 0 it should not.
- pos-sign-posn - Position of the sign for nonnegative (positive or zero) monetary quantities:
0 : Currency symbol and quantity surrounded by parentheses.
1 : Sign before the quantity and currency symbol.
2 : Sign after the quantity and currency symbol.
3 : Sign right before currency symbol.
4 : Sign right after currency symbol.
unspecified-sign-posn : unspecified.
neg-sign-posn - Position of the sign for negative monetary quantities. See pos-sign-posn above.
international-pos-cs-precedes - Same as pos-cs-precedes, but for the international format.
international-neg-cs-precedes - Same as neg-cs-precedes, but for the international format.
international-pos-sep-by-space - Same as pos-sep-by-space, but for the international format.
international-neg-sep-by-space - Same as neg-sep-by-space, but for the international format.
international-pos-sign-posn - Same as pos-sign-posn, but for the international format.
international-neg-sign-posn - Same as neg-sign-posn, but for the international format.
For example, given the currency value 1.25 the table below shows the effect of the various formatting options above.
pos-sep-by-space = | 0 | 1 | 2 | |
pos-cs-precedes = 0 | pos-sign-posn = 0 | (1.25$) | (1.25 $) | (1.25 $) |
pos-sign-posn = 1 | +1.25$ | +1.25 $ | +1.25 $ | |
pos-sign-posn = 2 | 1.25$+ | 1.25 $+ | 1.25$ + | |
pos-sign-posn = 3 | 1.25+$ | 1.25 +$ | 1.25+ $ | |
pos-sign-posn = 4 | 1.25$+ | 1.25 $+ | 1.25$ + | |
pos-cs-precedes = 1 | pos-sign-posn = 0 | ($1.25) | ($ 1.25) | ($ 1.25) |
pos-sign-posn = 1 | +$1.25 | +$ 1.25 | + $1.25 | |
pos-sign-posn = 2 | $1.25+ | $ 1.25+ | $1.25 + | |
pos-sign-posn = 3 | +$1.25 | +$ 1.25 | + $1.25 | |
pos-sign-posn = 4 | $+1.25 | $+ 1.25 | $ +1.25 |
procedure
> (require locale)
> (set-locale (make-locale-string "en" "GB" #:code-page (normalize-code-page 'utf-8))) #f
> (get-locale-conventions)
(locale
"."
""
'#(0)
""
""
""
""
'#(0)
""
""
127
127
127
127
127
127
127
127
127
127
127
127
127
127)