/
En.elm
119 lines (85 loc) · 2.32 KB
/
En.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
module Date.Distance.I18n.En
exposing
( LocaleConfig
, locale
)
{-| English locale. Used by default.
@docs LocaleConfig
@docs locale
-}
import Date.Distance.Types exposing (Locale, DistanceLocale(..))
import Date.Extra as Date exposing (Interval(..))
{-| Configure the localization function.
- `addSuffix` – turns `2 days` into `2 days ago` or `in 2 days`
-}
type alias LocaleConfig =
{ addSuffix : Bool }
{-| Configure the English locale.
locale =
I18n.En.locale { addSuffix = True }
inWords =
{ defaultConfig | locale = locale }
|> inWordsWithConfig
-}
locale : LocaleConfig -> Locale
locale { addSuffix } order distance =
let
result =
locale_ distance
in
if addSuffix then
if order == LT then
"in " ++ result
else
result ++ " ago"
else
result
locale_ : DistanceLocale -> String
locale_ distance =
case distance of
LessThanXSeconds i ->
circa "less than" Second i
HalfAMinute ->
"half a minute"
LessThanXMinutes i ->
circa "less than" Minute i
XMinutes i ->
exact Minute i
AboutXHours i ->
circa "about" Hour i
XDays i ->
exact Day i
AboutXMonths i ->
circa "about" Month i
XMonths i ->
exact Month i
AboutXYears i ->
circa "about" Year i
OverXYears i ->
circa "over" Year i
AlmostXYears i ->
circa "almost" Year i
formatInterval : Interval -> String
formatInterval =
String.toLower << toString
singular : Interval -> String
singular interval =
case interval of
Minute ->
"a " ++ formatInterval interval
_ ->
"1 " ++ formatInterval interval
circa : String -> Interval -> Int -> String
circa prefix interval i =
case i of
1 ->
prefix ++ " " ++ singular interval
_ ->
prefix ++ " " ++ toString i ++ " " ++ formatInterval interval ++ "s"
exact : Interval -> Int -> String
exact interval i =
case i of
1 ->
"1 " ++ formatInterval interval
_ ->
toString i ++ " " ++ formatInterval interval ++ "s"