@@ -2,7 +2,7 @@ import { describe, it, expect } from 'vitest';
22import { createMemoryCache } from './memory-cache' ;
33import { createMemoryQueue } from './memory-queue' ;
44import { createMemoryJob } from './memory-job' ;
5- import { createMemoryI18n } from './memory-i18n' ;
5+ import { createMemoryI18n , resolveLocale } from './memory-i18n' ;
66import { CORE_FALLBACK_FACTORIES } from './index' ;
77
88describe ( 'CORE_FALLBACK_FACTORIES' , ( ) => {
@@ -222,3 +222,60 @@ describe('createMemoryI18n', () => {
222222 expect ( i18n . getTranslations ( 'en' ) ) . toEqual ( { hello : 'Hello' , bye : 'Goodbye' } ) ;
223223 } ) ;
224224} ) ;
225+
226+ describe ( 'resolveLocale' , ( ) => {
227+ it ( 'should return exact match' , ( ) => {
228+ expect ( resolveLocale ( 'zh-CN' , [ 'en' , 'zh-CN' , 'ja' ] ) ) . toBe ( 'zh-CN' ) ;
229+ } ) ;
230+
231+ it ( 'should return case-insensitive match' , ( ) => {
232+ expect ( resolveLocale ( 'zh-cn' , [ 'en' , 'zh-CN' ] ) ) . toBe ( 'zh-CN' ) ;
233+ expect ( resolveLocale ( 'EN-US' , [ 'en-US' , 'zh-CN' ] ) ) . toBe ( 'en-US' ) ;
234+ } ) ;
235+
236+ it ( 'should return base language match' , ( ) => {
237+ expect ( resolveLocale ( 'zh-TW' , [ 'en' , 'zh' ] ) ) . toBe ( 'zh' ) ;
238+ } ) ;
239+
240+ it ( 'should return variant expansion (zh → zh-CN)' , ( ) => {
241+ expect ( resolveLocale ( 'zh' , [ 'en' , 'zh-CN' , 'zh-TW' ] ) ) . toBe ( 'zh-CN' ) ;
242+ } ) ;
243+
244+ it ( 'should return undefined for no match' , ( ) => {
245+ expect ( resolveLocale ( 'fr' , [ 'en' , 'zh-CN' ] ) ) . toBeUndefined ( ) ;
246+ } ) ;
247+
248+ it ( 'should return undefined for empty available locales' , ( ) => {
249+ expect ( resolveLocale ( 'en' , [ ] ) ) . toBeUndefined ( ) ;
250+ } ) ;
251+
252+ it ( 'should handle es → es-ES expansion' , ( ) => {
253+ expect ( resolveLocale ( 'es' , [ 'en' , 'es-ES' , 'fr' ] ) ) . toBe ( 'es-ES' ) ;
254+ } ) ;
255+ } ) ;
256+
257+ describe ( 'createMemoryI18n locale fallback' , ( ) => {
258+ it ( 'should resolve translations via locale fallback (zh → zh-CN)' , ( ) => {
259+ const i18n = createMemoryI18n ( ) ;
260+ i18n . loadTranslations ( 'zh-CN' , { hello : '你好' } ) ;
261+ expect ( i18n . getTranslations ( 'zh' ) ) . toEqual ( { hello : '你好' } ) ;
262+ } ) ;
263+
264+ it ( 'should resolve translations via case-insensitive fallback (zh-cn → zh-CN)' , ( ) => {
265+ const i18n = createMemoryI18n ( ) ;
266+ i18n . loadTranslations ( 'zh-CN' , { hello : '你好' } ) ;
267+ expect ( i18n . getTranslations ( 'zh-cn' ) ) . toEqual ( { hello : '你好' } ) ;
268+ } ) ;
269+
270+ it ( 'should translate via locale fallback (zh → zh-CN)' , ( ) => {
271+ const i18n = createMemoryI18n ( ) ;
272+ i18n . loadTranslations ( 'zh-CN' , { greeting : '你好世界' } ) ;
273+ expect ( i18n . t ( 'greeting' , 'zh' ) ) . toBe ( '你好世界' ) ;
274+ } ) ;
275+
276+ it ( 'should still fall back to default locale when no locale match at all' , ( ) => {
277+ const i18n = createMemoryI18n ( ) ;
278+ i18n . loadTranslations ( 'en' , { hello : 'Hello' } ) ;
279+ expect ( i18n . t ( 'hello' , 'ja' ) ) . toBe ( 'Hello' ) ;
280+ } ) ;
281+ } ) ;
0 commit comments