Skip to content

Commit

Permalink
feat: reactive availableLocales using computed property (#2058)
Browse files Browse the repository at this point in the history
* feat: add computed property and tests

* feat(tests): add checking with watchEffect

---------

Co-authored-by: mnenie <[email protected]>
  • Loading branch information
mnenie and mnenie authored Dec 31, 2024
1 parent fe6956c commit 44169cd
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 18 deletions.
11 changes: 7 additions & 4 deletions packages/vue-i18n-core/src/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,7 @@ export interface Composer<
* @remarks
* The list of available locales in `messages` in lexical order.
*/
readonly availableLocales: Locales[]
readonly availableLocales: ComputedRef<Locales[]>
/**
* @remarks
* The locale messages of localization.
Expand Down Expand Up @@ -2130,6 +2130,11 @@ export function createComposer(options: any = {}): any {
() => _messages.value as any
)

// availableLocales
const availableLocales = computed<Locale[]>(() =>
Object.keys(_messages.value).sort()
)

// datetimeFormats
const datetimeFormats = /* #__PURE__*/ computed<DateTimeFormatsType>(
() => _datetimeFormats.value
Expand Down Expand Up @@ -2549,9 +2554,7 @@ export function createComposer(options: any = {}): any {
updateFallbackLocale(_context, _locale.value, _fallbackLocale.value)
}
},
get availableLocales(): Locale[] {
return Object.keys(_messages.value).sort()
},
availableLocales,
messages,
get modifiers(): LinkedModifiers<Message> {
return _modifiers
Expand Down
4 changes: 2 additions & 2 deletions packages/vue-i18n-core/src/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ export interface VueI18n<
* @remarks
* The list of available locales in `messages` in lexical order.
*/
readonly availableLocales: Composition['availableLocales']
readonly availableLocales: Locales[]
/**
* @remarks
* The locale messages of localization.
Expand Down Expand Up @@ -1485,7 +1485,7 @@ export function createVueI18n(options: any = {}): any {

// availableLocales
get availableLocales(): Locale[] {
return composer.availableLocales as Locale[]
return composer.availableLocales.value
},

// missing
Expand Down
14 changes: 11 additions & 3 deletions packages/vue-i18n-core/test/composer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ describe('inheritLocale', () => {
describe('availableLocales', () => {
test('not initialize messages at composer creating', () => {
const { availableLocales } = createComposer({})
expect(availableLocales).toEqual(['en-US'])
expect(availableLocales.value).toEqual(['en-US'])
})

test('initialize messages at composer creating', () => {
Expand All @@ -162,10 +162,18 @@ describe('availableLocales', () => {
fr: {}
}
})
expect(availableLocales).toEqual(['en', 'ja', 'ru', 'fr'].sort())
expect(availableLocales.value).toEqual(['en', 'ja', 'ru', 'fr'].sort())
})
test('reactive update', () => {
const { availableLocales, setLocaleMessage } = createComposer({
messages: {
en: {}
}
})
setLocaleMessage('ja', { hello: 'こんにちは、世界!' })
expect(availableLocales.value).toEqual(['en', 'ja'].sort())
})
})

describe('messages', () => {
test('default value', () => {
const { messages } = createComposer({})
Expand Down
46 changes: 37 additions & 9 deletions packages/vue-i18n-core/test/legacy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,44 @@ test('fallbackLocale', () => {
expect(i18n.fallbackLocale).toEqual('ja')
})

test('availableLocales', () => {
const i18n = createVueI18n({
messages: {
en: {},
ja: {},
ru: {},
fr: {}
}
describe('availableLocales', () => {
test('initialize messages', async () => {
const i18n = createVueI18n({
messages: {
en: {},
ja: {},
fr: {}
}
})
expect(i18n.availableLocales).toEqual(['en', 'ja', 'fr'].sort())
})
test('reactive update', () => {
const i18n = createVueI18n({
messages: {
en: {},
ja: {},
fr: {}
}
})
i18n.setLocaleMessage('ru', { hello: 'Привет мир!' })
expect(i18n.availableLocales).toEqual(['en', 'ja', 'fr', 'ru'].sort())
})
test('trigger new value with watchEffect', async () => {
const i18n = createVueI18n({
messages: {
ja: {}
}
})
let locales = [] as string[]

watchEffect(() => {
locales = i18n.availableLocales
})

i18n.setLocaleMessage('en', { hello: 'Hello' })
await nextTick()
expect(locales.sort()).toEqual(['en', 'ja'].sort())
})
expect(i18n.availableLocales).toEqual(['en', 'ja', 'ru', 'fr'].sort())
})

test('missing', () => {
Expand Down

0 comments on commit 44169cd

Please sign in to comment.