Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Add a way to register custom (non-ISO) currencies #40

Open
dejurin opened this issue Dec 23, 2024 · 2 comments
Open

Feature Request: Add a way to register custom (non-ISO) currencies #40

dejurin opened this issue Dec 23, 2024 · 2 comments

Comments

@dejurin
Copy link

dejurin commented Dec 23, 2024

Hi! Thanks for this library. I love using bojanz/currency for ISO-defined currencies and localizing them. However, I occasionally need to work with codes that are not in the standard ISO 4217 list, such as:

  • Cryptocurrencies (BTC, ETH, etc.).
  • Custom or test codes for internal usage.

Currently, bojanz/currency has no public API to register or define additional currency codes. If I try to do NewAmount("100", "BTC"), it fails with an “unknown currency code” error (since BTC is absent in data.go).

I’d like to request a feature that allows adding custom entries at runtime, similar to how some libraries have a Register or AddCurrency function. This could look like: Then, the code NewAmount("123.45", "BTC") would succeed, and formatter.Format would also work if I supply it with, say, a default symbol or pattern.

Rationale:

  1. Many real-world applications mix official ISO currencies with special tokens or private codes.
  2. A Register approach would keep the library’s built-in data intact while letting advanced users inject their own definitions.
  3. Without it, we either have to fork data.go or maintain a separate logic for custom codes and do manual formatting.

Possible approach:

  • A public map or method in the package that merges user-provided data with the built-in CLDR sets.
  • For formatting, if a custom code is found, we use user-specified digits/symbol instead of failing.
  • Ideally, it could be a single currency.Register(...) method.

I realize that might require some discussion about how to handle locale-specific symbols, but even minimal support (like picking a fallback or “generic” symbol) would be extremely helpful.

Questions:

  1. Would you be open to a PR that introduces a Register() or AddCurrency() method?
  2. Alternatively, is there a recommended approach/pattern for working with non-ISO codes that I might have missed?

Thanks again for your great work on this library! Let me know if you need more details or if there’s another recommended solution for custom codes.

@dejurin dejurin changed the title Add a way to register custom (non-ISO) currencies at runtime Feature Request: Add a way to register custom (non-ISO) currencies at runtime Dec 23, 2024
@dejurin dejurin changed the title Feature Request: Add a way to register custom (non-ISO) currencies at runtime Feature Request: Add a way to register custom (non-ISO) currencies Dec 23, 2024
dejurin added a commit to dejurin/currency that referenced this issue Dec 23, 2024
Feature Request: Add a way to register custom (non-ISO) currencies
@bojanz
Copy link
Owner

bojanz commented Dec 24, 2024

Thanks, I'll review this in ~7 days.

@dejurin
Copy link
Author

dejurin commented Dec 24, 2024

Thank you. 🙏 I hope this works out for you.

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

No branches or pull requests

2 participants