-
Notifications
You must be signed in to change notification settings - Fork 5
/
currency.ex
1201 lines (925 loc) · 32.9 KB
/
currency.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.Currency do
@moduledoc """
Defines a currency structure and a set of functions to manage the validity of a currency code
and to return metadata for currencies.
"""
alias Cldr.Locale
alias Cldr.LanguageTag
@type format ::
:standard
| :accounting
| :short
| :long
| :percent
| :scientific
@type code :: atom()
@type currency_status :: :all | :current | :historic | :tender | :unannotated | :private
@type filter :: list(currency_status | code) | currency_status | code
@type t :: %__MODULE__{
code: code,
alt_code: code,
name: String.t(),
tender: boolean,
symbol: String.t(),
digits: non_neg_integer,
rounding: non_neg_integer,
narrow_symbol: String.t(),
cash_digits: non_neg_integer,
cash_rounding: non_neg_integer,
iso_digits: non_neg_integer,
count: %{},
from: Calendar.year(),
to: Calendar.year()
}
defstruct code: nil,
alt_code: nil,
name: "",
symbol: "",
narrow_symbol: nil,
digits: 0,
rounding: 0,
cash_digits: 0,
cash_rounding: 0,
iso_digits: 0,
tender: false,
count: nil,
from: nil,
to: nil
alias Cldr.LanguageTag
# Starts the supervisor for the private use
# currencies, delegated to Eternal which
# keeps :ets tables alive as much as is
# possible
@doc false
def start_link(_init_arg) do
start_link()
end
@doc false
def start_link do
Eternal.start_link(__MODULE__, [ :set, { :read_concurrency, true }], [ quiet: true ])
end
@doc false
def child_spec(opts) do
%{
id: __MODULE__,
start: {__MODULE__, :start_link, [opts]},
type: :supervisor,
restart: :permanent,
shutdown: 500
}
end
@doc """
Returns a `Currency` struct created from the arguments.
## Arguments
* `currency` is a custom currency code of a format defined in ISO4217
* `options` is a map of options representing the optional elements of
the `t:Cldr.Currency.t` struct
## Options
* `:name` is the name of the currenct. Required.
* `:digits` is the precision of the currency. Required.
* `:symbol` is the currency symbol. Optional.
* `:narrow_symbol` is an alternative narrow symbol. Optional.
* `:round_nearest` is the rounding precision such as `0.05`.
Optional.
* `:alt_code` is an alternative currency code for application use.
* `:cash_digits` is the precision of the currency when used as cash.
Optional.
* `:cash_round_nearest` is the rounding precision when used as cash
such as `0.05`. Optional.
## Returns
* `{:ok, Cldr.Currency.t}` or
* `{:error, {exception, message}}`
## Example
iex> Cldr.Currency.new(:XAA, name: "XAA currency", digits: 0)
{:ok,
%Cldr.Currency{
alt_code: :XAA,
cash_digits: 0,
cash_rounding: nil,
code: :XAA,
count: %{other: "XAA currency"},
digits: 0,
from: nil,
iso_digits: 0,
name: "XAA currency",
narrow_symbol: nil,
rounding: 0,
symbol: "XAA",
tender: false,
to: nil
}}
iex> Cldr.Currency.new(:XBC)
{:error, {Cldr.CurrencyAlreadyDefined, "Currency :XBC is already defined."}}
iex> Cldr.Currency.new(:ZAA, name: "Invalid Custom Name", digits: 0)
{:error, {Cldr.UnknownCurrencyError, "The currency :ZAA is invalid"}}
"""
@spec new(binary | atom, map | list) :: {:ok, t} | {:error, {module(), String.t}}
def new(currency, options \\ [])
def new(currency, options) when is_list(options) do
with {:ok, currency_code} <- Cldr.validate_currency(currency),
{:ok, currency_code} <- validate_new_currency(currency_code),
{:ok, options} <- validate_options(currency_code, options) do
currency = struct(@struct, [{:code, currency_code} | options])
store_currency(currency)
end
end
defp validate_options(code, options) do
with {:ok, options} <- assert_options(options, [:name, :digits]) do
options = [
code: code,
alt_code: options[:alt_code] || code,
name: options[:name],
symbol: options[:symbol] || to_string(code),
narrow_symbol: options[:narrow_symbol] || options[:symbol],
digits: options[:digits],
rounding: options[:round_nearest] || 0,
cash_digits: options[:cash_digits] || options[:digits],
cash_rounding: options[:cash_round_nearest] || options[:round_nearest],
iso_digits: options[:digits],
tender: options[:tender] || false,
count: options[:count] || %{other: options[:name]}
]
{:ok, options}
end
end
defp assert_options(options, keys) do
if Enum.all?(keys, &options[&1]) do
{:ok, options}
else
{:error, "Required option(s) missing. Required options are #{inspect keys}"}
end
end
@doc """
Determines is a new currency is already
defined.
## Example
iex> Cldr.Currency.validate_new_currency :XAC
{:ok, :XAC}
iex> Cldr.Currency.validate_new_currency :USD
{:error, {Cldr.CurrencyAlreadyDefined, "Currency :USD is already defined."}}
"""
@spec validate_new_currency(code) :: {:ok, code} | {:error, {module, String.t}}
def validate_new_currency(code) do
if code in known_currency_codes() do
{:error, {Cldr.CurrencyAlreadyDefined, currency_already_defined_error(code)}}
else
{:ok, code}
end
end
def store_currency(%Cldr.Currency{code: code} = currency) do
:ets.insert_new(__MODULE__, {code, currency})
{:ok, currency}
rescue ArgumentError ->
{:error, {Cldr.CurrencyNotSavedError, currency_not_saved_error(code)}}
end
@doc """
Returns the appropriate currency display name for the `currency`, based
on the plural rules in effect for the `locale`.
## Arguments
* `number` is an integer, float or `Decimal`
* `currency` is any currency returned by `Cldr.Currency.known_currencies/0`
* `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
* `options` is a keyword list of options
## Options
* `:locale` is any locale returned by `Cldr.Locale.new!/2`. The
default is `<backend>.get_locale/1`
## Returns
* `{:ok, plural_string}` or
* `{:error, {exception, message}}`
## Examples
iex> Cldr.Currency.pluralize 1, :USD, MyApp.Cldr
{:ok, "US dollar"}
iex> Cldr.Currency.pluralize 3, :USD, MyApp.Cldr
{:ok, "US dollars"}
iex> Cldr.Currency.pluralize 12, :USD, MyApp.Cldr, locale: "zh"
{:ok, "美元"}
iex> Cldr.Currency.pluralize 12, :USD, MyApp.Cldr, locale: "fr"
{:ok, "dollars des États-Unis"}
iex> Cldr.Currency.pluralize 1, :USD, MyApp.Cldr, locale: "fr"
{:ok, "dollar des États-Unis"}
"""
@spec pluralize(pos_integer, code(), Cldr.backend(), Keyword.t()) ::
{:ok, String.t()} | {:error, {module(), String.t()}}
def pluralize(number, currency, backend, options \\ []) do
locale = Keyword.get_lazy(options, :locale, &backend.get_locale/0)
options = Keyword.put(options, :locale, locale)
with {:ok, currency_code} <- Cldr.validate_currency(currency),
{:ok, locale} <- Cldr.validate_locale(locale, backend),
{:ok, currency_data} <- currency_for_code(currency_code, backend, options) do
counts = Map.get(currency_data, :count)
{:ok, Module.concat(backend, Number.Cardinal).pluralize(number, locale, counts)}
end
end
@doc """
Returns a list of all known currency codes.
## Example
iex> Cldr.Currency.known_currency_codes |> Enum.count
303
"""
@spec known_currency_codes() :: list(atom)
def known_currency_codes do
Cldr.known_currencies() ++ private_currency_codes()
end
@deprecated "Use known_currency_codes/0"
defdelegate known_currencies, to: __MODULE__, as: :known_currency_codes
@doc """
Returns a boolean indicating if the supplied currency code is known.
## Arguments
* `currency_code` is a `binary` or `atom` representing an ISO4217
currency code
## Returns
* `true` or `false`
## Examples
iex> Cldr.Currency.known_currency_code? "AUD"
true
iex> Cldr.Currency.known_currency_code? "GGG"
false
iex> Cldr.Currency.known_currency_code? :XCV
false
"""
@spec known_currency_code?(code()) :: boolean
def known_currency_code?(currency_code) do
with {:ok, currency_code} <- Cldr.validate_currency(currency_code) do
currency_code in known_currency_codes()
else
_other -> false
end
end
@deprecated "Use known_currency_code?/0"
defdelegate known_currency?(currency), to: __MODULE__, as: :known_currency_code?
@doc """
Returns a 2-tuple indicating if the supplied currency code is known.
## Arguments
* `currency_code` is a `binary` or `atom` representing an ISO4217
currency code
## Returns
* `{:ok, currency_code}` or
* `{:error, {exception, reason}}`
## Examples
iex> Cldr.Currency.known_currency_code "AUD"
{:ok, :AUD}
iex> Cldr.Currency.known_currency_code "GGG"
{:error, {Cldr.UnknownCurrencyError, "The currency \\"GGG\\" is invalid"}}
"""
@spec known_currency_code(code()) :: {:ok, code} | {:error, {module, String.t}}
def known_currency_code(currency_code) do
with {:ok, currency_code} <- Cldr.validate_currency(currency_code) do
if currency_code in known_currency_codes() do
{:ok, currency_code}
else
{:error, {Cldr.UnknownCurrencyError, Cldr.unknown_currency_error(currency_code)}}
end
end
end
@doc """
Returns a list of all private currency codes.
"""
@spec private_currency_codes() :: list(atom)
def private_currency_codes do
Map.keys(private_currencies())
end
@doc """
Returns a map of private currencies.
These comprise all currencies created with
`Cldr.Currency.new/2`.
"""
@spec private_currencies :: %{code => t}
def private_currencies do
__MODULE__
|> :ets.tab2list
|> Map.new
rescue ArgumentError ->
%{}
end
@doc """
Returns the effective currency for a given locale
## Arguments
* `locale` is a `Cldr.LanguageTag` struct returned by `Cldr.Locale.new!/2`
## Returns
* A ISO 4217 currency code as an upcased atom
## Examples
iex> {:ok, locale} = Cldr.validate_locale "en", MyApp.Cldr
iex> Cldr.Currency.currency_from_locale locale
:USD
iex> {:ok, locale} = Cldr.validate_locale "en-AU", MyApp.Cldr
iex> Cldr.Currency.currency_from_locale locale
:AUD
iex> Cldr.Currency.currency_from_locale "en-GB"
:GBP
"""
def currency_from_locale(%LanguageTag{locale: %{currency: nil}} = locale) do
current_currency_from_locale(locale)
end
def currency_from_locale(%LanguageTag{locale: %{currency: currency}}) do
currency
end
def currency_from_locale(%LanguageTag{} = locale) do
current_currency_from_locale(locale)
end
@doc """
Returns the effective currency for a given locale
## 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`
## Returns
* A ISO 4217 currency code as an upcased atom
## Examples
iex> Cldr.Currency.currency_from_locale "fr-CH", MyApp.Cldr
:CHF
iex> Cldr.Currency.currency_from_locale "fr-CH-u-cu-INR", MyApp.Cldr
:INR
"""
def currency_from_locale(locale, backend \\ Cldr.default_backend()) when is_binary(locale) do
with {:ok, locale} <- Cldr.validate_locale(locale, backend) do
currency_from_locale(locale)
end
end
@doc """
Returns the effective currency format for a given locale
## Arguments
* `locale` a `Cldr.LanguageTag` struct returned by `Cldr.Locale.new!/2`
## Returns
* Either `:accounting` or `:currency`
## Examples
iex> {:ok, locale} = Cldr.validate_locale "en", MyApp.Cldr
iex> Cldr.Currency.currency_format_from_locale locale
:currency
iex> {:ok, locale} = Cldr.validate_locale "en-AU-u-cu-eur", MyApp.Cldr
iex> Cldr.Currency.currency_format_from_locale locale
:currency
iex> {:ok, locale} = Cldr.validate_locale "en-AU-u-cu-eur-cf-account", MyApp.Cldr
iex> Cldr.Currency.currency_format_from_locale locale
:accounting
"""
def currency_format_from_locale(%LanguageTag{locale: %{currency_format: nil}}) do
:currency
end
def currency_format_from_locale(%LanguageTag{locale: %{currency_format: currency_format}}) do
currency_format
end
def currency_format_from_locale(%LanguageTag{}) do
:currency
end
@doc """
Returns a mapping of all ISO3166 territory
codes and a list of historic and the current
currency for those territories.
## Example
iex> Cldr.Currency.territory_currencies |> Map.get(:LT)
%{
EUR: %{from: ~D[2015-01-01], to: nil},
LTL: %{from: nil, to: ~D[2014-12-31]},
LTT: %{from: nil, to: ~D[1993-06-25]},
SUR: %{from: nil, to: ~D[1992-10-01]}
}
"""
@territory_currencies Cldr.Config.territory_currency_data()
def territory_currencies do
@territory_currencies
end
def territory_currencies(territory) do
with {:ok, territory} <- Cldr.validate_territory(territory),
{:ok, currencies} <- Map.fetch(territory_currencies(), territory) do
{:ok, currencies}
else
:error -> {:error, {Cldr.UnknownCurrencyError,
"No currencies for #{inspect territory} were found"}}
other -> other
end
end
def territory_currencies!(territory) do
case territory_currencies(territory) do
{:ok, currencies} -> currencies
{:error, {exception, reason}} -> raise exception, reason
end
end
@doc """
Returns a list of historic and the current
currency for a given locale.
## 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
## Example
iex> Cldr.Currency.currency_history_for_locale "en", MyApp.Cldr
{:ok,
%{
USD: %{from: ~D[1792-01-01], to: nil},
USN: %{tender: false},
USS: %{from: nil, tender: false, to: ~D[2014-03-01]}
}
}
"""
@spec currency_history_for_locale(LanguageTag.t) ::
{:ok, map()} | {:error, {atom, binary}}
def currency_history_for_locale(%LanguageTag{} = locale) do
locale
|> Cldr.Locale.territory_from_locale()
|> territory_currencies()
end
@spec currency_history_for_locale(Locale.locale_name, Cldr.backend) ::
{:ok, map()} | {:error, {module(), String.t}}
def currency_history_for_locale(locale_name, backend) when is_binary(locale_name) do
with {:ok, locale} <- Cldr.validate_locale(locale_name, backend) do
currency_history_for_locale(locale)
end
end
@doc """
Returns the current currency from a given locale.
This function does not consider the `U` extenion
parameters `cu` or `rg`. It is recommended to us
`Cldr.Currency.currency_from_locale/1` in most
circumstances.
## 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
## Examples
iex> Cldr.Currency.current_currency_from_locale "en", MyApp.Cldr
:USD
iex> Cldr.Currency.current_currency_from_locale "en-AU", MyApp.Cldr
:AUD
"""
@spec current_currency_from_locale(LanguageTag.t()) :: any()
def current_currency_from_locale(%LanguageTag{} = locale) do
locale
|> Cldr.Locale.territory_from_locale()
|> current_currency_for_territory()
end
@spec current_currency_from_locale(Cldr.Locale.locale_name(), Cldr.backend()) ::
code() | nil | {:error, {module(), String.t()}}
def current_currency_from_locale(locale_name, backend) when is_binary(locale_name) do
with {:ok, locale} <- Cldr.validate_locale(locale_name, backend) do
current_currency_from_locale(locale)
end
end
@doc """
Returns the current currency for a given territory.
## Arguments
* `territory` is any valid territory name returned by
`Cldr.known_territories/0`
## Examples
iex> Cldr.Currency.current_currency_for_territory :US
:USD
iex> Cldr.Currency.current_currency_for_territory :AU
:AUD
"""
@spec current_currency_for_territory(Cldr.territory()) ::
code() | nil | {:error, {module(), String.t()}}
def current_currency_for_territory(territory) do
with {:ok, territory} <- Cldr.validate_territory(territory),
{:ok, history} <- territory_currencies(territory) do
history
|> Enum.find(fn {_currency, dates} -> Map.has_key?(dates, :to) && is_nil(dates.to) end)
|> elem(0)
end
end
@doc """
Returns the currency metadata for the requested currency code.
## Arguments
* `currency_code` is a `binary` or `atom` representation of an
ISO 4217 currency code.
* `backend` is any module that includes `use Cldr` and therefore
is a `Cldr` backend module
* `options` is a `Keyword` list of options.
## Options
* `:locale` is any valid locale name returned by `Cldr.known_locale_names/1`
or a `Cldr.LanguageTag` struct returned by `Cldr.Locale.new!/2`
## Examples
iex> Cldr.Currency.currency_for_code("AUD", MyApp.Cldr)
{:ok,
%Cldr.Currency{
cash_digits: 2,
cash_rounding: 0,
code: "AUD",
count: %{one: "Australian dollar", other: "Australian dollars"},
digits: 2,
iso_digits: 2,
name: "Australian Dollar",
narrow_symbol: "$",
rounding: 0,
symbol: "A$",
tender: true
}}
iex> Cldr.Currency.currency_for_code("THB", MyApp.Cldr)
{:ok,
%Cldr.Currency{
cash_digits: 2,
cash_rounding: 0,
code: "THB",
count: %{one: "Thai baht", other: "Thai baht"},
digits: 2,
iso_digits: 2,
name: "Thai Baht",
narrow_symbol: "฿",
rounding: 0,
symbol: "THB",
tender: true
}}
"""
@spec currency_for_code(code, Cldr.backend(), Keyword.t()) ::
{:ok, t} | {:error, {module(), String.t()}}
def currency_for_code(currency_code, backend, options \\ []) do
{locale, backend} = Cldr.locale_and_backend_from(options[:locale], backend)
with {:ok, code} <- known_currency_code(currency_code),
{:ok, locale} <- Cldr.validate_locale(locale, backend),
{:ok, currencies} <- currencies_for_locale(locale, backend) do
{:ok, Map.get_lazy(currencies, code, fn -> Map.get(private_currencies(), code) end)}
end
end
@doc """
Returns a map of the metadata for all currencies for
a given locale.
## 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
* `currency_status` is `:all`, `:current`, `:historic`,
`unannotated` or `:tender`; or a list of one or more status.
The default is `:all`. See `Cldr.Currency.currency_filter/2`.
## Returns
* `{:ok, currency_map}` or
* `{:error, {exception, reason}}`
## Example
=> Cldr.Currency.currencies_for_locale "en", MyApp.Cldr
{:ok,
%{
FJD: %Cldr.Currency{
cash_digits: 2,
cash_rounding: 0,
code: "FJD",
count: %{one: "Fijian dollar", other: "Fijian dollars"},
digits: 2,
from: nil,
iso_digits: 2,
name: "Fijian Dollar",
narrow_symbol: "$",
rounding: 0,
symbol: "FJD",
tender: true,
to: nil
},
SUR: %Cldr.Currency{
cash_digits: 2,
cash_rounding: 0,
code: "SUR",
count: %{one: "Soviet rouble", other: "Soviet roubles"},
digits: 2,
from: nil,
iso_digits: nil,
name: "Soviet Rouble",
narrow_symbol: nil,
rounding: 0,
symbol: "SUR",
tender: true,
to: nil
},
...
}}
"""
@spec currencies_for_locale(Locale.locale_name() | LanguageTag.t(), Cldr.backend(),
only :: filter(), except :: filter()) ::
{:ok, map()} | {:error, {module(), String.t()}}
def currencies_for_locale(locale, backend, only \\ :all, except \\ nil) do
Module.concat(backend, Currency).currencies_for_locale(locale, only, except)
end
@doc """
Returns a map of the metadata for all currencies for
a given locale and raises on error.
## 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
* `currency_status` is `:all`, `:current`, `:historic`,
`unannotated` or `:tender`; or a list of one or more status.
The default is `:all`. See `Cldr.Currency.currency_filter/2`.
## Returns
* `{:ok, currency_map}` or
* raises an exception
## Example
#=> MyApp.Cldr.Currency.currencies_for_locale! "en"
%{
FJD: %Cldr.Currency{
cash_digits: 2,
cash_rounding: 0,
code: "FJD",
count: %{one: "Fijian dollar", other: "Fijian dollars"},
digits: 2,
from: nil,
iso_digits: 2,
name: "Fijian Dollar",
narrow_symbol: "$",
rounding: 0,
symbol: "FJD",
tender: true,
to: nil
},
SUR: %Cldr.Currency{
cash_digits: 2,
cash_rounding: 0,
code: "SUR",
count: %{one: "Soviet rouble", other: "Soviet roubles"},
digits: 2,
from: nil,
iso_digits: nil,
name: "Soviet Rouble",
narrow_symbol: nil,
rounding: 0,
symbol: "SUR",
tender: true,
to: nil
},
...
}
"""
@spec currencies_for_locale!(Locale.locale_name() | LanguageTag.t(),
Cldr.backend(),
only :: filter(),
except :: filter()) ::
map() | no_return()
def currencies_for_locale!(locale, backend, only \\ :all, except \\ nil) do
Module.concat(backend, Currency).currencies_for_locale!(locale, only, except)
end
@doc """
Returns a map that matches a currency string to a
currency code.
A currency string is a localised name or symbol
representing a currency in a locale-specific manner.
## 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`.
* `:only` is `:all`, `:current`, `:historic`,
`unannotated` or `:tender`; or a list of one or more status
The default is `:all`. See `Cldr.Currency.currency_filter/2`.
## Returns
* `{:ok, currency_string_map}` or
* `{:error, {exception, reason}}`
## Example
=> Cldr.Currency.currency_strings "en", MyApp.Cldr
{:ok,
%{
"mexican silver pesos" => :MXP,
"sudanese dinar" => :SDD,
"bad" => :BAD,
"rsd" => :RSD,
"swazi lilangeni" => :SZL,
"zairean new zaire" => :ZRN,
"guyanaese dollars" => :GYD,
"equatorial guinean ekwele" => :GQE,
...
}}
# Currencies match all currency status'
=> Cldr.Currency.currency_strings "en", MyApp.Cldr, [:tender, :current, :unannotated]
{:ok,
%{
"rsd" => :RSD,
"swazi lilangeni" => :SZL,
"guyanaese dollars" => :GYD,
"syrian pound" => :SYP,
"scr" => :SCR,
"bangladeshi takas" => :BDT,
"netherlands antillean guilders" => :ANG,
"pen" => :PEN,
...
}}
"""
@spec currency_strings(Cldr.LanguageTag.t() | Cldr.Locale.locale_name(),
only :: filter(), except :: filter()) ::
{:ok, map()} | {:error, {module(), String.t()}}
def currency_strings(locale, backend, only \\ :all, except \\ nil) do
Module.concat(backend, Currency).currency_strings(locale, only, except)
end
@doc """
Returns a map that matches a currency string to a
currency code or raises an exception.
A currency string is a localised name or symbol
representing a currency in a locale-specific manner.
## 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`.
* `currency_status` is `:all`, `:current`, `:historic`,
`unannotated` or `:tender`; or a list of one or more status.
The default is `:all`. See `Cldr.Currency.currency_filter/2`.
## Returns
* `{:ok, currency_string_map}` or
* raises an exception
## Example
=> Cldr.Currency.currency_strings! "en", MyApp.Cldr
%{
"mexican silver pesos" => :MXP,
"sudanese dinar" => :SDD,
"bad" => :BAD,
"rsd" => :RSD,
"swazi lilangeni" => :SZL,
"zairean new zaire" => :ZRN,
"guyanaese dollars" => :GYD,
"equatorial guinean ekwele" => :GQE,
...
}
"""
@spec currency_strings!(Cldr.LanguageTag.t() | Cldr.Locale.locale_name(),
only :: filter(), except :: filter()) ::
map() | no_return
def currency_strings!(locale, backend, only \\ :all, except \\ nil) do
case Module.concat(backend, Currency).currency_strings(locale, only, except) do
{:ok, currency_strings} -> currency_strings
{:error, {exception, reason}} -> raise exception, reason
end
end
@doc """
Returns the strings associated with a currency
in a given locale.
## Arguments
* `currency` is an ISO4217 currency code
* `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
## Returns
* A list of strings or
* `{:error, {exception, reason}}`
## Example
iex> Cldr.Currency.strings_for_currency :AUD, "en", MyApp.Cldr
["a$", "australian dollars", "aud", "australian dollar"]
iex> Cldr.Currency.strings_for_currency :AUD, "de", MyApp.Cldr
["australische dollar", "australischer dollar", "au$", "aud"]
iex> Cldr.Currency.strings_for_currency :AUD, "zh", MyApp.Cldr
["澳大利亚元", "au$", "aud"]
"""
def strings_for_currency(currency, locale, backend) do
module = Module.concat(backend, Currency)
with {:ok, currency_strings} <- module.currency_strings(locale),
{:ok, currency} <- Cldr.validate_currency(currency) do
Enum.filter(currency_strings, fn {_k, v} -> v == currency end)
|> Enum.map(fn {k, _v} -> k end)
end