codeant-ai-for-open-source[bot] commented on code in PR #40931: URL: https://github.com/apache/superset/pull/40931#discussion_r3409258282
########## superset-frontend/packages/superset-ui-core/src/currency-format/currencyLocale.ts: ########## @@ -0,0 +1,40 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +const DEFAULT_CURRENCY_LOCALE = 'en-US'; + +let currencyLocale: string = DEFAULT_CURRENCY_LOCALE; + +/** + * Set the locale used to resolve the default currency symbol position. + * + * Called once at application bootstrap with the deployment locale so that + * currency formatting follows the conventions of that locale (e.g. EUR is a + * suffix in `fr-FR`/`de-DE` but a prefix in `en-US`). + */ +export function setCurrencyLocale(locale?: string): void { + if (locale) { + currencyLocale = locale; + } +} Review Comment: **Suggestion:** Locale strings coming from bootstrap can be underscore-formatted (for example `zh_TW`/`pt_BR`), but this setter stores them unchanged and `Intl.NumberFormat` expects BCP-47 tags with hyphens. That mismatch throws at runtime inside symbol resolution and silently forces the fallback path (`prefix`), so locale-derived suffix currencies are formatted on the wrong side. Normalize/canonicalize the locale before storing it (for example converting `_` to `-` and validating via `Intl.getCanonicalLocales`) so symbol placement is resolved correctly. [api mismatch] <details> <summary><b>Severity Level:</b> Major ⚠️</summary> ```mdx - ❌ Locale-specific currency placement ignored for underscore locales. - ⚠️ Charts in zh_TW/pt_BR show non-localized currency positions. - ⚠️ Any new underscore locale will inherit incorrect behavior. ``` </details> <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. Configure a region-specific language with underscore in the backend, e.g. `LANGUAGES = { "zh_TW": {"flag": "tw", "name": "Traditional Chinese"}, ... }` in `superset/config.py:461-467`, or add a similar `"pt_BR"` entry as suggested by the comment in `superset/views/base.py:11-13` (shown in the locale-normalization block). 2. Set the user's locale to that region-specific language via `/lang/zh_TW` (or `/lang/pt_BR`), which stores `session["locale"] = locale` in `superset/initialization/__init__.py:1121-1124`; `common_bootstrap_payload()` then computes `language` using `normalized = locale.replace("-", "_")` and preserves values like `zh_TW`/`pt_BR` when present in `LANGUAGES` (`superset/views/base.py:8-17`). 3. When generating the SPA bootstrap payload, `bootstrap_data["locale"] = language` is set to the underscore-formatted tag (e.g. `"zh_TW"`) in `superset/views/base.py:520-523`; on the frontend, `getBootstrapData()` reads this into `bootstrapData.common.locale` (`superset-frontend/src/utils/getBootstrapData.ts:24-33`), and `initPreamble()` assigns `const lang = bootstrapData.common.locale || 'en';` then calls `setupFormatters(..., lang)` in `superset-frontend/src/preamble.ts:15-23`. 4. `setupFormatters` receives `lang === 'zh_TW'` (or similar) and calls `setCurrencyLocale(locale)` at `superset-frontend/src/setup/setupFormatters.ts:37-45`, which currently stores the value unchanged (`currencyLocale = locale;` in `currencyLocale.ts:31-35`). Later, any chart using `CurrencyFormatter` constructs the formatter with `this.locale = config.locale || getCurrencyLocale();` (`CurrencyFormatter.ts:88-95`), and `resolveSymbolPosition()` is invoked with that locale (`CurrencyFormatter.ts:132-161`). Inside `resolveSymbolPosition`, `new Intl.NumberFormat(locale, { style: 'currency', currency: currencyCode }).formatToParts(1)` is called with `locale === 'zh_TW'` or `pt_BR` (`symbolPosition.ts:49-54`); because these are not well-formed BCP-47 tags, many JS engines throw `RangeError`, which is caught by the `catch {}` block (`symbolPosition.ts:50-63`), causing `resolveSymbolPosition` to fall through to the `'prefix'` default (`symbolPosition.ts:67`). As a result, currencies that should be suffixes under that locale (per `Intl.NumberFormat` when given the canonical tag like `zh-TW`/`pt-BR`) are instead always formatted as prefixes, e.g. a value that should render as `56.1M €` is rendered as `€ 56.1M`. This matches the suggestion's claim that underscore-formatted locales silently force the fallback path and break locale-derived symbol placement. ``` </details> [Fix in Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=352d918067c044eca5d26bf86455dbdb&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) | [Fix in VSCode Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=352d918067c044eca5d26bf86455dbdb&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** superset-frontend/packages/superset-ui-core/src/currency-format/currencyLocale.ts **Line:** 31:35 **Comment:** *Api Mismatch: Locale strings coming from bootstrap can be underscore-formatted (for example `zh_TW`/`pt_BR`), but this setter stores them unchanged and `Intl.NumberFormat` expects BCP-47 tags with hyphens. That mismatch throws at runtime inside symbol resolution and silently forces the fallback path (`prefix`), so locale-derived suffix currencies are formatted on the wrong side. Normalize/canonicalize the locale before storing it (for example converting `_` to `-` and validating via `Intl.getCanonicalLocales`) so symbol placement is resolved correctly. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40931&comment_hash=c0c81566d035a3303e2b9e3045fba6254b4665b3e6eab0b2d7af2d8fdea30eba&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40931&comment_hash=c0c81566d035a3303e2b9e3045fba6254b4665b3e6eab0b2d7af2d8fdea30eba&reaction=dislike'>👎</a> -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
