From 0f0914d9a040ec621f8c0ca2ad618b2c972c5853 Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Mon, 20 Mar 2017 23:41:56 +0900 Subject: [PATCH] :boom: breaking: remove messages settter, and add getLocaleMessage API --- decls/i18n.js | 2 +- src/index.js | 13 +++++++------ src/mixin.js | 6 +++--- src/util.js | 4 ++++ test/unit/{hot.test.js => message.test.js} | 12 ++++++------ 5 files changed, 21 insertions(+), 16 deletions(-) rename test/unit/{hot.test.js => message.test.js} (80%) diff --git a/decls/i18n.js b/decls/i18n.js index b22eb8ec4..f435d8e49 100644 --- a/decls/i18n.js +++ b/decls/i18n.js @@ -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, diff --git a/src/index.js b/src/index.js index b3aac1f51..2bba32401 100644 --- a/src/index.js +++ b/src/index.js @@ -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' @@ -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) } @@ -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 } diff --git a/src/mixin.js b/src/mixin.js index 3c113bf45..8076b3557 100644 --- a/src/mixin.js +++ b/src/mixin.js @@ -6,7 +6,7 @@ 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) } @@ -14,7 +14,7 @@ const $t = (vm: any): Function => { 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) } @@ -22,7 +22,7 @@ const $tc = (vm: any): Function => { 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) } diff --git a/src/util.js b/src/util.js index 5e1182a2d..59e5d961c 100644 --- a/src/util.js +++ b/src/util.js @@ -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)) +} diff --git a/test/unit/hot.test.js b/test/unit/message.test.js similarity index 80% rename from test/unit/hot.test.js rename to test/unit/message.test.js index 2bde514af..52dcf4cef 100644 --- a/test/unit/hot.test.js +++ b/test/unit/message.test.js @@ -1,6 +1,6 @@ import messages from './fixture/index' -describe('hot reloading', () => { +describe('message', () => { let el let i18n let orgEnLocale @@ -13,12 +13,12 @@ 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) @@ -26,8 +26,8 @@ describe('hot reloading', () => { 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 => { @@ -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