-
Notifications
You must be signed in to change notification settings - Fork 34
/
cldr_chars.ex
43 lines (36 loc) · 916 Bytes
/
cldr_chars.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import Kernel, except: [to_string: 1]
defprotocol Cldr.Chars do
@moduledoc ~S"""
The `Cldr.Chars` protocol mirrors the
`String.Chars` protocol but localises
the output. It is intended to be
drop-in replacement for `String.Chars`.
"""
@doc """
Converts `term` to a localised string.
"""
@fallback_to_any true
@spec to_string(t) :: String.t()
def to_string(term)
end
defimpl Cldr.Chars, for: Cldr.LanguageTag do
def to_string(language_tag) do
Cldr.LanguageTag.to_string(language_tag)
end
end
defimpl Cldr.Chars, for: BitString do
def to_string(term) when is_binary(term) do
term
end
def to_string(term) do
raise Protocol.UndefinedError,
protocol: @protocol,
value: term,
description: "cannot convert a bitstring to a string"
end
end
defimpl Cldr.Chars, for: Any do
def to_string(language_tag) do
Kernel.to_string(language_tag)
end
end