/
cldr.ex
2502 lines (1845 loc) · 71.1 KB
/
cldr.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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
defmodule Cldr do
@moduledoc """
Cldr provides the core functions to retrieve and manage
the CLDR data that supports formatting and localisation.
This module provides the core functions to access formatted
CLDR data, set and retrieve a current locale and validate
certain core data types such as locales, currencies and
territories.
> #### `use Cldr` {: .info}
>
> When you `use Cldr`, a number of functions are generated
> that encapsulate CLDR data. A moodule that invokes `use Cldr`
> is referred to as a Cldr backend module.
>
> The functions in a Cldr backend module form the primary
> recommended API for `ex_cldr`.
>
> In additional, a number of additional modules
> may be generated with names that are prefixed by the name of
> the module in which `use Cldr` is invoked. The number and names
> of these additional modules is determined by the modules
> configured under the `:providers` option to `use Cldr`.
>
> It is not recommended that a module that invoke `use Cldr`
> define any other functions.
>
> See [the configuration guide](https://hexdocs.pm/ex_cldr/readme.html#configuration)
> for details on how to configure.
`Cldr` functionality is packaged into a several
libraries that each depend on this one. These additional
libraries are:
* Plugs for setting the locale from an HTTP request: [ex_cldr_plugs](https://hex.pm/packages/ex_cldr_plugs)
* Number formatting: [ex_cldr_numbers](https://hex.pm/packages/ex_cldr_numbers)
* List formatting: [ex_cldr_lists](https://hex.pm/packages/ex_cldr_lists)
* Unit formatting: [ex_cldr_units](https://hex.pm/packages/ex_cldr_units)
* Date/Time/DateTime formatting: [ex_cldr_dates_times](https://hex.pm/packages/ex_cldr_dates_times)
* Route localization for Phoenix: [ex_cldr_routes](https://hex.pm/packages/ex_cldr_routes)
* Locale name localisation: [ex_cldr_locale_display](https://hex.pm/packages/ex_cldr_locale_display)
* HTML select helpers: [ex_cldr_html](https://hex.pm/packages/cldr_html)
* Calendars: [ex_cldr_calendars](https://hex.pm/packages/ex_cldr_calendars)
* Calendar formatting: [ex_cldr_calendars_format](https://hex.pm/packages/ex_cldr_calendars_format)
* Printf-like formatting: [ex_cldr_print](https://hex.pm/packages/ex_cldr_print)
* Collation: [ex_cldr_collation](https://hex.pm/packages/ex_cldr_collation)
* ICU Message formatting: [ex_cldr_messages](https://hex.pm/packages/ex_cldr_messages)
* Territories localization and information: [ex_cldr_territories](https://hex.pm/packages/ex_cldr_territories) by @Schultzer
* Languages localization: [ex_cldr_languages](https://hex.pm/packages/ex_cldr_languages) by @lostkobrakai
"""
@external_resource "priv/cldr/language_tags.ebin"
@app_name Cldr.Config.app_name()
@type backend :: module()
defguard is_locale_name(locale_name) when is_atom(locale_name) or is_binary(locale_name)
alias Cldr.Config
alias Cldr.Locale
alias Cldr.Locale.Loader
alias Cldr.LanguageTag
require Config
require Cldr.Backend.Compiler
import Kernel, except: [to_string: 1]
@doc false
defmacro __using__(opts \\ []) do
quote bind_quoted: [opts: opts] do
@cldr_opts opts
@before_compile Cldr.Backend.Compiler
end
end
@version_string Config.version()
@doc """
Returns the version of the CLDR repository.
## Example
iex> Cldr.version
Version.parse!(#{inspect(@version_string)})
"""
@spec version :: Version.t()
def version do
Version.parse!(@version_string)
end
@warn_if_greater_than 100
@doc false
def install_locales(config) do
alias Cldr.Config
Cldr.Install.install_known_locale_names(config)
known_locale_count = Enum.count(Loader.known_locale_names(config))
locale_string = if known_locale_count > 1, do: "locales named ", else: "locale named "
if Enum.any?(Config.unknown_locale_names(config)) do
raise Cldr.UnknownLocaleError,
"Some locale names are configured that are not known to CLDR. " <>
"Compilation cannot continue until the configuration includes only " <>
"locales names known in CLDR.\n\n" <>
"Configured locales names: #{inspect(Config.requested_locale_names(config))}\n" <>
"Gettext locales names: #{inspect(Config.known_gettext_locale_names(config))}\n" <>
"Unknown locales names: " <>
"#{IO.ANSI.red()}#{inspect(Config.unknown_locale_names(config))}" <>
"#{IO.ANSI.default_color()}\n"
end
IO.puts(
"Generating #{inspect(config.backend)} for #{known_locale_count} " <>
locale_string <>
"#{inspect(Loader.known_locale_names(config), limit: 5)} with " <>
"a default locale named #{inspect(Config.default_locale_name(config))}"
)
if known_locale_count > @warn_if_greater_than do
IO.puts("Please be patient, generating functions for many locales " <> "can take some time")
end
end
@doc """
Return the `Cldr` locale for the
current process.
Note that the locale is set per-process. If the locale
is not set for the given process then:
* Return the global default locale
which is defined under the `:ex_cldr` key in
`config.exs`
* Or the system-wide default locale which is
#{inspect(Cldr.Config.default_locale())}
Note that if there is no locale set for the current
process then an error is not returned - a default locale
will be returned per the rules above.
## Example
iex> Cldr.put_locale(TestBackend.Cldr.Locale.new!("pl"))
iex> Cldr.get_locale()
%Cldr.LanguageTag{
backend: TestBackend.Cldr,
canonical_locale_name: "pl",
cldr_locale_name: :pl,
extensions: %{},
language: "pl",
locale: %{},
private_use: [],
rbnf_locale_name: :pl,
territory: :PL,
requested_locale_name: "pl",
script: :Latn,
transform: %{},
language_variants: []
}
"""
@process_dictionary_key :cldr_locale
def get_locale(_backend \\ nil)
def get_locale(nil) do
Process.get(@process_dictionary_key) || default_locale()
end
def get_locale(backend) do
Process.get(@process_dictionary_key) ||
backend.default_locale() ||
default_locale()
end
@doc """
Set the current process's locale for a specified backend or
for all backends.
## Arguments
* `backend` is any module that includes `use Cldr` and therefore
is a `Cldr` backend module. The default is to set the locale
for all backends.
* `locale` is any valid locale name returned by `Cldr.known_locale_names/1`
or a `Cldr.LanguageTag` struct returned by `Cldr.Locale.new!/2`. It may
also be a map that contains the keys `"cldr_locale"` and `"cldr_backend"`
which is the shape of a `Phoenix` and `Plug` session making it easy to
set the locale from a session.
## Returns
* `{:ok, locale}`
## Behaviour
1. If no backend is provided and the locale is a `Cldr.LanguageTag.t`
then the the locale is set as the default for the current process
## Notes
See [rfc5646](https://tools.ietf.org/html/rfc5646) for the specification
of a language tag
## Examples
iex> Cldr.put_locale(TestBackend.Cldr, "en")
{:ok,
%Cldr.LanguageTag{
backend: TestBackend.Cldr,
canonical_locale_name: "en",
cldr_locale_name: :en,
language_subtags: [],
extensions: %{},
gettext_locale_name: "en",
language: "en",
locale: %{},
private_use: [],
rbnf_locale_name: :en,
requested_locale_name: "en",
script: :Latn,
territory: :US,
transform: %{},
language_variants: []
}}
iex> Cldr.put_locale(TestBackend.Cldr, "invalid-locale!")
{:error, {Cldr.LanguageTag.ParseError,
"Expected a BCP47 language tag. Could not parse the remaining \\"!\\" starting at position 15"}}
"""
@spec put_locale(backend(), Locale.locale_reference()) ::
{:ok, LanguageTag.t()} | {:error, {module(), String.t()}}
def put_locale(backend \\ nil, locale)
def put_locale(nil, locale) when is_locale_name(locale) do
backend = default_backend!()
with {:ok, locale} <- backend.validate_locale(locale) do
put_locale(locale)
end
end
def put_locale(backend, locale_name) when is_atom(backend) and is_locale_name(locale_name) do
with {:ok, backend} <- validate_backend(backend),
{:ok, locale} <- backend.validate_locale(locale_name) do
put_locale(locale)
end
end
def put_locale(_backend, %LanguageTag{} = locale) do
Process.put(@process_dictionary_key, locale)
{:ok, locale}
end
# For when the parameter is a session (used by Plug and Phoenix)
def put_locale(_backend, %{"cldr_locale" => locale, "cldr_backend" => backend}) do
backend = Module.concat([backend])
put_locale(backend, locale)
end
def put_locale(_backend, %{"cldr_locale" => locale}) do
put_locale(locale)
end
if Code.ensure_loaded?(Gettext) do
@doc """
Set the current process's Gettext locale from a
`t:Cldr.LanguageTag`.
## Arguments
* `locale` is a `Cldr.LanguageTag` struct returned by `Cldr.Locale.new!/2`.
## Returns
* `{:ok, gettext_locale_name}` or
* `{:error, {exception, reason}}`
## Behaviour
1. If the `locale.gettext_locale_name` is `nil` then an error
is returned.
2. The `gettext` locale for the `gettext_backend` configured for the
CLDR backend defined by the `t:Cldr.LanguageTag` is set.
## Examples
iex> import Cldr.LanguageTag.Sigil
iex> Cldr.put_gettext_locale(~l"en")
{:ok, "en"}
iex> import Cldr.LanguageTag.Sigil
iex> Cldr.put_gettext_locale(~l"de")
{:error,
{Cldr.UnknownLocaleError,
"Locale #Cldr.LanguageTag<de [validated]> does not map to a known gettext locale name"}}
"""
@spec put_gettext_locale(LanguageTag.t()) ::
{:ok, binary() | nil} | {:error, {module(), String.t()}}
def put_gettext_locale(%LanguageTag{gettext_locale_name: nil} = locale) do
{:error,
{Cldr.UnknownLocaleError,
"Locale #{inspect(locale)} does not map to a known gettext locale name"}}
end
def put_gettext_locale(%LanguageTag{gettext_locale_name: gettext_locale_name} = locale) do
gettext_backend = locale.backend.__cldr__(:gettext)
_ = Gettext.put_locale(gettext_backend, gettext_locale_name)
{:ok, gettext_locale_name}
end
end
@doc """
Set's the system default locale.
The locale set here is the base level
system default equivalent to setting the
`:default_locale` key in `config.exs`.
## Arguments
* `locale` is a `Cldr.LanguageTag` struct returned by `Cldr.Locale.new!/2`
with a non-nil `:cldr_locale_name`.
## Returns
* `{:ok, locale}`
"""
def put_default_locale(%Cldr.LanguageTag{} = locale) do
:ok = Application.put_env(@app_name, :_default_locale, locale)
{:ok, locale}
end
@doc """
Sets the system default locale.
The locale set here is the base level
system default equivalent to setting the
`:default_locale` key in `config.exs`.
## Arguments
* `locale` is any valid locale name returned by `Cldr.known_locale_names/1`.
* `backend` is any module that includes `use Cldr` and therefore
is a `Cldr` backend module.
## Returns
* `{:ok, locale}` or
* `{:error, {exception, reason}}`
"""
def put_default_locale(locale_name, backend \\ default_backend!()) do
with {:ok, locale} <- validate_locale(locale_name, backend) do
put_default_locale(locale)
end
end
@doc """
Execute a function with a locale ensuring that the
current locale is restored after the function.
## Arguments
* `locale` is any `t:Cldr.LanguageTag.t/0`, tyically returned
by `Cldr.validate_locale/2`.
* `fun` is any 0-arity function or function capture.
## Returns
* The value returned by the function `fun/0` or
* raises an exception if the current locale cannot be
identified.
"""
@doc since: "2.32.0"
@spec with_locale(Cldr.LanguageTag.t(), fun) :: any
def with_locale(%Cldr.LanguageTag{} = locale, fun) when is_function(fun) do
current_locale = get_locale(locale.backend)
try do
put_locale(locale.backend, locale)
fun.()
after
put_locale(locale.backend, current_locale)
end
end
@doc """
Execute a function with a locale ensuring that the
current locale is restored after the function.
## Arguments
* `locale` is any valid locale name returned by `Cldr.known_locale_names/1`.
* `backend` is any module that includes `use Cldr` and therefore
is a `Cldr` backend module. The default is `Cldr.default_backend!/0`.
* `fun` is any 0-arity function or function capture.
## Returns
* The value returned by the function `fun/0` or
* `{:error, {exception, reason}}` if the locale is invalid or
* raises an exception if the current locale cannot be
identified.
"""
@doc since: "2.27.0"
@spec with_locale(Cldr.Locale.locale_name(), backend(), fun) :: any
def with_locale(locale, backend \\ default_backend!(), fun) when is_locale_name(locale) do
with {:ok, locale} = validate_locale(locale, backend) do
with_locale(locale, fun)
end
end
@doc """
Returns the global default `locale` for a
given backend.
## Arguments
* `backend` is any module that includes `use Cldr` and therefore
is a `Cldr` backend module.
## Returns
* The default locale for the backend.
## Example
iex> Cldr.default_locale(TestBackend.Cldr)
%Cldr.LanguageTag{
backend: TestBackend.Cldr,
canonical_locale_name: "en-001",
cldr_locale_name: :"en-001",
language_subtags: [],
extensions: %{},
gettext_locale_name: "en",
language: "en",
locale: %{},
private_use: [],
rbnf_locale_name: :en,
requested_locale_name: "en-001",
script: :Latn,
territory: :"001",
transform: %{},
language_variants: []
}
"""
@spec default_locale(backend()) :: LanguageTag.t()
def default_locale(backend) do
backend.default_locale()
end
@doc """
Returns the configured global default `locale`.
The default locale can be set with
`Cldr.put_default_locale/1`.
Alternatively the default locale may be configured in
`config.exs` under the `ex_cldr` key as follows:
config :ex_cldr,
default_locale: <locale_name>
## Returns
* The default locale or
* Raises an exception if no default
backend is configured
## Notes
`Cldr.default_locale/0` returns the system-wide
default locale.
## Example
iex> Cldr.default_locale
%Cldr.LanguageTag{
backend: TestBackend.Cldr,
canonical_locale_name: "en-001",
cldr_locale_name: :"en-001",
language_subtags: [],
extensions: %{},
gettext_locale_name: "en",
language: "en",
locale: %{},
private_use: [],
rbnf_locale_name: :en,
requested_locale_name: "en-001",
script: :Latn,
territory: :"001",
transform: %{},
language_variants: []
}
"""
def default_locale do
if locale = Application.get_env(@app_name, :_default_locale) do
locale
else
{:ok, locale} = put_default_locale(Cldr.Config.default_locale())
locale
end
end
@default_script :Latn
@doc """
Returns the default script.
## Returns
* The default script which is `#{@default_script}`.
"""
@doc since: "2.31.0"
@spec default_script :: Locale.script()
def default_script do
@default_script
end
@doc """
Returns the default territory when a locale
does not specify one and none can be inferred.
## Arguments
* `backend` is any module that includes `use Cldr` and therefore
is a `Cldr` backend module. The default is `Cldr.default_backend/0`
## Returns
* The default territory or
* Raises if no argument is supplied and there is no
default backend configured
## Example
iex> Cldr.default_territory(TestBackend.Cldr)
:"001"
"""
@spec default_territory(backend()) :: atom()
def default_territory(backend \\ default_backend!()) do
backend.default_territory
end
@doc """
Returns the configured default backend module.
The default backend can be set with
`Cldr.put_default_backend/1`.
Alternatively the default backend may be configured in
`config.exs` under the `ex_cldr` key as follows:
config :ex_cldr,
default_backend: <backend_module>
## Important Note
If this function is called and no default backend
is configured an exception will be raised.
"""
@spec default_backend :: backend() | no_return
@compile {:inline, default_backend!: 0}
def default_backend! do
Cldr.Config.default_backend()
end
@deprecated "Use default_backend!/0"
def default_backend do
default_backend!()
end
@doc """
Set the default system-wide backend module.
## Arguments
* `backend` is any module that includes `use Cldr` and therefore
is a `Cldr` backend module.
## Returns
* `{:ok, backend}` or
* `{:error, {exception, reason}}`
"""
def put_default_backend(backend) do
with {:ok, backend} <- validate_backend(backend) do
:ok = Application.put_env(Cldr.Config.app_name(), :default_backend, backend)
{:ok, backend}
end
end
@doc """
Returns the territory for the world
This is the outermost containment of
territories in CLDR.
CLDR does not yet consider non-terrestrial
territories.
"""
@compile {:inline, the_world: 0}
@the_world :"001"
def the_world do
@the_world
end
@doc """
Return a localsed string for types
that implement the `Cldr.Chars` protocol.
The `Cldr.Chars` protocol is implemented in this
library for `t:Cldr.LanguageTag.t()`.
Other CLDR-related libraries implement
the protocol for the types they support
such as `Float`, `Integer`, `Decimal`,
`Money`, `Unit` and `List`.
"""
@spec to_string(term()) :: String.t()
def to_string(term) do
Cldr.Chars.to_string(term)
end
@doc """
Return a localised string suitable for
presentation purposes for structs that
implement the `Cldr.LanguageTag.DisplayName`
protocol.
The `Cldr.LanguageTag.DisplayName` protocol is
implemented in this library for
`t:Cldr.LanguageTag.t`.
Other CLDR-related libraries implement
the protocol for the types they support
such as `t:Cldr.Unit` and `t:Cldr.Currency`.
"""
@spec display_name(term(), Keyword.t()) :: String.t()
@display_name_options [prefer: :default, compound_locale: true]
def display_name(term, options \\ []) do
options = Keyword.merge(@display_name_options, options)
Cldr.DisplayName.display_name(term, options)
end
@doc """
Validates that a module is a CLDR backend module.
## Arguments
* `backend` is any module name that may be a
`Cldr` backend module.
## Returns
* `{:ok, backend}` is the module if a CLDR backend module or
* `{:error, {exception, reason}}` if the module is unknown or if
the module is not a backend module.
## Examples
iex> Cldr.validate_backend MyApp.Cldr
{:ok, MyApp.Cldr}
iex> Cldr.validate_backend :something_else
{:error,
{Cldr.UnknownBackendError,
"The backend :something_else is not known or not a backend module."}}
"""
@spec validate_backend(backend :: atom()) :: {:ok, atom()} | {:error, {atom(), String.t()}}
def validate_backend(backend) when is_atom(backend) do
if Cldr.Code.ensure_compiled?(backend) && function_exported?(backend, :__cldr__, 1) do
{:ok, backend}
else
{:error, unknown_backend_error(backend)}
end
end
def validate_backend(backend) do
{:error, unknown_backend_error(backend)}
end
defp unknown_backend_error(backend) do
{Cldr.UnknownBackendError,
"The backend #{inspect(backend)} is not known or not a backend module."}
end
@doc false
def validate_backend!(backend) do
case validate_backend(backend) do
{:ok, backend} -> backend
{:error, {exception, reason}} -> raise exception, reason
end
end
@doc """
Normalise and validate a locale name.
## Arguments
* `locale` is any valid locale name returned by `Cldr.known_locale_names/1`
or a `Cldr.LanguageTag` struct returned by `Cldr.Locale.new!/2`
* `backend` is any module that includes `use Cldr` and therefore
is a `Cldr` backend module. The default is `Cldr.default_backend!/0`.
Note that `Cldr.default_backend!/0` will raise an exception if
no `:default_backend` is configured under the `:ex_cldr` key in
`config.exs`.
## Returns
* `{:ok, language_tag}`
* `{:error, reason}`
## Examples
iex> Cldr.validate_locale(:en, TestBackend.Cldr)
{:ok,
%Cldr.LanguageTag{
backend: TestBackend.Cldr,
canonical_locale_name: "en",
cldr_locale_name: :en,
extensions: %{},
gettext_locale_name: "en",
language: "en",
locale: %{},
private_use: [],
rbnf_locale_name: :en,
requested_locale_name: "en",
script: :Latn,
territory: :US,
transform: %{},
language_variants: []
}}
iex> Cldr.validate_locale(:af, TestBackend.Cldr)
{:ok,
%Cldr.LanguageTag{
backend: TestBackend.Cldr,
canonical_locale_name: "af",
cldr_locale_name: :af,
extensions: %{},
gettext_locale_name: nil,
language: "af",
locale: %{},
private_use: [],
rbnf_locale_name: :af,
requested_locale_name: "af",
script: :Latn,
territory: :ZA,
transform: %{},
language_variants: []
}}
iex> Cldr.validate_locale("zzz", TestBackend.Cldr)
{:error, {Cldr.InvalidLanguageError, "The language \\"zzz\\" is invalid"}}
"""
@spec validate_locale(Locale.locale_name() | LanguageTag.t() | String.t(), backend()) ::
{:ok, LanguageTag.t()} | {:error, {module(), String.t()}}
def validate_locale(locale, backend \\ nil)
def validate_locale(%LanguageTag{} = locale, nil) do
{:ok, locale}
end
def validate_locale(locale, nil) do
validate_locale(locale, Cldr.default_backend!())
end
def validate_locale(locale, backend) do
backend.validate_locale(locale)
end
@doc """
Returns a list of all the locale names defined in
the CLDR repository.
Note that not necessarily all of these locales are
available since functions are only generated for configured
locales which is most cases will be a subset of locales
defined in CLDR.
See also: `requested_locales/1` and `known_locales/1`
"""
@all_locale_names Config.all_locale_names()
@spec all_locale_names :: [Locale.locale_name(), ...]
def all_locale_names do
@all_locale_names
end
@doc """
Returns a list of all requested locale names.
## Arguments
* `backend` is any module that includes `use Cldr` and therefore
is a `Cldr` backend module. The default is `Cldr.default_backend!/0`.
Note that `Cldr.default_backend!/0` will raise an exception if
no `:default_backend` is configured under the `:ex_cldr` key in
`config.exs`.
The list is the combination of configured locales,
`Gettext` locales and the default locale.
See also `known_locales/1` and `all_locales/0`
"""
@spec requested_locale_names(backend()) :: [Locale.locale_name(), ...] | []
def requested_locale_names(backend \\ default_backend!()) do
backend.requested_locale_names
end
@doc """
Returns a list of the known locale names.
## Arguments
* `backend` is any module that includes `use Cldr` and therefore
is a `Cldr` backend module. The default is `Cldr.default_backend!/0`.
Note that `Cldr.default_backend!/0` will raise an exception if
no `:default_backend` is configured under the `:ex_cldr` key in
`config.exs`.
Known locales are those locales which
are the subset of all CLDR locales that
have been configured for use either
directly in the `config.exs` file or
in `Gettext`.
"""
@spec known_locale_names(backend()) :: [Locale.locale_name(), ...] | []
def known_locale_names(backend \\ default_backend!()) do
backend.known_locale_names
end
@doc """
Returns a list of the locales names that are configured,
but not known in CLDR.
## Arguments
* `backend` is any module that includes `use Cldr` and therefore
is a `Cldr` backend module. The default is `Cldr.default_backend!/0`.
Note that `Cldr.default_backend!/0` will raise an exception if
no `:default_backend` is configured under the `:ex_cldr` key in
`config.exs`.
Since there is a compile-time exception raise if there are
any unknown locales this function should always
return an empty list.
"""
@spec unknown_locale_names(backend()) :: [Locale.locale_name(), ...] | []
def unknown_locale_names(backend \\ default_backend!()) do
backend.unknown_locale_names
end
@doc """
Returns a list of locale names which have rules based number
formats (RBNF).
## Arguments
* `backend` is any module that includes `use Cldr` and therefore
is a `Cldr` backend module
"""
@spec known_rbnf_locale_names(backend()) :: [Locale.locale_name(), ...] | []
def known_rbnf_locale_names(backend \\ default_backend!()) do
backend.known_rbnf_locale_names
end
@doc """
Returns a list of Gettext locale names but in CLDR format with
underscore replaced by hyphen in order to facilitate comparisons
with Cldr locale names.
## Arguments
* `backend` is any module that includes `use Cldr` and therefore
is a `Cldr` backend module. The default is `Cldr.default_backend!/0`.
Note that `Cldr.default_backend!/0` will raise an exception if
no `:default_backend` is configured under the `:ex_cldr` key in
`config.exs`.
"""
@spec known_gettext_locale_names(backend()) :: [Locale.locale_name(), ...] | []
def known_gettext_locale_names(backend \\ default_backend!()) do
backend.known_gettext_locale_names
end
@doc """
Returns either the `locale_name` or `false` based upon
whether the locale name is configured in `Cldr`.
This is helpful when building a list of `or` expressions
to return the first known locale name from a list.
## Arguments
* `locale` is any valid locale name returned by `Cldr.known_locale_names/1`
* `backend` is any module that includes `use Cldr` and therefore
is a `Cldr` backend module. The default is `Cldr.default_backend!/0`.
Note that `Cldr.default_backend!/0` will raise an exception if
no `:default_backend` is configured under the `:ex_cldr` key in
`config.exs`.
## Examples
iex> Cldr.known_locale_name(:"en-AU", TestBackend.Cldr)
:"en-AU"
iex> Cldr.known_locale_name(:"en-SA", TestBackend.Cldr)
nil
"""
@spec known_locale_name(Locale.locale_name() | String.t(), backend()) :: atom() | nil
def known_locale_name(locale_name, backend \\ default_backend!())
def known_locale_name(locale_name, backend) when is_atom(locale_name) do
if name = backend.known_locale_name(locale_name) do
name
else
nil
end
end
# TODO remove when we get to Cldr 3.0
def known_locale_name(locale_name, backend) when is_binary(locale_name) do
locale_name = String.to_existing_atom(locale_name)
known_locale_name(locale_name, backend)
rescue
ArgumentError ->
nil
end
@doc """
Returns a boolean indicating if the specified locale
name is configured and available in Cldr.
## Arguments
* `locale` is any valid locale name returned by `Cldr.known_locale_names/1`
* `backend` is any module that includes `use Cldr` and therefore