/
plural.ex
684 lines (573 loc) · 14.9 KB
/
plural.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
defmodule Gettext.Plural do
@moduledoc """
Behaviour and default implementation for finding plural forms in given
locales.
This module both defines the `Gettext.Plural` behaviour and provides a default
implementation for it.
## Plural forms
> For a given language, there is a grammatical rule on how to change words
> depending on the number qualifying the word. Different languages can have
> different rules.
[[source]](https://udn.realityripple.com/docs/Mozilla/Localization/Localization_and_Plurals)
Such grammatical rules define a number of **plural forms**. For example,
English has two plural forms: one for when there is just one element (the
*singular*) and another one for when there are zero or more than one elements
(the *plural*). There are languages which only have one plural form and there
are languages which have more than two.
In GNU Gettext (and in Gettext for Elixir), plural forms are represented by
increasing 0-indexed integers. For example, in English `0` means singular and
`1` means plural.
The goal of this module is to determine, given a locale:
* how many plural forms exist in that locale (`nplurals/1`);
* to what plural form a given number of elements belongs to in that locale
(`plural/2`).
## Default implementation
`Gettext.Plural` provides a default implementation of a plural module. Most
languages used on Earth should be covered by this default implementation. If
custom pluralization rules are needed (for example, to add additional
languages) a different plural module can be specified when creating a Gettext
backend. For example, pluralization rules for the Elvish language could be
added as follows:
defmodule MyApp.Plural do
@behaviour Gettext.Plural
def nplurals("elv"), do: 3
def plural("elv", 0), do: 0
def plural("elv", 1), do: 1
def plural("elv", _), do: 2
# Fallback to Gettext.Plural
def nplurals(locale), do: Gettext.Plural.nplurals(locale)
def plural(locale, n), do: Gettext.Plural.plural(locale, n)
end
The mathematical expressions used in this module to determine the plural form
of a given number of elements are taken from [this
page](http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html#f2)
as well as from [Mozilla's guide on "Localization and
plurals"](https://udn.realityripple.com/docs/Mozilla/Localization/Localization_and_Plurals).
Now that we have defined our custom plural forms, we can use them
in two ways. You can set it for all `:gettext` backends in your
config files:
config :gettext, :plural_forms, MyApp.Plural
Or to each specific backend:
defmodule MyApp.Gettext do
use Gettext, otp_app: :my_app, plural_forms: MyApp.Plural
end
**Note**: set `:plural_forms` in your `config/config.exs` and
not in `config/runtime.exs`, as this configuration is read when
compiling your backends.
Notice that tasks such as `mix gettext.merge` use the plural
backend configured under the `:gettext` application, so generally
speaking the first format is preferred.
Note some tasks also allow the number of plural forms to be given
explicitly, for example:
mix gettext.merge priv/gettext --locale=gsw_CH --plural-forms=2
### Unknown locales
Trying to call `Gettext.Plural` functions with unknown locales will result in
a `Gettext.Plural.UnknownLocaleError` exception.
### Language and territory
Often, a locale is composed as a language and territory couple, such as
`en_US`. The default implementation for `Gettext.Plural` handles `xx_YY` by
forwarding it to `xx` (except for *just Brazilian Portuguese*, `pt_BR`, which
is not forwarded to `pt` as pluralization rules slightly differ). We treat the
underscore as a separator according to
[ISO 15897](https://en.wikipedia.org/wiki/ISO/IEC_15897). Sometimes, a dash `-` is
used as a separator (for example [BCP47](https://en.wikipedia.org/wiki/IETF_language_tag)
locales use this as in `en-US`): this is not forwarded to `en` in the default
`Gettext.Plural` (and it will raise an `Gettext.Plural.UnknownLocaleError` exception
if there are no translations for `en-US`).
## Examples
An example of the plural form of a given number of elements in the Polish
language:
iex> Plural.plural("pl", 1)
0
iex> Plural.plural("pl", 2)
1
iex> Plural.plural("pl", 5)
2
iex> Plural.plural("pl", 112)
2
As expected, `nplurals/1` returns the possible number of plural forms:
iex> Plural.nplurals("pl")
3
"""
# Behaviour definition.
@doc """
Returns the number of possible plural forms in the given `locale`.
"""
@callback nplurals(locale :: String.t()) :: pos_integer
@doc """
Returns the plural form in the given `locale` for the given `count` of
elements.
"""
@callback plural(locale :: String.t(), count :: integer) :: plural_form :: non_neg_integer
defmodule UnknownLocaleError do
@moduledoc """
Raised when a pluralized module doesn't know how to handle a locale.
## Examples
raise Gettext.Plural.UnknownLocaleError, "en-US"
"""
defexception [:message]
def exception(locale) when is_binary(locale) do
message = """
unknown locale #{inspect(locale)}. If this is a locale you need to handle,
consider using a custom pluralizer module instead of the default
Gettext.Plural. You can read more about this on the Gettext docs at
https://hexdocs.pm/gettext/Gettext.Plural.html
"""
%__MODULE__{message: message}
end
end
# Behaviour implementation.
defmacrop ends_in(n, digits) do
digits = List.wrap(digits)
quote do
rem(unquote(n), 10) in unquote(digits)
end
end
@one_form [
# Aymará
"ay",
# Tibetan
"bo",
# Chiga
"cgg",
# Dzongkha
"dz",
# Persian
"fa",
# Indonesian
"id",
# Japanese
"ja",
# Lojban
"jbo",
# Georgian
"ka",
# Kazakh
"kk",
# Khmer
"km",
# Korean
"ko",
# Kyrgyz
"ky",
# Lao
"lo",
# Malay
"ms",
# Burmese
"my",
# Yakut
"sah",
# Sundanese
"su",
# Thai
"th",
# Tatar
"tt",
# Uyghur
"ug",
# Vietnamese
"vi",
# Wolof
"wo",
# Chinese [2]
"zh"
]
@two_forms_1 [
# Afrikaans
"af",
# Aragonese
"an",
# Angika
"anp",
# Assamese
"as",
# Asturian
"ast",
# Azerbaijani
"az",
# Bulgarian
"bg",
# Bengali
"bn",
# Bodo
"brx",
# Catalan
"ca",
# Danish
"da",
# German
"de",
# Dogri
"doi",
# Greek
"el",
# English
"en",
# Esperanto
"eo",
# Spanish
"es",
# Estonian
"et",
# Basque
"eu",
# Fulah
"ff",
# Finnish
"fi",
# Faroese
"fo",
# Friulian
"fur",
# Frisian
"fy",
# Galician
"gl",
# Gujarati
"gu",
# Hausa
"ha",
# Hebrew
"he",
# Hindi
"hi",
# Chhattisgarhi
"hne",
# Armenian
"hy",
# Hungarian
"hu",
# Interlingua
"ia",
# Italian
"it",
# Greenlandic
"kl",
# Kannada
"kn",
# Kurdish
"ku",
# Letzeburgesch
"lb",
# Maithili
"mai",
# Malayalam
"ml",
# Mongolian
"mn",
# Manipuri
"mni",
# Marathi
"mr",
# Nahuatl
"nah",
# Neapolitan
"nap",
# Norwegian Bokmal
"nb",
# Nepali
"ne",
# Dutch
"nl",
# Northern Sami
"se",
# Norwegian Nynorsk
"nn",
# Norwegian (old code)
"no",
# Northern Sotho
"nso",
# Oriya
"or",
# Pashto
"ps",
# Punjabi
"pa",
# Papiamento
"pap",
# Piemontese
"pms",
# Portuguese
"pt",
# Romansh
"rm",
# Kinyarwanda
"rw",
# Santali
"sat",
# Scots
"sco",
# Sindhi
"sd",
# Sinhala
"si",
# Somali
"so",
# Songhay
"son",
# Albanian
"sq",
# Swahili
"sw",
# Swedish
"sv",
# Tamil
"ta",
# Telugu
"te",
# Turkmen
"tk",
# Urdu
"ur",
# Yoruba
"yo"
]
@two_forms_2 [
# Acholi
"ach",
# Akan
"ak",
# Amharic
"am",
# Mapudungun
"arn",
# Breton
"br",
# Filipino
"fil",
# French
"fr",
# Gun
"gun",
# Lingala
"ln",
# Mauritian Creole
"mfe",
# Malagasy
"mg",
# Maori
"mi",
# Occitan
"oc",
# Tajik
"tg",
# Tigrinya
"ti",
# Tagalog
"tl",
# Turkish
"tr",
# Uzbek
"uz",
# Walloon
"wa"
]
@three_forms_slavic [
# Belarusian
"be",
# Bosnian
"bs",
# Croatian
"hr",
# Serbian
"sr",
# Russian
"ru",
# Ukrainian
"uk"
]
@three_forms_slavic_alt [
# Czech
"cs",
# Slovak
"sk"
]
# Number of plural forms.
def nplurals(locale)
# All the groupable forms.
for l <- @one_form do
def nplurals(unquote(l)), do: 1
end
for l <- @two_forms_1 ++ @two_forms_2 do
def nplurals(unquote(l)), do: 2
end
for l <- @three_forms_slavic ++ @three_forms_slavic_alt do
def nplurals(unquote(l)), do: 3
end
# Then, all other ones.
# Arabic
def nplurals("ar"), do: 6
# Kashubian
def nplurals("csb"), do: 3
# Welsh
def nplurals("cy"), do: 4
# Irish
def nplurals("ga"), do: 5
# Scottish Gaelic
def nplurals("gd"), do: 4
# Icelandic
def nplurals("is"), do: 2
# Javanese
def nplurals("jv"), do: 2
# Cornish
def nplurals("kw"), do: 4
# Lithuanian
def nplurals("lt"), do: 3
# Latvian
def nplurals("lv"), do: 3
# Macedonian
def nplurals("mk"), do: 3
# Mandinka
def nplurals("mnk"), do: 3
# Maltese
def nplurals("mt"), do: 4
# Polish
def nplurals("pl"), do: 3
# Romanian
def nplurals("ro"), do: 3
# Slovenian
def nplurals("sl"), do: 4
# Match-all clause.
def nplurals(locale) do
recall_if_territory_or_raise(locale, &nplurals/1)
end
# Plural form of groupable languages.
def plural(locale, count)
# All the `x_Y` languages that have different pluralization rules than `x`.
def plural("pt_BR", n) when n in [0, 1], do: 0
def plural("pt_BR", _n), do: 1
# Groupable forms.
for l <- @one_form do
def plural(unquote(l), _n), do: 0
end
for l <- @two_forms_1 do
def plural(unquote(l), 1), do: 0
def plural(unquote(l), _n), do: 1
end
for l <- @two_forms_2 do
def plural(unquote(l), n) when n in [0, 1], do: 0
def plural(unquote(l), _n), do: 1
end
for l <- @three_forms_slavic do
def plural(unquote(l), n)
when ends_in(n, 1) and rem(n, 100) != 11,
do: 0
def plural(unquote(l), n)
when ends_in(n, [2, 3, 4]) and (rem(n, 100) < 10 or rem(n, 100) >= 20),
do: 1
def plural(unquote(l), _n), do: 2
end
for l <- @three_forms_slavic_alt do
def plural(unquote(l), 1), do: 0
def plural(unquote(l), n) when n in 2..4, do: 1
def plural(unquote(l), _n), do: 2
end
# Custom plural forms.
# Arabic
# n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5
def plural("ar", 0), do: 0
def plural("ar", 1), do: 1
def plural("ar", 2), do: 2
def plural("ar", n) when rem(n, 100) >= 3 and rem(n, 100) <= 10, do: 3
def plural("ar", n) when rem(n, 100) >= 11, do: 4
def plural("ar", _n), do: 5
# Kashubian
# (n==1) ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;
def plural("csb", 1), do: 0
def plural("csb", n)
when ends_in(n, [2, 3, 4]) and (rem(n, 100) < 10 or rem(n, 100) >= 20),
do: 1
def plural("csb", _n), do: 2
# Welsh
# (n==1) ? 0 : (n==2) ? 1 : (n != 8 && n != 11) ? 2 : 3
def plural("cy", 1), do: 0
def plural("cy", 2), do: 1
def plural("cy", n) when n != 8 and n != 11, do: 2
def plural("cy", _n), do: 3
# Irish
# n==1 ? 0 : n==2 ? 1 : (n>2 && n<7) ? 2 :(n>6 && n<11) ? 3 : 4
def plural("ga", 1), do: 0
def plural("ga", 2), do: 1
def plural("ga", n) when n in 3..6, do: 2
def plural("ga", n) when n in 7..10, do: 3
def plural("ga", _n), do: 4
# Scottish Gaelic
# (n==1 || n==11) ? 0 : (n==2 || n==12) ? 1 : (n > 2 && n < 20) ? 2 : 3
def plural("gd", n) when n == 1 or n == 11, do: 0
def plural("gd", n) when n == 2 or n == 12, do: 1
def plural("gd", n) when n > 2 and n < 20, do: 2
def plural("gd", _n), do: 3
# Icelandic
# n%10!=1 || n%100==11
def plural("is", n) when ends_in(n, 1) and rem(n, 100) != 11, do: 0
def plural("is", _n), do: 1
# Javanese
# n != 0
def plural("jv", 0), do: 0
def plural("jv", _), do: 1
# Cornish
# (n==1) ? 0 : (n==2) ? 1 : (n == 3) ? 2 : 3
def plural("kw", 1), do: 0
def plural("kw", 2), do: 1
def plural("kw", 3), do: 2
def plural("kw", _), do: 3
# Lithuanian
# n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2
def plural("lt", n)
when ends_in(n, 1) and rem(n, 100) != 11,
do: 0
def plural("lt", n)
when rem(n, 10) >= 2 and (rem(n, 100) < 10 or rem(n, 100) >= 20),
do: 1
def plural("lt", _), do: 2
# Latvian
# n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2
def plural("lv", n) when ends_in(n, 1) and rem(n, 100) != 11, do: 0
def plural("lv", n) when n != 0, do: 1
def plural("lv", _), do: 2
# Macedonian
# n==1 || n%10==1 ? 0 : 1; Can’t be correct needs a 2 somewhere
def plural("mk", n) when ends_in(n, 1), do: 0
def plural("mk", n) when ends_in(n, 2), do: 1
def plural("mk", _), do: 2
# Mandinka
# n==0 ? 0 : n==1 ? 1 : 2
def plural("mnk", 0), do: 0
def plural("mnk", 1), do: 1
def plural("mnk", _), do: 2
# Maltese
# n==1 ? 0 : n==0 || ( n%100>1 && n%100<11) ? 1 : (n%100>10 && n%100<20 ) ? 2 : 3
def plural("mt", 1), do: 0
def plural("mt", n) when n == 0 or (rem(n, 100) > 1 and rem(n, 100) < 11), do: 1
def plural("mt", n) when rem(n, 100) > 10 and rem(n, 100) < 20, do: 2
def plural("mt", _), do: 3
# Polish
# n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2
def plural("pl", 1), do: 0
def plural("pl", n)
when ends_in(n, [2, 3, 4]) and (rem(n, 100) < 10 or rem(n, 100) >= 20),
do: 1
def plural("pl", _), do: 2
# Romanian
# n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2
def plural("ro", 1), do: 0
def plural("ro", n) when n == 0 or (rem(n, 100) > 0 and rem(n, 100) < 20), do: 1
def plural("ro", _), do: 2
# Slovenian
# n%100==1 ? 1 : n%100==2 ? 2 : n%100==3 || n%100==4 ? 3 : 0
def plural("sl", n) when rem(n, 100) == 1, do: 1
def plural("sl", n) when rem(n, 100) == 2, do: 2
def plural("sl", n) when rem(n, 100) == 3, do: 3
def plural("sl", _), do: 0
# Match-all clause.
def plural(locale, n) do
recall_if_territory_or_raise(locale, &plural(&1, n))
end
defp recall_if_territory_or_raise(locale, fun) do
case String.split(locale, "_", parts: 2, trim: true) do
[lang, _territory] -> fun.(lang)
_other -> raise UnknownLocaleError, locale
end
end
end