This repository was archived by the owner on Jul 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalization.elm
95 lines (72 loc) · 2.3 KB
/
Localization.elm
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
module Localization
exposing
( Translation
, t
, untranslated
, Translator
, translateMessage
, translateMessageOrCrash
, orTranslator
)
{-| This is the main module of the package. It provides functions for creating
translations for messages, as well as some for working with translators that
translate the translation messages.
## Translations
@docs Translation, t, untranslated
## Translators
@docs Translator, translateMessage, translateMessageOrCrash, orTranslator
-}
{-| Translator is a function that holds translations of translation messages.
-}
type alias Translator message =
message -> Translation
{-| Represents a translated string.
Translated messages are created using `t` constructor; `untranslated` function
is used as a placeholder for the ones missing translation.
-}
type Translation
= Translated String
| Untranslated
{-| Create a translation of a message.
-}
t : String -> Translation
t tr =
Translated tr
{-| Untranslated message.
-}
untranslated : Translation
untranslated =
Untranslated
{-| Convert translation to `String`.
If the translator cannot translate the message, the message constructor is stringified.
-}
translateMessage : Translator message -> message -> String
translateMessage translator str =
case translator str of
Untranslated ->
toString str
Translated translation ->
translation
{-| Unsafely convert translation to `String`.
If the translator cannot translate the message, the application crashes.
-}
translateMessageOrCrash : Translator message -> message -> String
translateMessageOrCrash translator str =
case translator str of
Untranslated ->
Debug.crash ("Cannot find a translation for " ++ toString str)
Translated translation ->
translation
{-| If the first translator cannot translate the message, the second one is used.
-}
orTranslator : Translator message -> Translator message -> Translator message
orTranslator translator fallbackTranslator =
let
newTranslator str =
case translator str of
Untranslated ->
fallbackTranslator str
Translated translation ->
Translated translation
in
newTranslator