Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
30 changes: 21 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,28 @@ 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', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is necessary to test with watch / watchEffect to see if the reaction is taking place.

const i18n = createVueI18n({
messages: {
en: {},
ja: {},
fr: {}
}
})
i18n.setLocaleMessage('ru', { hello: 'Привет мир!' })
expect(i18n.availableLocales).toEqual(['en', 'ja', 'fr', 'ru'].sort())
})
expect(i18n.availableLocales).toEqual(['en', 'ja', 'ru', 'fr'].sort())
})

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