From 8b71eda12a267bb2d52964a0323ddb941172b5d2 Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Thu, 16 Mar 2017 15:17:57 +0900 Subject: [PATCH] :star: new: add 'setLocaleMessage' API --- decls/i18n.js | 1 + src/index.js | 4 ++++ test/unit/hot.test.js | 25 +++++++++++++++++++------ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/decls/i18n.js b/decls/i18n.js index 6230b98fc..b22eb8ec4 100644 --- a/decls/i18n.js +++ b/decls/i18n.js @@ -33,6 +33,7 @@ declare interface I18n { set missing (handler: MissingHandler): void, get formatter (): Formatter, set formatter (formatter: Formatter): void, + setLocaleMessage (locale: Locale, message: LocaleMessage): void, t (key: Path, ...args: any): TranslateResult, tc (key: Path, choice?: number, ...args: any): TranslateResult, te (key: Path, ...args: any): boolean, diff --git a/src/index.js b/src/index.js index 6ce345cf2..051ff46f0 100644 --- a/src/index.js +++ b/src/index.js @@ -216,6 +216,10 @@ export default class VueI18n { te (key: Path, ...args: any): boolean { return this._te(key, this.locale, this.messages, ...args) } + + setLocaleMessage (locale: Locale, message: LocaleMessage): void { + this._vm.messages[locale] = message + } } VueI18n.install = install diff --git a/test/unit/hot.test.js b/test/unit/hot.test.js index aa1e4a9dc..2bde514af 100644 --- a/test/unit/hot.test.js +++ b/test/unit/hot.test.js @@ -3,11 +3,18 @@ import messages from './fixture/index' describe('hot reloading', () => { let el let i18n - let orgLocale - const expectLocale = 'the world updated' + let orgEnLocale + let orgJaLocaleMessage + const expectEnLocale = 'the world updated' + const expectJaLocaleMessage = { + message: { + hello: 'ザ・世界 -> メイド・イン・ヘブン' + } + } beforeEach(() => { - orgLocale = messages.en.message.hello + orgEnLocale = messages.en.message.hello + orgJaLocaleMessage = messages.ja i18n = new VueI18n({ locale: 'en', messages @@ -18,7 +25,8 @@ describe('hot reloading', () => { }) afterEach(() => { - messages.en.message.hello = orgLocale + messages.en.message.hello = orgEnLocale + messages.ja = orgJaLocaleMessage i18n.messages = messages }) @@ -34,10 +42,15 @@ describe('hot reloading', () => { waitForUpdate(() => { assert.equal(text.textContent, messages.en.message.hello) // hot reload (set reactivity messages) - messages.en.message.hello = expectLocale + messages.en.message.hello = expectEnLocale i18n.messages = messages }).then(() => { - assert.equal(text.textContent, expectLocale) + assert.equal(text.textContent, expectEnLocale) + // upade locale + i18n.setLocaleMessage('ja', expectJaLocaleMessage) + i18n.locale = 'ja' + }).then(() => { + assert.equal(text.textContent, expectJaLocaleMessage.message.hello) }).then(done) }) })