Configuring Cldr
Cldr
attempts to maximise runtime performance at the expense of additional compile time. Where possible Cldr
will create functions to encapsulate data at compile time. To perform these optimizations for all 511 locales known to Cldr wouldn’t be an effective use of your time or your computers. So Cldr
requires that you configure the locales you want to use. You can do this in your mix.exs
by specifying the locales you want to configure or by telling Cldr
about a Gettext
module you may already have configured - in which case Cldr
will configure whatever locales you have configured in Gettext
.
Here’s an example configuration that uses all of the available configuration keys.
config :cldr,
default_locale: "en",
locales: ["fr", "en", "bs", "si", "ak", "th"],
gettext: MyApp.Gettext,
data_dir: "./priv/cldr"
Configuration Keys
The configuration keys available for Cldr
are:
default_locale
specifies the default locale to be used if none has been set byCldr.put_locale/2
and none has been set in a configuredGettext
module. The default locale in case no other locale has been set is"en"
. Default locale calculated by:- If set by the
:default_locale
key, then this is the priority - If no
:default_locale
key, the a configuredGettext
default locale is chosen - If no
:default_locale
key is specified and noGettext
module is configured, or is configured but has not default set, then the default locale will be"en"
- If set by the
locales
: Defines what locales will be configured inCldr
. Only these locales will be available and an exceptionCldr.UnknownLocaleError
will be raised if there is an attempt to use an unknown locale. This is the same behaviour asGettext
. Locales are configured as a list of binaries (strings). For convenince it is possible to use wildcard matching of locales which is particulalry helpful when there are many regional variances of a single language locale. For example, there are over 100 regional variants of the “en” locale in CLDR. A wildcard locale is detected by the presence of.
,[
,*
and+
in the locale string. This locale is then matched using the pattern as aregex
to match against all available locales. For example:config :cldr, default_locale: "en", locales: ["en-*", "fr"]
will configure all locales that start with
en-
and the localefr
. There is one additional setting which is:all
which will configure all 514 locales. This is highly discouraged since it will take minutes to compile your project and will consume more memory than you really want. This setting is there to aid in running the test suite. Really, don’t use this setting.gettext
: configuresCldr
to use aGettext
module as a source of defining what locales you want to configure. SinceGettext
uses locales with an ‘_‘ in them andCldr
uses a ‘-‘,Cldr
will transliterate locale names fromGettext
into theCldr
canonical form.data_dir
: indicates where downloaded locale files will be stored. The default is:code.priv_dir(:ex_cldr)
.