Skip to content

Commit 77606c7

Browse files
authored
feat(use-i18n): allow default date-fns locale instead of lazy loading (#547)
* feat(use-i18n): allow default date-fns locale instead of lazy loading * fix: sort * fix: correct tests
1 parent acc5dbe commit 77606c7

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

packages/use-i18n/src/__tests__/usei18n.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ describe('i18n hook', () => {
186186
jest.spyOn(window, 'navigator', 'get').mockImplementation(() => ({
187187
language: 'en-US',
188188
languages: ['en-US', 'en'],
189-
}))
189+
}) as unknown as Navigator)
190190
const { result, waitForNextUpdate } = renderHook(() => useI18n(), {
191191
wrapper: wrapper({
192192
defaultLocale: 'fr',
@@ -201,7 +201,7 @@ describe('i18n hook', () => {
201201
jest.spyOn(window, 'navigator', 'get').mockImplementation(() => ({
202202
language: 'en',
203203
languages: undefined,
204-
}))
204+
}) as unknown as Navigator)
205205
const { result, waitForNextUpdate } = renderHook(() => useI18n(), {
206206
wrapper: wrapper({
207207
defaultLocale: 'fr',
@@ -278,7 +278,7 @@ describe('i18n hook', () => {
278278
}),
279279
})
280280
await waitForNextUpdate()
281-
const identiqueTranslate = result.current.namespaceTranslation()
281+
const identiqueTranslate = result.current.namespaceTranslation('')
282282
expect(identiqueTranslate('title')).toEqual(result.current.t('title'))
283283

284284
const translate = result.current.namespaceTranslation('tests.test')

packages/use-i18n/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import usei18n from './usei18n'
1+
import I18nContextProvider from './usei18n'
22

33
export * from './usei18n'
44

5-
export default usei18n
5+
export default I18nContextProvider

packages/use-i18n/src/usei18n.tsx

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,18 @@ const I18nContextProvider = ({
120120
children,
121121
defaultLoad,
122122
loadDateLocale,
123+
defaultDateLocale,
123124
defaultLocale,
124-
defaultTranslations,
125-
enableDefaultLocale,
126-
enableDebugKey,
127-
localeItemStorage,
125+
defaultTranslations = {},
126+
enableDefaultLocale = false,
127+
enableDebugKey = false,
128+
localeItemStorage = LOCALE_ITEM_STORAGE,
128129
supportedLocales,
129130
}: {
130131
children: ReactNode,
131132
defaultLoad: LoadTranslationsFn,
132-
loadDateLocale: LoadLocaleFn,
133+
loadDateLocale?: LoadLocaleFn,
134+
defaultDateLocale?: Locale,
133135
defaultLocale: string,
134136
defaultTranslations: TranslationsByLocales,
135137
enableDefaultLocale: boolean,
@@ -142,10 +144,10 @@ const I18nContextProvider = ({
142144
)
143145
const [translations, setTranslations] = useState<TranslationsByLocales>(defaultTranslations)
144146
const [namespaces, setNamespaces] = useState<string[]>([])
145-
const [dateFnsLocale, setDateFnsLocale] = useState<Locale>()
147+
const [dateFnsLocale, setDateFnsLocale] = useState<Locale | undefined>(defaultDateLocale ?? undefined)
146148

147149
useEffect(() => {
148-
loadDateLocale(currentLocale === 'en' ? 'en-GB' : currentLocale)
150+
loadDateLocale?.(currentLocale === 'en' ? 'en-GB' : currentLocale)
149151
.then(setDateFnsLocale)
150152
.catch(() => loadDateLocale('en-GB').then(setDateFnsLocale))
151153
}, [loadDateLocale, currentLocale])
@@ -340,21 +342,15 @@ const I18nContextProvider = ({
340342
return <I18nContext.Provider value={value}>{children}</I18nContext.Provider>
341343
}
342344

343-
I18nContextProvider.defaultProps = {
344-
defaultTranslations: {},
345-
enableDebugKey: false,
346-
enableDefaultLocale: false,
347-
localeItemStorage: LOCALE_ITEM_STORAGE,
348-
}
349-
350345
I18nContextProvider.propTypes = {
351346
children: PropTypes.node.isRequired,
347+
defaultDateLocale: PropTypes.shape({}),
352348
defaultLoad: PropTypes.func.isRequired,
353349
defaultLocale: PropTypes.string.isRequired,
354350
defaultTranslations: PropTypes.shape({}),
355351
enableDebugKey: PropTypes.bool,
356352
enableDefaultLocale: PropTypes.bool,
357-
loadDateLocale: PropTypes.func.isRequired,
353+
loadDateLocale: PropTypes.func,
358354
localeItemStorage: PropTypes.string,
359355
supportedLocales: PropTypes.arrayOf(PropTypes.string).isRequired,
360356
}

0 commit comments

Comments
 (0)