Skip to content

Commit

Permalink
fix: Resolve en locale as en-US
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
PolariTOON committed Dec 13, 2022
1 parent 1cf960e commit 347bb5e
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions react/I18n/format.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@ import { DEFAULT_LANG } from '.'
import formatDistanceToNow from 'date-fns/formatDistanceToNow'

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]
}

Expand Down

0 comments on commit 347bb5e

Please sign in to comment.