feat(i18n): add fr/en dicts + useT hook
This commit is contained in:
1 parent
a57047505d
commit
359104ce88
6 files changed
+358
No files matched your search
@@ -0,0 +1,18 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { fr } from '@/i18n/fr'
|
||||
import { en } from '@/i18n/en'
|
||||
|
||||
describe('i18n dicts', () => {
|
||||
it('fr and en share the exact same key set', () => {
|
||||
const frKeys = Object.keys(fr).sort()
|
||||
const enKeys = Object.keys(en).sort()
|
||||
expect(frKeys).toEqual(enKeys)
|
||||
})
|
||||
|
||||
it('every value is a non-empty string', () => {
|
||||
for (const k of Object.keys(fr)) {
|
||||
expect(fr[k].length).toBeGreaterThan(0)
|
||||
expect(en[k].length).toBeGreaterThan(0)
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,29 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
||||
import { renderHook, act } from '@testing-library/react'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
|
||||
beforeEach(() => {
|
||||
useLocaleStore.setState({ locale: 'fr' })
|
||||
})
|
||||
|
||||
describe('useT', () => {
|
||||
it('returns the FR value for a known key', () => {
|
||||
const { result } = renderHook(() => useT())
|
||||
expect(result.current('common.cancel')).toBe('Annuler')
|
||||
})
|
||||
|
||||
it('returns the EN value after switching locale', () => {
|
||||
const { result } = renderHook(() => useT())
|
||||
act(() => useLocaleStore.getState().setLocale('en'))
|
||||
expect(result.current('common.cancel')).toBe('Cancel')
|
||||
})
|
||||
|
||||
it('returns the key literal and warns when key is missing', () => {
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
||||
const { result } = renderHook(() => useT())
|
||||
expect(result.current('nonexistent.key')).toBe('nonexistent.key')
|
||||
expect(warn).toHaveBeenCalled()
|
||||
warn.mockRestore()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user