diff --git a/decls/i18n.js b/decls/i18n.js index ed1e3a093..e6d8dcdd6 100644 --- a/decls/i18n.js +++ b/decls/i18n.js @@ -85,6 +85,7 @@ declare interface I18n { set fallbackLocale (locale: Locale): void, get messages (): LocaleMessages, get dateTimeFormats (): DateTimeFormats, + get availableLocales (): Locale[], get missing (): ?MissingHandler, set missing (handler: MissingHandler): void, get formatter (): Formatter, diff --git a/src/index.js b/src/index.js index 1bef669df..b0aaa77b7 100644 --- a/src/index.js +++ b/src/index.js @@ -169,6 +169,7 @@ export default class VueI18n { get messages (): LocaleMessages { return looseClone(this._getMessages()) } get dateTimeFormats (): DateTimeFormats { return looseClone(this._getDateTimeFormats()) } get numberFormats (): NumberFormats { return looseClone(this._getNumberFormats()) } + get availableLocales (): Locale[] { return Object.keys(this.messages).sort() } get locale (): Locale { return this._vm.locale } set locale (locale: Locale): void { diff --git a/test/unit/basic.test.js b/test/unit/basic.test.js index db1f91da8..9fc548bc9 100644 --- a/test/unit/basic.test.js +++ b/test/unit/basic.test.js @@ -629,6 +629,12 @@ describe('basic', () => { }) }) + describe('i18n#availableLocales', () => { + it('should return locales defined in messages in lexical order', () => { + assert.deepStrictEqual(i18n.availableLocales, ['en', 'ja']) + }) + }) + let desc = VueI18n.availabilities.dateTimeFormat ? describe : describe.skip desc('i18n#d', () => { let dt diff --git a/types/index.d.ts b/types/index.d.ts index a07abc15c..fee46e76c 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -141,6 +141,7 @@ declare class VueI18n { readonly messages: VueI18n.LocaleMessages; readonly dateTimeFormats: VueI18n.DateTimeFormats; readonly numberFormats: VueI18n.NumberFormats; + readonly availableLocales: VueI18n.Locale[]; locale: VueI18n.Locale; fallbackLocale: VueI18n.Locale; diff --git a/vuepress/api/README.md b/vuepress/api/README.md index fc8820987..596e6ad52 100644 --- a/vuepress/api/README.md +++ b/vuepress/api/README.md @@ -209,6 +209,18 @@ The number formats of localization. * **See also:** `NumberFormats` type of [flowtype definitions](https://github.com/kazupon/vue-i18n/blob/dev/decls/i18n.js) +#### availableLocales + +> :new: 8.9.0+ + + * **Type:** `Locale[]` + + * **Default:** `[]` + + * **Examples:** `["en", "ja"]` + +The list of available locales in `messages` in lexical order. + #### formatter * **Type:** `Formatter`