Packages
ex_cldr
0.0.7
2.47.5
2.47.4
2.47.3
2.47.2
2.47.1
2.47.0
2.46.0
2.45.2
2.45.1
2.45.0
2.44.1
2.44.0
2.43.2
2.43.1
2.43.0
2.42.0
2.41.1
2.41.0
2.40.4
2.40.3
2.40.2
2.40.1
2.40.0
2.39.2
2.39.1
2.39.0
2.38.1
2.38.0
2.37.5
2.37.4
2.37.3
retired
2.37.2
2.37.1
2.37.0
2.36.0
2.35.1
2.35.0
retired
2.34.2
2.34.1
2.34.0
2.33.2
2.33.1
2.33.1-rc.0
2.33.0
2.32.1
2.32.0
2.31.0
2.30.0
2.29.0
2.28.0
2.27.1
2.27.0
retired
2.26.3
2.26.2
2.26.1
2.26.0
2.25.0
2.24.2
2.24.1
2.24.0
2.24.0-rc.6
2.24.0-rc.5
2.24.0-rc.4
2.24.0-rc.3
2.24.0-rc.2
2.24.0-rc.1
2.24.0-rc.0
2.23.2
2.23.1
2.23.0
2.22.1
2.22.0
2.21.0
2.20.0
2.20.0-rc.3
2.20.0-rc.2
2.20.0-rc.1
2.20.0-rc.0
2.19.1
2.19.0
2.18.2
2.18.1
2.18.0
2.18.0-rc.0
2.17.2
2.17.1
2.17.0
2.16.2
2.16.1
2.16.1-rc.0
2.16.0
2.15.0
2.14.1
2.14.0
2.14.0-rc.0
2.13.0
2.12.1
2.12.0
2.11.1
2.11.0
2.10.2
2.10.1
2.10.0
retired
2.9.0
2.8.1
2.8.0
2.7.2
2.7.1
2.7.0
2.6.2
2.6.1
2.6.0
2.5.0
2.4.3
2.4.2
2.4.1
2.4.0
2.3.2
2.3.1
2.3.0
2.2.7
2.2.6
2.2.5
2.2.4
2.2.3
2.2.2
2.2.1
2.2.0
2.1.0
2.0.4
2.0.3
2.0.2
2.0.1
2.0.0
2.0.0-rc.3
retired
2.0.0-rc.2
retired
2.0.0-rc.1
retired
2.0.0-rc.0
retired
1.8.2
1.8.1
1.8.0
1.7.1
1.7.0
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.2
1.5.1
1.5.0
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
retired
1.4.0-rc.3
retired
1.4.0-rc.2
retired
1.4.0-rc.1
retired
1.4.0-rc.0
retired
1.3.2
1.3.1
1.3.0
1.2.0
1.1.0
1.0.0
1.0.0-rc.3
retired
1.0.0-rc.2
retired
1.0.0-rc.1
retired
1.0.0-rc.0
retired
0.13.1
0.13.0
0.12.2
retired
0.12.1
retired
0.12.0
retired
0.11.0
retired
0.10.0
retired
0.9.0
retired
0.8.3
retired
0.8.2
retired
0.8.1
retired
0.8.0
retired
0.7.0
retired
0.6.2
retired
0.6.1
retired
0.6.0
retired
0.5.2
retired
0.5.1
retired
0.5.0
retired
0.4.2
retired
0.4.1
retired
0.4.0
retired
0.3.0
retired
0.2.1
retired
0.2.0
retired
0.1.3
retired
0.1.2
retired
0.1.1
retired
0.1.0
retired
0.0.20
retired
0.0.19
retired
0.0.18
retired
0.0.17
retired
0.0.16
retired
0.0.15
retired
0.0.14
retired
0.0.13
retired
0.0.12
retired
0.0.11
retired
0.0.10
retired
0.0.8
retired
0.0.7
retired
0.0.6
retired
0.0.5
retired
0.0.4
retired
0.0.3
retired
Common Locale Data Repository (CLDR) functions for Elixir to localize and format numbers, dates, lists, messages, languages, territories and units with support for over 700 locales for internationalized (i18n) and localized (L10N) applications.
Retired package: Deprecated
Current section
Files
Jump to
Current section
Files
lib/mix/support/consolidate.ex
defmodule Cldr.Consolidate do
@moduledoc """
Consolidates all locale-specific information from the CLDR repository into
one locale-specific file.
"""
@cldr_modules ["number_formats", "list_formats", "currencies",
"number_systems", "number_symbols", "minimum_grouping_digits",
"rbnf"
]
@doc """
Identifies the top level keys in the consolidated locale file.
These keys represent difference dimensions of content in the CLDR
repository and serve three purposes:
1. To structure the content in the locale file
2. To provide a rudimentary way to validate that some json represents a
valid locale file
3. To all conditional inclusion of CLDR content at compile time to help
manage memory footprint. This capability is not yet built into `Cldr`.
"""
@spec required_modules :: [String.t]
def required_modules do
@cldr_modules
end
@doc """
Returns the directory where the downloaded CLDR repository files
are stored.
"""
def download_data_dir do
Path.join(Cldr.Config.cldr_home, "data")
end
@doc """
Returns the directory where the consolidated `Cldr` content is stored.
We store the consolidated files in the `./priv/cldr` directory which
is part of the github repo and therefore available for download.
However only the "en" locale is packaged in hex and any other configured
locales will be downloaded when the client app is compiled.
"""
def consolidated_output_dir do
Cldr.Install.cldr_data_dir()
end
@doc """
Returns the directory where the locale-specific json files are stored.
"""
def consolidated_locales_dir do
Path.join(consolidated_output_dir(), "/locales")
end
@doc """
Consolidates all available CLDR locale-specific json files into a set of
locale-specific json files, one per locale.
Also formats non-locale-specific CLDR data that is core to `Cldr`
operation.
"""
@spec consolidate_locales :: :ok
def consolidate_locales do
alias Experimental.Flow
ensure_output_dir_exists!(consolidated_output_dir())
ensure_output_dir_exists!(consolidated_locales_dir())
save_plurals()
save_number_systems()
save_locales()
all_locales()
|> Flow.from_enumerable()
|> Flow.map(&consolidate_locale/1)
|> Enum.to_list
:ok
end
@doc """
Consolidates known locales as defined by `Cldr.known_locales/0`.
"""
@spec consolidate_known_locales :: :ok
def consolidate_known_locales do
alias Experimental.Flow
Cldr.known_locales()
|> Flow.from_enumerable()
|> Flow.map(&consolidate_locale/1)
|> Enum.to_list
:ok
end
@doc """
Consolidates one locale.
* `locale` is any locale defined by `Cldr.all_locales/0`
"""
def consolidate_locale(locale) do
cldr_locale_specific_dirs()
|> consolidate_locale_content(locale)
|> level_up_locale(locale)
|> Cldr.Map.underscore_keys
|> normalize_content(locale)
|> Map.take(@cldr_modules)
|> Cldr.Map.atomize_keys
|> save_locale(locale)
end
defp consolidate_locale_content(locale_dirs, locale) do
locale_dirs
|> Enum.map(&locale_specific_content(locale, &1))
|> merge_maps
end
defp normalize_content(content, locale) do
Cldr.Normalize.Number.normalize(content, locale)
|> Cldr.Normalize.Currency.normalize(locale)
|> Cldr.Normalize.List.normalize(locale)
|> Cldr.Normalize.NumberSystem.normalize(locale)
|> Cldr.Normalize.Rbnf.normalize(locale)
end
# Remove the top two levels of the map since they add nothing
# but more levels :-)
defp level_up_locale(content, locale) do
get_in(content, ["main", locale])
end
defp save_locale(content, locale) do
output_path = Path.join(consolidated_locales_dir(), "#{locale}.json")
File.write!(output_path, Poison.encode!(content))
end
defp merge_maps([file_1]) do
file_1
end
defp merge_maps([file_1, file_2]) do
Cldr.Map.deep_merge(file_1, file_2)
end
defp merge_maps([file | rest]) do
Cldr.Map.deep_merge(file, merge_maps(rest))
end
defp locale_specific_content(locale, directory) do
dir = Path.join(directory, ["main/", locale])
dir
|> File.ls!
|> Enum.map(&Path.join(dir, &1))
|> Enum.map(&File.read!(&1))
|> Enum.map(&Poison.decode!(&1))
|> merge_maps
end
defp cldr_locale_specific_dirs do
cldr_directories()
|> Enum.filter(&locale_specific_dir?/1)
end
defp locale_specific_dir?(filename) do
String.ends_with?(filename, "-full")
end
defp cldr_directories do
download_data_dir()
|> File.ls!
|> Enum.filter(&cldr_dir?/1)
|> Enum.map(&Path.join(download_data_dir(), &1))
end
defp cldr_dir?("common") do
true
end
defp cldr_dir?(filename) do
String.starts_with?(filename, "cldr-")
end
defp ensure_output_dir_exists!(dir) do
case File.mkdir(dir) do
:ok ->
:ok
{:error, :eexist} ->
:ok
{:error, code} ->
raise RuntimeError,
message: "Couldn't create #{dir}: #{inspect code}"
end
end
def all_locales() do
download_data_dir()
|> Path.join(["cldr-core", "/availableLocales.json"])
|> File.read!
|> Poison.decode!
|> get_in(["availableLocales", "full"])
end
defp save_locales do
path = Path.join(consolidated_output_dir(), "available_locales.json")
save_file(all_locales(), path)
end
defp save_plurals do
cardinal = Path.join(download_data_dir(), ["cldr-core", "/supplemental", "/plurals.json"])
|> File.read!
|> Poison.decode!
|> get_in(["supplemental", "plurals-type-cardinal"])
ordinal = Path.join(download_data_dir(), ["cldr-core", "/supplemental", "/ordinals.json"])
|> File.read!
|> Poison.decode!
|> get_in(["supplemental", "plurals-type-ordinal"])
content = %{cardinal: cardinal, ordinal: ordinal}
save_file(content, Path.join(consolidated_output_dir(), "plural_rules.json"))
end
defp save_number_systems do
Path.join(download_data_dir(), ["cldr-core", "/supplemental", "/numberingSystems.json"])
|> File.read!
|> Poison.decode!
|> get_in(["supplemental", "numberingSystems"])
|> remove_leading_underscores
|> save_file(Path.join(consolidated_output_dir(), "number_systems.json"))
end
defp remove_leading_underscores(%{} = systems) do
Enum.map(systems, fn {k, v} ->
{String.replace_prefix(k, "_", ""), remove_leading_underscores(v)} end)
|> Enum.into(%{})
end
defp remove_leading_underscores(v) do
v
end
defp save_file(content, path) do
File.write!(path, Poison.encode!(content))
end
end