pwa-studio icon indicating copy to clipboard operation
pwa-studio copied to clipboard

Fix convert locale from Magento standard to react-intl BCP 47 language code

Open ngtankhoa opened this issue 3 years ago • 5 comments

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch @magento/[email protected] for the project I'm working on.

Function toReactIntl in node_modules/@magento/venia-ui/lib/util/formatLocale.js which Convert locale from Magento standard to react-intl BCP 47 language code, don't work as expected.

/**
 * Convert locale from Magento standard to react-intl BCP 47 language code
 *
 * @param {string} string
 * @returns {string} A string (e.g. `fr-FR`).
 */
export const toReactIntl = string => {
    return string.replace('_', '-');
};

According to MDN

replace(substr, newSubstr) substr: A String that is to be replaced by newSubstr. It is treated as a literal string and is not interpreted as a regular expression. Only the first occurrence will be replaced.

Locale code like zh_Hant_HK if use toReactIntl() will result in zh-Hant_HK So I change string.replace('_', '-') into string.replaceAll('_', '-')

Here is the diff that solved my problem:

index b10550a..216c978 100644
--- a/node_modules/@magento/venia-ui/lib/util/formatLocale.js
+++ b/node_modules/@magento/venia-ui/lib/util/formatLocale.js
@@ -15,5 +15,5 @@ export const fromReactIntl = string => {
  * @returns {string} A string (e.g. `fr-FR`).
  */
 export const toReactIntl = string => {
-    return string.replace('_', '-');
+    return string.replaceAll('_', '-');
 };

This issue body was partially generated by patch-package.

ngtankhoa avatar Sep 09 '21 08:09 ngtankhoa

Hi @ngtankhoa. Thank you for your report. To help us process this issue please make sure that you provided sufficient information.

Please, add a comment to assign the issue: @magento I am working on this


m2-assistant[bot] avatar Sep 09 '21 08:09 m2-assistant[bot]

@magento export issue to JIRA project PWA as Bug

anthoula avatar May 23 '22 14:05 anthoula

:white_check_mark: Jira issue https://jira.corp.magento.com/browse/PWA-2866 is successfully created for this GitHub issue.

github-jira-sync-bot avatar May 23 '22 14:05 github-jira-sync-bot

Feel free to open a pull request to make this contribution!

anthoula avatar Jul 18 '22 14:07 anthoula

Hi @ngtankhoa,

Please share how will it impact venia functionality also if you can please help with steps to reproduce.

Tarun50745 avatar Sep 12 '23 12:09 Tarun50745