Skip to content

Commit

Permalink
⭐ new: add 'setLocaleMessage' API
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Mar 16, 2017
1 parent e41f0c9 commit 8b71eda
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions decls/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 19 additions & 6 deletions test/unit/hot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,7 +25,8 @@ describe('hot reloading', () => {
})

afterEach(() => {
messages.en.message.hello = orgLocale
messages.en.message.hello = orgEnLocale
messages.ja = orgJaLocaleMessage
i18n.messages = messages
})

Expand All @@ -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)
})
})

0 comments on commit 8b71eda

Please sign in to comment.