From 0cf896672cfdca67429cf09fabfce17f91eef5af Mon Sep 17 00:00:00 2001 From: PolariTOON <36267812+PolariTOON@users.noreply.github.com> Date: Thu, 8 Dec 2022 16:41:01 +0100 Subject: [PATCH] fix: Resolve `en` locale as `en-US` `date-fns` does not export any `en` folder anymore, but apps still rely on it via the `I18n` component. We allow them to use either `en` or `en-US` as a locale identifier. This should help consuming libs and apps to migrate. --- react/I18n/format.jsx | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/react/I18n/format.jsx b/react/I18n/format.jsx index ac67179478..49190da1d6 100644 --- a/react/I18n/format.jsx +++ b/react/I18n/format.jsx @@ -4,26 +4,31 @@ import formatDistanceToNow from 'date-fns/formatDistanceToNow' import formatDistanceToNowStrict from 'date-fns/formatDistanceToNowStrict' const locales = {} -let lang = DEFAULT_LANG +let lang = DEFAULT_LANG === 'en' ? 'en-US' : DEFAULT_LANG const getWarningMessage = lang => `The "${lang}" locale isn't supported by date-fns. or has not been included in the build. Check if you have configured a ContextReplacementPlugin that is too restrictive.` export const provideDateFnsLocale = (userLang, defaultLang = DEFAULT_LANG) => { - lang = userLang + const resolvedDefaultLang = defaultLang === 'en' ? 'en-US' : defaultLang + const resolvedUserLang = userLang === 'en' ? 'en-US' : userLang try { - locales[defaultLang] = require(`date-fns/locale/${defaultLang}/index.js`) + locales[ + resolvedDefaultLang + ] = require(`date-fns/locale/${resolvedDefaultLang}/index.js`) } catch (err) { - console.warn(getWarningMessage(defaultLang)) + console.warn(getWarningMessage(resolvedDefaultLang)) } - - if (lang && lang !== defaultLang) { + if (resolvedUserLang && resolvedUserLang !== resolvedDefaultLang) { try { - locales[lang] = require(`date-fns/locale/${lang}/index.js`) + locales[ + resolvedUserLang + ] = require(`date-fns/locale/${resolvedUserLang}/index.js`) } catch (e) { - console.warn(getWarningMessage(lang)) + console.warn(getWarningMessage(resolvedUserLang)) } } + lang = resolvedUserLang return locales[lang] }