Add fallback option to Currency.currency_for_code/2#24
Merged
kipcole9 merged 1 commit intoMay 11, 2026
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Mirrors the fix you shipped in
ex_cldr_currenciesv2.17.2 (elixir-cldr/cldr_currencies#19) for the equivalent issue in Localize. Onmain,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
Localize.CurrencyNotLocalizedErrordistinguishes "valid currency, no data in this locale" from "unknown currency code".currency_for_code/2acceptsfallback: true(defaultfalse). When set, walksLocalize.Locale.parent/1and thenLocalize.default_locale/0looking for the currency before failing.currencies_for_locale/1propagate through the fallback chain rather than being masked asCurrencyNotLocalizedError.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 originalUnknownCurrencyErroris unchanged for genuinely unknown currency codes.Notes
mix gettext.extractpicked up 4 unrelated pre-existing messages that are declared inLocalize.Gettext.Messagesbut were never extracted topriv/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.Tested locally with
mix test(23758 tests, 0 failures).