Skip to content

Commit

Permalink
💥 breaking: remove messages settter, and add getLocaleMessage API
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Mar 20, 2017
1 parent 7536b60 commit 0f0914d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion decls/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ declare interface I18n {
get fallbackLocale (): Locale,
set fallbackLocale (locale: Locale): void,
get messages (): LocaleMessages,
set messages (messages: LocaleMessages): void,
get missing (): ?MissingHandler,
set missing (handler: MissingHandler): void,
get formatter (): Formatter,
set formatter (formatter: Formatter): void,
getLocaleMessage (locale: Locale): LocaleMessage,
setLocaleMessage (locale: Locale, message: LocaleMessage): void,
t (key: Path, ...args: any): TranslateResult,
tc (key: Path, choice?: number, ...args: any): TranslateResult,
Expand Down
13 changes: 7 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @flow */

import { install, Vue } from './install'
import { warn, isNull, parseArgs, fetchChoice, isPlainObject } from './util'
import { warn, isNull, parseArgs, fetchChoice, isPlainObject, looseClone } from './util'
import BaseFormatter from './format'
import getPathValue from './path'

Expand Down Expand Up @@ -67,12 +67,9 @@ export default class VueI18n {

get vm (): any { return this._vm }

get messages (): LocaleMessages { return this._vm.$data.messages }
set messages (messages: LocaleMessages): void {
this._vm.$set(this._vm, 'messages', messages)
}
get messages (): LocaleMessages { return looseClone(this._vm.messages) }

get locale (): Locale { return this._vm.$data.locale }
get locale (): Locale { return this._vm.locale }
set locale (locale: Locale): void {
this._vm.$set(this._vm, 'locale', locale)
}
Expand Down Expand Up @@ -221,6 +218,10 @@ export default class VueI18n {
return this._te(key, this.locale, this.messages, ...args)
}

getLocaleMessage (locale: Locale): LocaleMessage {
return looseClone(this._vm.messages[locale])
}

setLocaleMessage (locale: Locale, message: LocaleMessage): void {
this._vm.messages[locale] = message
}
Expand Down
6 changes: 3 additions & 3 deletions src/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import { isPlainObject, warn } from './util'
const $t = (vm: any): Function => {
// add dependency tracking !!
const locale: Locale = vm.$i18n.locale
const messages: LocaleMessages = vm.$i18n.messages
const messages: LocaleMessages = vm.$i18n.vm.messages
return (key: string, ...args: any): TranslateResult => {
return vm.$i18n._t(key, locale, messages, vm, ...args)
}
}
const $tc = (vm: any): Function => {
// add dependency tracking !!
const locale: Locale = vm.$i18n.locale
const messages: LocaleMessages = vm.$i18n.messages
const messages: LocaleMessages = vm.$i18n.vm.messages
return (key: string, choice?: number, ...args: any): TranslateResult => {
return vm.$i18n._tc(key, locale, messages, vm, choice, ...args)
}
}
const $te = (vm: any): Function => {
// add dependency tracking !!
const locale: Locale = vm.$i18n.locale
const messages: LocaleMessages = vm.$i18n.messages
const messages: LocaleMessages = vm.$i18n.vm.messages
return (key: string, ...args: any): boolean => {
return vm.$i18n._te(key, locale, messages, ...args)
}
Expand Down
4 changes: 4 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@ export function fetchChoice (message: string, choice: number): ?string {
if (!choices[choice]) { return message }
return choices[choice].trim()
}

export function looseClone (obj: Object): Object {
return JSON.parse(JSON.stringify(obj))
}
12 changes: 6 additions & 6 deletions test/unit/hot.test.js → test/unit/message.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import messages from './fixture/index'

describe('hot reloading', () => {
describe('message', () => {
let el
let i18n
let orgEnLocale
Expand All @@ -13,21 +13,21 @@ describe('hot reloading', () => {
}

beforeEach(() => {
orgEnLocale = messages.en.message.hello
orgJaLocaleMessage = messages.ja
i18n = new VueI18n({
locale: 'en',
messages
})
orgEnLocale = i18n.getLocaleMessage('en').message.hello
orgJaLocaleMessage = i18n.getLocaleMessage('ja')

el = document.createElement('div')
document.body.appendChild(el)
})

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

it('should be reload', done => {
Expand All @@ -43,7 +43,7 @@ describe('hot reloading', () => {
assert.equal(text.textContent, messages.en.message.hello)
// hot reload (set reactivity messages)
messages.en.message.hello = expectEnLocale
i18n.messages = messages
i18n.setLocaleMessage('en', messages.en)
}).then(() => {
assert.equal(text.textContent, expectEnLocale)
// upade locale
Expand Down

0 comments on commit 0f0914d

Please sign in to comment.