Skip to content

Add fallback option to Currency.currency_for_code/2#24

Merged
kipcole9 merged 1 commit into
elixir-localize:mainfrom
neilberkman:currency-fallback-option
May 11, 2026
Merged

Add fallback option to Currency.currency_for_code/2#24
kipcole9 merged 1 commit into
elixir-localize:mainfrom
neilberkman:currency-fallback-option

Conversation

@neilberkman
Copy link
Copy Markdown
Contributor

Summary

Mirrors the fix you shipped in ex_cldr_currencies v2.17.2 (elixir-cldr/cldr_currencies#19) for the equivalent issue in Localize. On main, Localize.Currency.currency_for_code(:VED, locale: :es) returns {:error, %Localize.UnknownCurrencyError{}} — the same error you get for :ZZZ — even though VED is a known tender currency that simply lacks Spanish-locale display data in CLDR.

Changes

  • New Localize.CurrencyNotLocalizedError distinguishes "valid currency, no data in this locale" from "unknown currency code".
  • currency_for_code/2 accepts fallback: true (default false). When set, walks Localize.Locale.parent/1 and then Localize.default_locale/0 looking for the currency before failing.
  • Real provider/load errors from currencies_for_locale/1 propagate through the fallback chain rather than being masked as CurrencyNotLocalizedError.
  • Tests for the new error type, fallback happy path, and that unknown codes still error under fallback: true.

Behaviour change

Callers that pattern-matched {:error, %Localize.UnknownCurrencyError{}} to detect the "missing in locale" case will now see %Localize.CurrencyNotLocalizedError{} instead. The original UnknownCurrencyError is unchanged for genuinely unknown currency codes.

Notes

  • mix gettext.extract picked up 4 unrelated pre-existing messages that are declared in Localize.Gettext.Messages but were never extracted to priv/gettext/localize.pot (interval-format-fallback, MF2 parse with line/column, RBNF rule, number-system-not-valid). Those are in the .pot diff but aren't from this PR.
  • I left CHANGELOG and version bump alone — happy to add an entry in whatever section/format you prefer if you'd like me to.

Tested locally with mix test (23758 tests, 0 failures).

Mirrors the ex_cldr_currencies v2.17.2 fix (elixir-cldr/cldr_currencies#19)
for Localize. Previously, requesting a known tender currency that lacks
locale-specific CLDR data (e.g. VED in :es, UYW in :de) returned the
same UnknownCurrencyError as a genuinely bogus code like :ZZZ.

* New Localize.CurrencyNotLocalizedError distinguishes "valid currency,
  no data in this locale" from "unknown currency code".
* currency_for_code/2 accepts fallback: true (default false). When set,
  walks Localize.Locale.parent/1 then Localize.default_locale/0 looking
  for the currency before failing.
* Real provider/load errors from currencies_for_locale/1 propagate
  through the fallback chain rather than being masked.
@kipcole9 kipcole9 merged commit 5869307 into elixir-localize:main May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants