Skip to content

fix: add locale support for balance formatting #3319

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

Merged
merged 4 commits into from
Mar 27, 2025
Merged

Conversation

lawvs
Copy link
Member

@lawvs lawvs commented Mar 27, 2025

Description

Fix #2754

image

PR Type

  • Feature
  • Bugfix
  • Hotfix
  • Other (please describe):

Screenshots (if UI change)

Demo Video (if new feature)

Linked Issues

Additional context

Changelog

  • I have updated the changelog/next.md with my changes.

Copy link

vercel bot commented Mar 27, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
follow ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 27, 2025 2:02pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
follow-external-ssr ⬜️ Ignored (Inspect) Visit Preview Mar 27, 2025 2:02pm

@follow-reviewer-bot
Copy link

Suggested PR Title:

feat: enhance balance formatting with locale support

Change Summary:
Updated balance component to include locale-based number formatting using user's language settings. Modified utility function to better handle scientific notation with multiple locale conventions. This improves the accuracy and clarity of displayed balances across different languages.

Code Review:

Code Review

File: apps/desktop/src/renderer/src/modules/wallet/balance.tsx

  1. Issue with Intl.Locale fallback logic

    • Lines: 7-12
    • Problem: The fallback to "en-US" in the try-catch block for the Intl.Locale constructor seems to assume a specific default language. However, "en-US" might not be a valid default locale for all users or applications. If language is invalid or undefined, the component may produce unexpected behavior.
    • Recommendation: Use a more appropriate fallback mechanism. Either fetch the application's default locale dynamically or ensure that "en-US" is explicitly validated as the intended fallback. Additionally, log a warning when the fallback is triggered for better debugging and traceability.
  2. Unnecessary recomputation of formatted number

    • Lines: 25-27
    • Problem: The line toScientificNotation(format(n, { trailingZeros: true }), scientificThreshold) recomputes the numeric formatting even though the formatted value is already computed and stored in formatted. This adds unnecessary computational overhead.
    • Recommendation: Refactor this line to avoid recalculating the formatted value. You may cache it in a variable or use formattedFull directly if applicable.

File: packages/utils/src/utils.ts

  1. Unsupported locales in toScientificNotation
    • Lines: 233-234
    • Problem: The comment explains the limitations of the implementation (handling only the English locale's decimal/thousands separator). However, the function does not validate inputs against unsupported locales, which can lead to incorrect results or runtime errors if num contains separators from non-supported locales.
    • Recommendation: Add input validation and potentially throw an error or fallback behavior for unsupported formats. Alternatively, extend the method to support parsing numbers from diverse locales by factoring in locale-specific separators.

Summary

There are two issues in apps/desktop/src/renderer/src/modules/wallet/balance.tsx and one issue in packages/utils/src/utils.ts that require changes as noted above. Once these changes are addressed, the code will be more robust and performant.

@lawvs lawvs enabled auto-merge (squash) March 27, 2025 14:03
@lawvs lawvs merged commit 4467334 into dev Mar 27, 2025
12 checks passed
@lawvs lawvs deleted the fix/balance-format branch March 27, 2025 14:04
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.

[i18n]: Errors in number formatting in French and German, and suggestions for improving date formatting
1 participant