Packages
ex_cldr
0.1.2
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/cldr/number/transliterate.ex
defmodule Cldr.Number.Transliterate do
@moduledoc """
Transliteration for digits and separators.
Transliterating a string is an expensive business. First the string has to
be exploded into its component graphemes. Then for each grapheme we have
to map to the equivalent in the other `{locale, number_system}`. Then we
have to reassemble the string.
Effort is made to short circuit where possible. Transliteration is not
required for any `{locale, number_system}` that is the same as `{"en",
"latn"}` since the implementation usese this for the placeholders during
formatting already. When short circuiting is possible (typically the en-*
locales with "latn" number_system - the total number of short circuited
locales is 211 of the 511 in CLDR) the overall number formatting is twice as
fast than when formal transliteration is required.
"""
alias Cldr.Number.System
alias Cldr.Number.Symbol
alias Cldr.Number.Format.Compiler
@doc """
Transliterates from latin digits to another number system's digits.
Transliterates the latin digits 0..9 to their equivalents in
another number system. Also transliterates the decimal and grouping
separators as well as the plus and minus sign. Any other character
in the string will be returned "as is".
* `sequence` is the string to be transliterated.
* `locale` is any known locale, defaulting to `Cldr.get_locale()`.
* `number_system` is any known number system. If expressed as a `string` it
is the actual name of a known number system. If epressed as an `atom` it is
used as a key to look up a number system for the locale (the usual keys are
`:default` and `:native` but :traditional and :finance are also part of the
standard). See `Cldr.Number.System.number_systems_for/1` for a locale to
see what number system types are defined. The default is `:default`.
For available number systems see `Cldr.Number.System.number_systems/0`
and `Cldr.Number.System.number_systems_for/1`. Also see
`Cldr.Number.Symbol.number_symbols_for/1`.
## Examples
iex> Cldr.Number.System.transliterate("123556")
"123556"
iex> Cldr.Number.System.transliterate("123,556.000", "fr", :default)
"123 556,000"
iex> Cldr.Number.System.transliterate("123556", "th", :default)
"123556"
iex> Cldr.Number.System.transliterate("123556", "th", "thai")
"๑๒๓๕๕๖"
iex> Cldr.Number.System.transliterate("123556", "th", :native)
"๑๒๓๕๕๖"
iex> Cldr.Number.System.transliterate("Some number is: 123556", "th", "thai")
"Some number is: ๑๒๓๕๕๖"
iex(5)> Cldr.Number.System.transliterate(12345, "th", "thai")
"๑๒๓๔๕"
iex(6)> Cldr.Number.System.transliterate(12345.0, "th", "thai")
"๑๒๓๔๕.๐"
iex(7)> Cldr.Number.System.transliterate(Decimal.new(12345.0), "th", "thai")
"๑๒๓๔๕.๐"
"""
@spec transliterate(String.t | number, Cldr.locale, String.t) :: String.t
def transliterate(sequence, locale \\ Cldr.get_locale(), number_system \\ System.default_number_system_type)
# No transliteration required when the digits and separators as the same
# as the ones we use in formatting.
Enum.each System.number_systems_like("en", :latn), fn {locale, system} ->
def transliterate(sequence, unquote(locale), unquote(system)) do
sequence
end
end
# Translate the number system type to a system and invoke the real
# transliterator
for locale <- Cldr.known_locales() do
for {system_type, number_system} <- Cldr.Number.System.number_systems_for(locale) do
def transliterate(sequence, unquote(locale), unquote(system_type)) do
transliterate(sequence, unquote(locale), unquote(number_system))
end
end
end
# We can only transliterate if the target {locale, number_system} has defined
# digits. Some systems don't have digits, just rules.
for {name, %{digits: _digits}} <- System.systems_with_digits do
def transliterate(sequence, locale, number_system = unquote(name)) do
sequence
|> String.graphemes
|> Enum.map(&transliterate_char(&1, locale, number_system))
|> List.to_string
end
end
# Functions to transliterate the symbols
for locale <- Cldr.known_locales,
name <- System.number_system_names_for(locale)
do
if Symbol.number_symbols_for(locale, name) do
# Mapping for the grouping separator
@group Symbol.number_symbols_for(locale, name).group
defp transliterate_char(unquote(Compiler.placeholder(:group)), unquote(locale), unquote(name)) do
@group
end
# Mapping for the decimal separator
@decimal Symbol.number_symbols_for(locale, name).decimal
defp transliterate_char(unquote(Compiler.placeholder(:decimal)), unquote(locale), unquote(name)) do
@decimal
end
# Mapping for the exponent
@exponent Symbol.number_symbols_for(locale, name).exponential
defp transliterate_char(unquote(Compiler.placeholder(:exponent)), unquote(locale), unquote(name)) do
@exponent
end
# Mapping for the plus sign
@plus Symbol.number_symbols_for(locale, name).plus_sign
defp transliterate_char(unquote(Compiler.placeholder(:plus)), unquote(locale), unquote(name)) do
@plus
end
# Mapping for the minus sign
@minus Symbol.number_symbols_for(locale, name).minus_sign
defp transliterate_char(unquote(Compiler.placeholder(:minus)), unquote(locale), unquote(name)) do
@minus
end
end
end
# Functions to transliterate the digits
for {name, %{digits: digits}} <- System.systems_with_digits() do
graphemes = String.graphemes(digits)
for latin_digit <- 0..9 do
grapheme = :lists.nth(latin_digit + 1, graphemes)
latin_char = Integer.to_string(latin_digit)
defp transliterate_char(unquote(latin_char), _locale, unquote(name)) do
unquote(grapheme)
end
end
# Any unknown mapping gets returned as is
defp transliterate_char(digit, _locale, unquote(name)) do
digit
end
end
def transliterate(_digit, locale, number_system) do
raise Cldr.UnknownLocaleError, """
Locale #{inspect locale} or number system #{inspect number_system}
(or the combination of the two) is not known.
"""
end
end