-
Notifications
You must be signed in to change notification settings - Fork 370
/
translator.ex
390 lines (353 loc) · 14 KB
/
translator.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
defmodule Timex.Translator do
import Timex.Gettext
defmacro with_locale(locale, do: block) do
quote do
Gettext.with_locale(Timex.Gettext, unquote(locale), fn -> unquote(block) end)
end
end
@doc """
Translates a string for a given locale and domain.
## Examples
iex> Timex.Translator.translate("ru", "weekdays", "Saturday")
"суббота"
iex> Timex.Translator.translate("it", "weekdays", "Saturday")
"Sabato"
iex> Timex.Translator.translate("invalid_locale", "weekdays", "Saturday")
"Saturday"
"""
@spec translate(locale :: String.t(), domain :: String.t(), msgid :: String.t()) :: String.t()
def translate(locale, domain, msgid) do
get_domain_text(locale, domain, msgid)
end
@doc """
Translates a string for a given locale and domain, following the pluralization rules of that
language.
## Examples
iex> Timex.Translator.translate_plural("ru", "relative_time", "in %{count} second", "in %{count} seconds", 5)
"через 5 секунд"
iex> Timex.Translator.translate_plural("it", "relative_time", "in %{count} second", "in %{count} seconds", 5)
"in 5 secondi"
iex> Timex.Translator.translate_plural("invalid_locale", "relative_time", "in %{count} second", "in %{count} seconds", 5)
"in 5 seconds"
"""
@spec translate_plural(
locale :: String.t(),
domain :: String.t(),
msgid :: String.t(),
msgid_plural :: String.t(),
n :: integer
) :: String.t()
def translate_plural(locale, domain, msgid, msgid_plural, n) do
get_plural_domain_text(locale, domain, msgid, msgid_plural, n)
end
@doc """
Returns the active locale for the process in which this function is called
"""
@spec current_locale() :: String.t()
def current_locale, do: Gettext.get_locale(Timex.Gettext)
@doc """
Returns a map of ordinal weekdays to weekday names, where Monday = 1,
translated in the given locale
"""
@spec get_weekdays(locale :: String.t()) :: %{integer() => String.t()}
def get_weekdays(locale) do
%{
1 => get_domain_text(locale, "weekdays", "Monday"),
2 => get_domain_text(locale, "weekdays", "Tuesday"),
3 => get_domain_text(locale, "weekdays", "Wednesday"),
4 => get_domain_text(locale, "weekdays", "Thursday"),
5 => get_domain_text(locale, "weekdays", "Friday"),
6 => get_domain_text(locale, "weekdays", "Saturday"),
7 => get_domain_text(locale, "weekdays", "Sunday")
}
end
@doc """
Returns a map of ordinal weekdays to weekday abbreviations, where Mon = 1
"""
@spec get_weekdays_abbreviated(locale :: String.t()) :: %{integer() => String.t()}
def get_weekdays_abbreviated(locale) do
%{
1 => get_domain_text(locale, "weekdays", "Mon"),
2 => get_domain_text(locale, "weekdays", "Tue"),
3 => get_domain_text(locale, "weekdays", "Wed"),
4 => get_domain_text(locale, "weekdays", "Thu"),
5 => get_domain_text(locale, "weekdays", "Fri"),
6 => get_domain_text(locale, "weekdays", "Sat"),
7 => get_domain_text(locale, "weekdays", "Sun")
}
end
@doc false
def get_weekdays_lookup(locale) do
%{
get_domain_text(locale, "weekdays", "Mon") => 1,
get_domain_text(locale, "weekdays", "Tue") => 2,
get_domain_text(locale, "weekdays", "Wed") => 3,
get_domain_text(locale, "weekdays", "Thu") => 4,
get_domain_text(locale, "weekdays", "Fri") => 5,
get_domain_text(locale, "weekdays", "Sat") => 6,
get_domain_text(locale, "weekdays", "Sun") => 7,
get_domain_text(locale, "weekdays", "Monday") => 1,
get_domain_text(locale, "weekdays", "Tuesday") => 2,
get_domain_text(locale, "weekdays", "Wednesday") => 3,
get_domain_text(locale, "weekdays", "Thursday") => 4,
get_domain_text(locale, "weekdays", "Friday") => 5,
get_domain_text(locale, "weekdays", "Saturday") => 6,
get_domain_text(locale, "weekdays", "Sunday") => 7
}
end
@doc """
Returns a map of ordinal months to month names
"""
@spec get_months(locale :: String.t()) :: %{integer() => String.t()}
def get_months(locale) do
%{
1 => get_domain_text(locale, "months", "January"),
2 => get_domain_text(locale, "months", "February"),
3 => get_domain_text(locale, "months", "March"),
4 => get_domain_text(locale, "months", "April"),
5 => get_domain_text(locale, "months", "May"),
6 => get_domain_text(locale, "months", "June"),
7 => get_domain_text(locale, "months", "July"),
8 => get_domain_text(locale, "months", "August"),
9 => get_domain_text(locale, "months", "September"),
10 => get_domain_text(locale, "months", "October"),
11 => get_domain_text(locale, "months", "November"),
12 => get_domain_text(locale, "months", "December")
}
end
@doc """
Returns a map of ordinal months to month abbreviations
"""
@spec get_months_abbreviated(locale :: String.t()) :: %{integer() => String.t()}
def get_months_abbreviated(locale) do
%{
1 => get_domain_text(locale, "months_abbr", "Jan"),
2 => get_domain_text(locale, "months_abbr", "Feb"),
3 => get_domain_text(locale, "months_abbr", "Mar"),
4 => get_domain_text(locale, "months_abbr", "Apr"),
5 => get_domain_text(locale, "months_abbr", "May"),
6 => get_domain_text(locale, "months_abbr", "Jun"),
7 => get_domain_text(locale, "months_abbr", "Jul"),
8 => get_domain_text(locale, "months_abbr", "Aug"),
9 => get_domain_text(locale, "months_abbr", "Sep"),
10 => get_domain_text(locale, "months_abbr", "Oct"),
11 => get_domain_text(locale, "months_abbr", "Nov"),
12 => get_domain_text(locale, "months_abbr", "Dec")
}
end
@doc false
def get_months_lookup(locale) do
%{
get_domain_text(locale, "months", "January") => 1,
get_domain_text(locale, "months", "February") => 2,
get_domain_text(locale, "months", "March") => 3,
get_domain_text(locale, "months", "April") => 4,
get_domain_text(locale, "months", "May") => 5,
get_domain_text(locale, "months", "June") => 6,
get_domain_text(locale, "months", "July") => 7,
get_domain_text(locale, "months", "August") => 8,
get_domain_text(locale, "months", "September") => 9,
get_domain_text(locale, "months", "October") => 10,
get_domain_text(locale, "months", "November") => 11,
get_domain_text(locale, "months", "December") => 12,
get_domain_text(locale, "months_abbr", "Jan") => 1,
get_domain_text(locale, "months_abbr", "Feb") => 2,
get_domain_text(locale, "months_abbr", "Mar") => 3,
get_domain_text(locale, "months_abbr", "Apr") => 4,
get_domain_text(locale, "months_abbr", "May") => 5,
get_domain_text(locale, "months_abbr", "Jun") => 6,
get_domain_text(locale, "months_abbr", "Jul") => 7,
get_domain_text(locale, "months_abbr", "Aug") => 8,
get_domain_text(locale, "months_abbr", "Sep") => 9,
get_domain_text(locale, "months_abbr", "Oct") => 10,
get_domain_text(locale, "months_abbr", "Nov") => 11,
get_domain_text(locale, "months_abbr", "Dec") => 12
}
end
@doc """
Returns a map of day period types to translated day period names
## Examples
iex> day_periods = Timex.Translator.get_day_periods("en")
...> {day_periods[:am], day_periods[:AM]}
{"am", "AM"}
"""
@spec get_day_periods(locale :: String.t()) :: %{atom() => String.t()}
def get_day_periods(locale) do
%{
:AM => get_domain_text(locale, "day_periods", "AM"),
:am => get_domain_text(locale, "day_periods", "am"),
:PM => get_domain_text(locale, "day_periods", "PM"),
:pm => get_domain_text(locale, "day_periods", "pm")
}
end
@doc false
def get_day_periods_lower(locale) do
[
get_domain_text(locale, "day_periods", "am"),
get_domain_text(locale, "day_periods", "pm")
]
end
@doc false
def get_day_periods_upper(locale) do
[
get_domain_text(locale, "day_periods", "AM"),
get_domain_text(locale, "day_periods", "PM")
]
end
@doc false
def get_day_periods_lookup(locale) do
%{
get_domain_text(locale, "day_periods", "AM") => :AM,
get_domain_text(locale, "day_periods", "am") => :am,
get_domain_text(locale, "day_periods", "PM") => :PM,
get_domain_text(locale, "day_periods", "pm") => :pm
}
end
@spec get_domain_text(locale :: String.t(), domain :: String.t(), msgid :: String.t()) ::
String.t()
defp get_domain_text(locale, domain, msgid) do
Gettext.with_locale(Timex.Gettext, locale, fn ->
Gettext.dgettext(Timex.Gettext, domain, msgid, %{})
end)
end
@spec get_plural_domain_text(
locale :: String.t(),
domain :: String.t(),
msgid :: String.t(),
msgid_plural :: String.t(),
n :: integer
) :: String.t()
defp get_plural_domain_text(locale, domain, msgid, msgid_plural, n) do
Gettext.with_locale(Timex.Gettext, locale, fn ->
Gettext.dngettext(Timex.Gettext, domain, msgid, msgid_plural, n, %{})
end)
end
### After this point, all gettext calls are here for use with compile-time tooling
dngettext("units", "%{count} nanosecond", "%{count} nanoseconds", 0)
dngettext("units", "%{count} microsecond", "%{count} microseconds", 0)
dngettext("units", "%{count} millisecond", "%{count} milliseconds", 0)
dngettext("units", "%{count} second", "%{count} seconds", 0)
dngettext("units", "%{count} minute", "%{count} minutes", 0)
dngettext("units", "%{count} hour", "%{count} hours", 0)
dngettext("units", "%{count} day", "%{count} days", 0)
dngettext("units", "%{count} week", "%{count} weeks", 0)
dngettext("units", "%{count} month", "%{count} months", 0)
dngettext("units", "%{count} year", "%{count} years", 0)
dgettext("day_periods", "AM")
dgettext("day_periods", "am")
dgettext("day_periods", "PM")
dgettext("day_periods", "pm")
dgettext("weekdays", "Mon")
dgettext("weekdays", "Tue")
dgettext("weekdays", "Wed")
dgettext("weekdays", "Thu")
dgettext("weekdays", "Fri")
dgettext("weekdays", "Sat")
dgettext("weekdays", "Sun")
dgettext("weekdays", "Monday")
dgettext("weekdays", "Tuesday")
dgettext("weekdays", "Wednesday")
dgettext("weekdays", "Thursday")
dgettext("weekdays", "Friday")
dgettext("weekdays", "Saturday")
dgettext("weekdays", "Sunday")
dgettext("months_abbr", "Jan")
dgettext("months_abbr", "Feb")
dgettext("months_abbr", "Mar")
dgettext("months_abbr", "Apr")
dgettext("months_abbr", "May")
dgettext("months_abbr", "Jun")
dgettext("months_abbr", "Jul")
dgettext("months_abbr", "Aug")
dgettext("months_abbr", "Sep")
dgettext("months_abbr", "Oct")
dgettext("months_abbr", "Nov")
dgettext("months_abbr", "Dec")
dgettext("months", "January")
dgettext("months", "February")
dgettext("months", "March")
dgettext("months", "April")
dgettext("months", "May")
dgettext("months", "June")
dgettext("months", "July")
dgettext("months", "August")
dgettext("months", "September")
dgettext("months", "October")
dgettext("months", "November")
dgettext("months", "December")
# relative years
dgettext("relative_time", "last year")
dgettext("relative_time", "this year")
dgettext("relative_time", "next year")
dngettext("relative_time", "in %{count} year", "in %{count} years", 0)
dngettext("relative_time", "%{count} year ago", "%{count} years ago", 0)
# relative months
dgettext("relative_time", "last month")
dgettext("relative_time", "this month")
dgettext("relative_time", "next month")
dngettext("relative_time", "in %{count} month", "in %{count} months", 0)
dngettext("relative_time", "%{count} month ago", "%{count} months ago", 0)
# relative weeks
dgettext("relative_time", "last week")
dgettext("relative_time", "this week")
dgettext("relative_time", "next week")
dngettext("relative_time", "in %{count} week", "in %{count} weeks", 0)
dngettext("relative_time", "%{count} week ago", "%{count} weeks ago", 0)
# relative days
dgettext("relative_time", "yesterday")
dgettext("relative_time", "today")
dgettext("relative_time", "tomorrow")
dngettext("relative_time", "in %{count} day", "in %{count} days", 0)
dngettext("relative_time", "%{count} day ago", "%{count} days ago", 0)
# relative weekdays
dgettext("relative_time", "last monday")
dgettext("relative_time", "this monday")
dgettext("relative_time", "next monday")
dgettext("relative_time", "last tuesday")
dgettext("relative_time", "this tuesday")
dgettext("relative_time", "next tuesday")
dgettext("relative_time", "last wednesday")
dgettext("relative_time", "this wednesday")
dgettext("relative_time", "next wednesday")
dgettext("relative_time", "last thursday")
dgettext("relative_time", "this thursday")
dgettext("relative_time", "next thursday")
dgettext("relative_time", "last friday")
dgettext("relative_time", "this friday")
dgettext("relative_time", "next friday")
dgettext("relative_time", "last saturday")
dgettext("relative_time", "this saturday")
dgettext("relative_time", "next saturday")
dgettext("relative_time", "last sunday")
dgettext("relative_time", "this sunday")
dgettext("relative_time", "next sunday")
# relative hours
dngettext("relative_time", "in %{count} hour", "in %{count} hours", 0)
dngettext("relative_time", "%{count} hour ago", "%{count} hours ago", 0)
# relative minutes
dngettext("relative_time", "in %{count} minute", "in %{count} minutes", 0)
dngettext("relative_time", "%{count} minute ago", "%{count} minutes ago", 0)
# relative seconds
dngettext("relative_time", "in %{count} second", "in %{count} seconds", 0)
dngettext("relative_time", "%{count} second ago", "%{count} seconds ago", 0)
# relative now
dgettext("relative_time", "now")
# symbols
# decimal
dgettext("symbols", ".")
# group
dgettext("symbols", ",")
# list
dgettext("symbols", ";")
# plus
dgettext("symbols", "+")
# minus
dgettext("symbols", "-")
# exponent
dgettext("symbols", "E")
# time separator
dgettext("symbols", ":")
# numbers
# decimal format
dgettext("numbers", "#,##0.###")
end