-
Notifications
You must be signed in to change notification settings - Fork 34
/
config.ex
2775 lines (2272 loc) · 76.2 KB
/
config.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.Config do
@moduledoc """
Provider configuration support
functions for ex_cldr.
The functions in this module should
be considered private use and they
may change at any time.
"""
alias Cldr.Locale
alias Cldr.LanguageTag
defstruct default_locale: "en-001",
locales: ["en-001"],
add_fallback_locales: false,
backend: nil,
gettext: nil,
data_dir: "cldr",
providers: nil,
precompile_number_formats: [],
precompile_transliterations: [],
precompile_date_time_formats: [],
precompile_interval_formats: [],
default_currency_format: nil,
otp_app: nil,
generate_docs: true,
suppress_warnings: false,
message_formats: %{},
force_locale_download: false,
https_proxy: nil
@type t :: %__MODULE__{
default_locale: String.t(),
locales: [binary(), ...] | :all,
add_fallback_locales: boolean(),
backend: module(),
gettext: module() | nil,
data_dir: String.t(),
precompile_number_formats: [String.t(), ...],
precompile_transliterations: [{atom(), atom()}, ...],
precompile_date_time_formats: [String.t(), ...],
precompile_interval_formats: [String.t(), ...],
default_currency_format: :currency | :accounting | nil,
otp_app: atom() | nil,
providers: [atom(), ...],
generate_docs: boolean(),
suppress_warnings: boolean(),
message_formats: map(),
force_locale_download: boolean,
https_proxy: String.t() | nil
}
@type number_system :: atom() | String.t()
@root_locale_name :und
@default_locale_name :"en-001"
@cldr_modules [
"version",
"number_formats",
"list_formats",
"currencies",
"number_systems",
"number_symbols",
"minimum_grouping_digits",
"rbnf",
"units",
"date_fields",
"dates",
"territories",
"languages",
"delimiters",
"ellipsis",
"lenient_parse",
"locale_display_names",
"subdivisions",
"person_names",
"layout"
]
@doc false
# Integer keys cater for 60 year cycles and 239 japanese eras
@keys_to_integerize Enum.map(-2..255, &to_string/1)
def keys_to_integerize do
@keys_to_integerize
end
# These delegates are here for backwards compatibility
# and will be removed when the data is
@doc false
@deprecated "Use Cldr.Locale.Loader.get_locale/2"
defdelegate get_locale(locale, config), to: Cldr.Locale.Loader
@doc false
defdelegate underscore(string), to: Cldr.Locale.Loader
def root_locale_name do
@root_locale_name
end
def include_module_docs?(false) do
false
end
def include_module_docs?(_flag) do
true
end
@doc false
@non_language_locale_names []
def non_language_locale_names do
@non_language_locale_names
end
@doc false
defmacro is_alphabetic(char) do
quote do
unquote(char) in ?a..?z or unquote(char) in ?A..?Z
end
end
@doc """
Return the configured application name
for cldr
Note this is probably `:ex_cldr` which is
what this app is called on `hex.pm`
"""
@app_name Mix.Project.config()[:app]
def app_name do
@app_name
end
poison = if(Code.ensure_loaded?(Poison), do: Poison, else: nil)
jason = if(Code.ensure_loaded?(Jason), do: Jason, else: nil)
cldr_json = Application.compile_env(:ex_cldr, :json_library)
@json_lib cldr_json || jason || poison
cond do
Code.ensure_loaded?(@json_lib) and function_exported?(@json_lib, :decode!, 1) ->
:ok
cldr_json ->
raise ArgumentError,
"Could not load configured :json_library, " <>
"make sure #{inspect(cldr_json)} is listed as a dependency"
true ->
raise ArgumentError, """
A JSON library has not been configured.\n
Please configure a JSON lib in your `mix.exs`
file. The suggested library is `:jason`.
For example in your `mix.exs`:
def deps() do
[
{:jason, "~> 1.0"},
...
]
end
You can then configure this library for `ex_cldr`
in your `config.exs` as follows:
config :ex_cldr,
json_library: Jason
If no configuration is provided, `ex_cldr` will
attempt to detect detect if Jason or Poison are
configured.
"""
end
@doc """
Return the configured json lib
"""
def json_library do
@json_lib
end
@doc """
Return the root path of the cldr application
"""
def cldr_home do
Path.join(__DIR__, "/../../..") |> Path.expand()
end
@doc """
Returns the directory where the downloaded CLDR repository files
are stored.
These are the real CLDR data files that are used only for the
development of ex_cldr. They are not included in the hex
package.
"""
@production_location "CLDR_PRODUCTION"
def download_data_dir do
production_data_location() ||
raise(ArgumentError, """
The environment variable $CLDR_PRODUCTION must be set to the
directory where the CLDR json data is stored.
See DEVELOPMENT.md for more information about CLDR data
and generating the json files.
""")
end
@doc false
def production_data_location do
location = System.get_env(@production_location, "")
if File.exists?(location), do: location, else: nil
end
@doc """
Return the directory where `Cldr` stores its source core data,
This directory should not be expected to be available
other than when developing Cldr since it points to a source
directory.
These are the json files that result from the normalization
of the original CLDR data.
"""
@cldr_source_dir "/priv/cldr" |> Path.expand()
def source_data_dir do
Path.join(cldr_home(), @cldr_source_dir)
end
@doc """
Returns the path of the CLDR data directory for the ex_cldr app.
This is the directory where base CLDR data files are stored
including included locale files.
"""
def cldr_data_dir do
[:code.priv_dir(app_name()), "/cldr"] |> :erlang.iolist_to_binary()
end
@doc """
Return the path name of the CLDR data directory for a client
application.
The order of priority to determine where the client data
directory is located is:
* A specified `:data_dir` of a backend configuration
* The specified `:data_dir` of the backend's `:otp_app` configuration
* The specified `:data_dir` of the global configuration
* The `priv_dir()` of a specified `:otp_app`
* The `priv_dir()` of `ex_cldr`
Note that `config_from_opts/1` merges the global config,
the otp_app config and the module config together so
that `:data_dir` already resolves this priority in most
cases.
The remaining cases are for when no `:data_dir` is
specified.
"""
@spec client_data_dir(map()) :: String.t()
def client_data_dir(%{data_dir: data_dir}) when not is_nil(data_dir) do
data_dir
end
def client_data_dir(%{otp_app: nil}) do
cldr_data_dir()
end
def client_data_dir(%{otp_app: otp_app}) do
case :code.priv_dir(otp_app) do
{:error, :bad_name} ->
raise Cldr.UnknownOTPAppError, "The configured OTP app #{inspect(otp_app)} is not known"
priv_dir ->
[priv_dir, "/cldr"] |> :erlang.iolist_to_binary()
end
end
def client_data_dir(config) when is_map(config) do
client_data_dir(%{data_dir: nil, otp_app: nil})
end
def client_data_dir(backend) when is_atom(backend) do
client_data_dir(backend.__cldr__(:config))
end
@doc """
Returns the directory where downloaded ex_cldr locales files
are located.
"""
def client_locales_dir(config) do
Path.join(client_data_dir(config), "locales")
end
@doc """
Returns the version string of the CLDR data repository
"""
def version do
cldr_data_dir()
|> Path.join("version.json")
|> File.read!()
|> json_library().decode!
end
@doc """
Returns the filename that contains the json representation
of a locale.
"""
def locale_filename(locale) do
"#{locale}.json"
end
@doc """
Return the configured `Gettext` module name or `nil`.
"""
@spec gettext(t() | map()) :: module() | nil
def gettext(%{} = config) do
Map.get(config, :gettext)
end
@doc """
Return the default locale name for a given backend
configuration.
In order of priority return either:
* The default locale for a given backend configuration
* The global default locale specified in `mix.exs` under
the `ex_cldr` key
* The `Gettext.get_locale/1` for the current configuration
* The system-wide default locale which is currently
#{inspect(@default_locale_name)}
"""
@spec default_locale_name(t() | map()) :: Locale.locale_name()
def default_locale_name(%{} = config) do
default =
Map.get(config, :default_locale) ||
Application.get_env(app_name(), :default_locale) ||
gettext_default_locale(config) ||
@default_locale_name
locale_name_from_posix(default)
|> String.to_atom()
end
@doc """
Return the system-wide default locale.
"""
def default_locale do
Application.get_env(app_name(), :default_locale, @default_locale_name)
|> locale_name_from_posix()
end
@doc """
Return the system-wide default backend
"""
def default_backend do
Application.get_env(app_name(), :default_backend) ||
raise(Cldr.NoDefaultBackendError, "No default #{inspect(app_name())} backend is configured")
end
@doc """
Return the default gettext locale for a CLDR
config.
"""
def gettext_default_locale(config) do
if gettext_configured?(config) do
Gettext
|> apply(:get_locale, [gettext(config)])
|> locale_name_from_posix()
else
nil
end
end
@doc """
Return a list of the locales defined in `Gettext`.
Return a list of locales configured in `Gettext` or
`[]` if `Gettext` is not configured.
"""
@spec known_gettext_locale_names(t()) :: [Locale.locale_name()]
def known_gettext_locale_names(config) do
if gettext_configured?(config) do
otp_app = gettext(config).__gettext__(:otp_app)
gettext_backend = gettext(config)
backend_default =
if backend_config = Application.get_env(otp_app, gettext_backend) do
Keyword.get(backend_config, :default_locale)
else
nil
end
global_default = Application.get_env(:gettext, :default_locale)
locales =
apply(Gettext, :known_locales, [gettext_backend]) ++
[backend_default, global_default]
locales
|> Enum.reject(&is_nil/1)
|> Enum.map(&locale_name_from_posix/1)
|> Enum.uniq()
|> Enum.sort()
else
[]
end
end
@doc """
Returns a list of all locales in the CLDR repository.
Returns a list of the complete locales list in CLDR, irrespective
of whether they are configured for use in the application.
Any configured locales that are not present in this list will
raise an exception at compile time.
"""
@available_locales_file "available_locales.json"
@spec all_locale_names :: [Locale.locale_name(), ...]
def all_locale_names do
Path.join(cldr_data_dir(), @available_locales_file)
|> File.read!()
|> json_library().decode!
|> Enum.map(&String.to_atom/1)
|> Enum.sort()
end
@doc """
Add the fallback locales to a list of
configured locales
"""
def maybe_add_fallback_locales(%__MODULE__{add_fallback_locales: false} = config) do
config
end
def maybe_add_fallback_locales(%__MODULE__{} = config) do
expanded_locales =
config.locales
|> Enum.flat_map(&fallback_chain/1)
|> Kernel.++(config.locales)
|> Enum.uniq()
|> Enum.sort()
%{config | locales: expanded_locales}
end
@doc """
Returns the fallback chain for a
locale name. Follows the CLDR [TR35](https://unicode.org/reports/tr35/tr35.html#Bundle_vs_Item_Lookup)
resource bundle lookup algorithm.
This function is only intended to
return fallback chains for the locales
defined by CLDR. It does not perform
any alias lookup or likely subtag
processing.
The primary purpose for this function is
to support including fallback locales
in a backend configuration since both
RBNF and Subdivision data follows the
fallback chain.
## Algorithm Summary
1. Decompose the locale name into language,
script, territory and variant. CLDR locale
names have no more than these four parts but
usually have less.
2. Look for a locale in the following order:
* language-script-territory
* language-script
* language-territory
* language
3. At each stage in (2) resolve
an alias in `parent_locales/1`
"""
def fallback_chain(locale_name) do
locale_name
|> fallback_chain([])
|> Enum.reverse()
end
@doc false
def fallback_chain(locale_name, acc) do
case fallback(locale_name) do
nil -> acc
fallback -> fallback_chain(fallback, [fallback | acc])
end
end
@doc """
Returns the immediate fallback locale for a
locale name. Follows the CLDR [TR35](https://unicode.org/reports/tr35/tr35.html#Bundle_vs_Item_Lookup)
resource bundle lookup algorithm.
This function is only intended to
return the fallback for the locales
defined by CLDR. It does not perform
any alias lookup or likely subtag
processing.
## Algorithm Summary
1. Decompose the locale name into language,
script, territory and variant. CLDR locale
names have no more than these four parts but
usually have less.
2. Look for a locale in the following order:
* language-script-territory
* language-script
* language-territory
* language
3. At each stage in (2) resolve
an alias in `parent_locales/1`
"""
def fallback(locale_name) do
all_locale_names = all_locale_names()
fun = fn locale_name ->
locale_name in all_locale_names && locale_name
end
if inherited = Map.get(parent_locales(), locale_name) do
inherited
else
{:ok, locale} = Cldr.LanguageTag.Parser.parse(to_string(locale_name))
first_match(locale.language, locale.script, locale.territory, fun)
end
end
defp first_match(_language, nil, nil, _fun) do
nil
end
defp first_match(language, _script, nil, fun) do
fun.(locale_name_from(language, nil, nil, [])) || nil
end
defp first_match(language, nil, _territory, fun) do
fun.(locale_name_from(language, nil, nil, [])) || nil
end
defp first_match(language, script, territory, fun) do
fun.(locale_name_from(language, script, nil, [])) ||
fun.(locale_name_from(language, nil, territory, [])) ||
fun.(locale_name_from(language, nil, nil, [])) || nil
end
@doc false
def locale_name_from(language, script, territory, variants, omit_singular_script? \\ true) do
[language, script, territory, join_variants(variants)]
|> omit_script_if_only_one(omit_singular_script?)
|> Enum.reject(&is_nil/1)
|> Enum.join("-")
end
@doc false
def join_variants([]), do: nil
def join_variants(variants),
do: variants |> Enum.sort() |> Enum.join("-")
# If the language has only one script for a given territory then
# we omit it in the canonical form
defp omit_script_if_only_one([_language, nil, _territory, _variants] = tag, _) do
tag
end
# If the language has only one script for a given territory then
# we omit it in the canonical form
defp omit_script_if_only_one([language, script, territory, variants], true) do
language_map = Map.get(language_data(), language, %{})
script = maybe_nil_script(Map.get(language_map, :primary), script, territory)
[language, script, territory, variants]
end
defp omit_script_if_only_one([language, script, territory, variants], false) do
[language, script, territory, variants]
end
# No :secondary
defp maybe_nil_script(nil, _script, _territory) do
nil
end
# There is only one script for this territory and its the requested one
# so its not required for the canonical form
defp maybe_nil_script(%{scripts: [script], territories: _territories}, script, _territory) do
nil
end
# In all other cases we keep the script
defp maybe_nil_script(%{scripts: _scripts, territories: _territories}, script, _territory) do
script
end
@doc """
Returns the map of language tags for all
available locales
"""
@tag_file "language_tags.ebin"
def all_language_tags do
Path.join(cldr_data_dir(), @tag_file)
|> File.read!()
|> :erlang.binary_to_term()
rescue
_e in File.Error -> %{}
end
@doc """
Return a map of validity data for a given
type.
The types are `:languages`, `:scripts`,
`:territories`, `:subdivisions`, `:variants`
`:units`, `:u`, and `:t`
"""
@validity_type [:languages, :scripts, :territories, :subdivisions, :variants]
def validity(type) when type in @validity_type do
Path.join(cldr_data_dir(), "validity/#{type}.json")
|> File.read!()
|> json_library().decode!
|> Cldr.Map.atomize_keys()
|> Map.new()
end
def validity(:units) do
Path.join(cldr_data_dir(), "validity/units.json")
|> File.read!()
|> json_library().decode!
|> Enum.map(fn {status, list} ->
list =
Enum.map(list, fn v ->
case String.split(v, "-", parts: 2) do
[v] -> v
[_category, unit] -> underscores(unit)
end
end)
{status, list}
end)
|> Cldr.Map.atomize_keys()
|> Map.new()
end
def validity(:u) do
Path.join(cldr_data_dir(), "bcp47/u.json")
|> File.read!()
|> json_library().decode!
|> Cldr.Map.deep_map(fn
k when is_binary(k) -> k
{k, "quaternary quarternary"} -> {k, "quaternary"}
{k, v} when is_binary(v) -> {underscores(k), underscores(v)}
{k, v} -> {underscores(k), v}
end)
end
def validity(:t) do
Path.join(cldr_data_dir(), "bcp47/t.json")
|> File.read!()
|> json_library().decode!
|> Cldr.Map.deep_map(fn
k when is_binary(k) -> k
{k, v} when is_binary(v) -> {underscores(k), underscores(v)}
{k, v} -> {underscores(k), v}
end)
end
@doc """
Return a map of plural ranges
"""
@plural_range_file "plural_ranges.json"
def plural_ranges do
Path.join(cldr_data_dir(), @plural_range_file)
|> File.read!()
|> json_library().decode!
|> Enum.map(fn
%{"locales" => locales, "ranges" => ranges} ->
%{
locales: locales,
ranges:
Enum.map(ranges, fn
range -> Cldr.Map.atomize_keys(range) |> Cldr.Map.atomize_values()
end)
}
end)
end
@doc """
Returns a mapping from Unicode script names to the
respective subtag used in BCP 47 language tags.
"""
@script_metadata_path "script_metadata.csv"
def unicode_script_to_subtag_mapping do
Path.join(cldr_data_dir(), @script_metadata_path)
|> File.read!()
|> String.split("\n")
|> tl
|> tl
|> Enum.map(fn line ->
[_number, name, code | _rest] = String.split(line,",")
name =
name
|> String.replace(" ", "")
|> Cldr.String.underscore()
|> String.to_atom()
code =
String.to_atom(code)
{name, code}
end)
|> Map.new
end
@doc """
Return a map of measurement systems
"""
@measurement_systems_file "measurement_systems.json"
def measurement_systems do
Path.join(cldr_data_dir(), @measurement_systems_file)
|> File.read!()
|> json_library().decode!
|> Cldr.Map.atomize_keys()
|> Cldr.Map.deep_map(fn
{:description, description} -> {:description, description}
{:alias, ""} -> {:alias, nil}
{k, v} when is_binary(v) -> {k, String.to_atom(v)}
other -> other
end)
|> Map.new()
end
@doc """
Return the language data that maps
valid territories and scripts
"""
@language_data_file "language_data.json"
def language_data do
Path.join(cldr_data_dir(), @language_data_file)
|> File.read!()
|> json_library().decode!
|> Cldr.Map.atomize_keys(level: 2..1000)
|> Cldr.Map.deep_map(fn
{:territories = k, list} -> {k, Enum.map(list, &String.to_atom/1)}
{:scripts = k, list} -> {k, Enum.map(list, &String.to_atom/1)}
other -> other
end)
end
@doc """
Returns a boolean indicating whether the language_tags.ebin
file exists
"""
def all_language_tags? do
cldr_data_dir()
|> Path.join(@tag_file)
|> File.exists?()
end
@doc """
Return the saved language tag for the
given locale name.
"""
@spec language_tag(Locale.locale_name()) :: Cldr.LanguageTag.t() | no_return()
# Even if compiling its possible, at the beginning, that
# the cache server isn't fully up and runing to we return a
# non-cached version is absolutely necessary
def language_tag(locale_name) do
if Cldr.Locale.Cache.compiling?() do
Cldr.Locale.Cache.get_language_tag(locale_name) ||
Map.fetch!(all_language_tags(), locale_name)
else
Map.fetch!(all_language_tags(), locale_name)
end
end
@doc """
Returns a list of all locales configured in a
`Cldr.Config.t` struct.
In order of priority return either:
* The list of locales configured configured in mix.exs if any
* The default locale
If the configured locales is `:all` then all locales
in CLDR are configured.
The locale "und" is always added to the list of configured locales since it
is required to support some RBNF functions.
The use of `:all` is not recommended since all 571 locales take
quite some time (minutes) to compile. It is however
helpful for testing Cldr.
"""
@spec configured_locale_names(t()) :: [Locale.locale_name()]
def configured_locale_names(config) do
app_locale_names =
config
|> Map.get(:locales)
locale_names =
case app_locale_names do
:all -> all_locale_names()
nil -> expand_locale_names([default_locale_name(config)])
_ -> expand_locale_names(app_locale_names)
end
[@root_locale_name | locale_names]
|> Enum.uniq()
|> Enum.sort()
end
@doc """
Returns a list of all locales that are configured and available
in the CLDR repository.
"""
@spec known_locale_names(t() | Cldr.backend()) :: [Locale.locale_name()]
@deprecated "Use Cldr.Locale.Loader.known_locale_names/1"
defdelegate known_locale_names(config), to: Cldr.Locale.Loader
@doc """
Returns a list of all locales that have RBNF data and that are
configured and available in the CLDR repository.
"""
@spec known_rbnf_locale_names(t()) :: [Locale.locale_name()]
@deprecated "Use Cldr.Locale.Loader.known_rbnf_locale_names/1"
defdelegate known_rbnf_locale_names(config), to: Cldr.Locale.Loader
@doc """
Returns either the locale name (if its known)
or `false` if the locale name is not known.
"""
def known_locale_name(locale_name, config) do
if locale_name in known_locale_names(config) do
locale_name
else
false
end
end
@doc """
Returns either the locale name (if its known
and has an rbnf configuration)
or `false`.
"""
def known_rbnf_locale_name(locale_name, config) do
if locale_name in known_rbnf_locale_names(config) do
locale_name
else
false
end
end
@doc """
Returns a list of all locales that are configured but not available
in the CLDR repository.
"""
@spec unknown_locale_names(t()) :: [Locale.locale_name()]
def unknown_locale_names(%__MODULE__{locales: :all}) do
[]
end
def unknown_locale_names(%__MODULE__{locales: locales}) do
locales
|> MapSet.new()
|> MapSet.difference(MapSet.new(all_locale_names()))
|> MapSet.to_list()
|> Enum.sort()
end
@doc """
Returns a list of all configured locales.
The list contains locales configured both in `Gettext` and
specified in the mix.exs configuration file as well as the
default locale.
"""
@spec requested_locale_names(t()) :: [Locale.locale_name()]
def requested_locale_names(config) do
locales =
configured_locale_names(config) ++
known_gettext_locale_names(config) ++
[default_locale_name(config)]
locales
|> Enum.uniq()
|> Enum.sort()
end
@doc """
Returns a list of strings representing the calendars known to `Cldr`.
## Example
iex> Cldr.Config.known_calendars
[:buddhist, :chinese, :coptic, :dangi, :ethiopic, :ethiopic_amete_alem,
:gregorian, :hebrew, :indian, :islamic, :islamic_civil, :islamic_rgsa,
:islamic_tbla, :islamic_umalqura, :japanese, :persian, :roc]
"""
def known_calendars do
calendars() |> Map.keys() |> Enum.sort()
end
@doc """
Returns a list of the currencies known in `Cldr` in
upcased atom format.
## Example
iex> Cldr.Config.known_currencies
[:ADP, :AED, :AFA, :AFN, :ALK, :ALL, :AMD, :ANG, :AOA, :AOK, :AON, :AOR, :ARA,
:ARL, :ARM, :ARP, :ARS, :ATS, :AUD, :AWG, :AZM, :AZN, :BAD, :BAM, :BAN, :BBD,
:BDT, :BEC, :BEF, :BEL, :BGL, :BGM, :BGN, :BGO, :BHD, :BIF, :BMD, :BND, :BOB,
:BOL, :BOP, :BOV, :BRB, :BRC, :BRE, :BRL, :BRN, :BRR, :BRZ, :BSD, :BTN, :BUK,
:BWP, :BYB, :BYN, :BYR, :BZD, :CAD, :CDF, :CHE, :CHF, :CHW, :CLE, :CLF, :CLP,
:CNH, :CNX, :CNY, :COP, :COU, :CRC, :CSD, :CSK, :CUC, :CUP, :CVE, :CYP, :CZK,
:DDM, :DEM, :DJF, :DKK, :DOP, :DZD, :ECS, :ECV, :EEK, :EGP, :ERN, :ESA, :ESB,
:ESP, :ETB, :EUR, :FIM, :FJD, :FKP, :FRF, :GBP, :GEK, :GEL, :GHC, :GHS, :GIP,
:GMD, :GNF, :GNS, :GQE, :GRD, :GTQ, :GWE, :GWP, :GYD, :HKD, :HNL, :HRD, :HRK,
:HTG, :HUF, :IDR, :IEP, :ILP, :ILR, :ILS, :INR, :IQD, :IRR, :ISJ, :ISK, :ITL,
:JMD, :JOD, :JPY, :KES, :KGS, :KHR, :KMF, :KPW, :KRH, :KRO, :KRW, :KWD, :KYD,
:KZT, :LAK, :LBP, :LKR, :LRD, :LSL, :LTL, :LTT, :LUC, :LUF, :LUL, :LVL, :LVR,
:LYD, :MAD, :MAF, :MCF, :MDC, :MDL, :MGA, :MGF, :MKD, :MKN, :MLF, :MMK, :MNT,
:MOP, :MRO, :MRU, :MTL, :MTP, :MUR, :MVP, :MVR, :MWK, :MXN, :MXP, :MXV, :MYR,
:MZE, :MZM, :MZN, :NAD, :NGN, :NIC, :NIO, :NLG, :NOK, :NPR, :NZD, :OMR, :PAB,
:PEI, :PEN, :PES, :PGK, :PHP, :PKR, :PLN, :PLZ, :PTE, :PYG, :QAR, :RHD, :ROL,
:RON, :RSD, :RUB, :RUR, :RWF, :SAR, :SBD, :SCR, :SDD, :SDG, :SDP, :SEK, :SGD,
:SHP, :SIT, :SKK, :SLE, :SLL, :SOS, :SRD, :SRG, :SSP, :STD, :STN, :SUR, :SVC,
:SYP, :SZL, :THB, :TJR, :TJS, :TMM, :TMT, :TND, :TOP, :TPE, :TRL, :TRY, :TTD,
:TWD, :TZS, :UAH, :UAK, :UGS, :UGX, :USD, :USN, :USS, :UYI, :UYP, :UYU, :UYW,
:UZS, :VEB, :VED, :VEF, :VES, :VND, :VNN, :VUV, :WST, :XAF, :XAG, :XAU, :XBA,
:XBB, :XBC, :XBD, :XCD, :XDR, :XEU, :XFO, :XFU, :XOF, :XPD, :XPF, :XPT, :XRE,
:XSU, :XTS, :XUA, :XXX, :YDD, :YER, :YUD, :YUM, :YUN, :YUR, :ZAL, :ZAR, :ZMK,
:ZMW, :ZRN, :ZRZ, :ZWD, :ZWL, :ZWR]
"""
def known_currencies do
cldr_data_dir()
|> Path.join("currencies.json")
|> File.read!()
|> json_library().decode!
|> Enum.sort()
|> Enum.map(&String.to_atom/1)
end
@doc """
Returns a list of strings representing the number systems known to `Cldr`.
## Example