UNPKG

4.68 kBJavaScriptView Raw
1/* @flow */
2
3import VueI18n from './index'
4import { isPlainObject, warn, error, merge } from './util'
5
6export default {
7 beforeCreate (): void {
8 const options: any = this.$options
9 options.i18n = options.i18n || (options.__i18n ? {} : null)
10
11 if (options.i18n) {
12 if (options.i18n instanceof VueI18n) {
13 // init locale messages via custom blocks
14 if (options.__i18n) {
15 try {
16 let localeMessages = {}
17 options.__i18n.forEach(resource => {
18 localeMessages = merge(localeMessages, JSON.parse(resource))
19 })
20 Object.keys(localeMessages).forEach((locale: Locale) => {
21 options.i18n.mergeLocaleMessage(locale, localeMessages[locale])
22 })
23 } catch (e) {
24 if (process.env.NODE_ENV !== 'production') {
25 error(`Cannot parse locale messages via custom blocks.`, e)
26 }
27 }
28 }
29 this._i18n = options.i18n
30 this._i18nWatcher = this._i18n.watchI18nData()
31 } else if (isPlainObject(options.i18n)) {
32 // component local i18n
33 if (this.$root && this.$root.$i18n && this.$root.$i18n instanceof VueI18n) {
34 options.i18n.root = this.$root
35 options.i18n.formatter = this.$root.$i18n.formatter
36 options.i18n.fallbackLocale = this.$root.$i18n.fallbackLocale
37 options.i18n.formatFallbackMessages = this.$root.$i18n.formatFallbackMessages
38 options.i18n.silentTranslationWarn = this.$root.$i18n.silentTranslationWarn
39 options.i18n.silentFallbackWarn = this.$root.$i18n.silentFallbackWarn
40 options.i18n.pluralizationRules = this.$root.$i18n.pluralizationRules
41 options.i18n.preserveDirectiveContent = this.$root.$i18n.preserveDirectiveContent
42 }
43
44 // init locale messages via custom blocks
45 if (options.__i18n) {
46 try {
47 let localeMessages = {}
48 options.__i18n.forEach(resource => {
49 localeMessages = merge(localeMessages, JSON.parse(resource))
50 })
51 options.i18n.messages = localeMessages
52 } catch (e) {
53 if (process.env.NODE_ENV !== 'production') {
54 warn(`Cannot parse locale messages via custom blocks.`, e)
55 }
56 }
57 }
58
59 const { sharedMessages } = options.i18n
60 if (sharedMessages && isPlainObject(sharedMessages)) {
61 options.i18n.messages = merge(options.i18n.messages, sharedMessages)
62 }
63
64 this._i18n = new VueI18n(options.i18n)
65 this._i18nWatcher = this._i18n.watchI18nData()
66
67 if (options.i18n.sync === undefined || !!options.i18n.sync) {
68 this._localeWatcher = this.$i18n.watchLocale()
69 }
70 } else {
71 if (process.env.NODE_ENV !== 'production') {
72 warn(`Cannot be interpreted 'i18n' option.`)
73 }
74 }
75 } else if (this.$root && this.$root.$i18n && this.$root.$i18n instanceof VueI18n) {
76 // root i18n
77 this._i18n = this.$root.$i18n
78 } else if (options.parent && options.parent.$i18n && options.parent.$i18n instanceof VueI18n) {
79 // parent i18n
80 this._i18n = options.parent.$i18n
81 }
82 },
83
84 beforeMount (): void {
85 const options: any = this.$options
86 options.i18n = options.i18n || (options.__i18n ? {} : null)
87
88 if (options.i18n) {
89 if (options.i18n instanceof VueI18n) {
90 // init locale messages via custom blocks
91 this._i18n.subscribeDataChanging(this)
92 this._subscribing = true
93 } else if (isPlainObject(options.i18n)) {
94 this._i18n.subscribeDataChanging(this)
95 this._subscribing = true
96 } else {
97 if (process.env.NODE_ENV !== 'production') {
98 warn(`Cannot be interpreted 'i18n' option.`)
99 }
100 }
101 } else if (this.$root && this.$root.$i18n && this.$root.$i18n instanceof VueI18n) {
102 this._i18n.subscribeDataChanging(this)
103 this._subscribing = true
104 } else if (options.parent && options.parent.$i18n && options.parent.$i18n instanceof VueI18n) {
105 this._i18n.subscribeDataChanging(this)
106 this._subscribing = true
107 }
108 },
109
110 beforeDestroy (): void {
111 if (!this._i18n) { return }
112
113 const self = this
114 this.$nextTick(() => {
115 if (self._subscribing) {
116 self._i18n.unsubscribeDataChanging(self)
117 delete self._subscribing
118 }
119
120 if (self._i18nWatcher) {
121 self._i18nWatcher()
122 self._i18n.destroyVM()
123 delete self._i18nWatcher
124 }
125
126 if (self._localeWatcher) {
127 self._localeWatcher()
128 delete self._localeWatcher
129 }
130
131 self._i18n = null
132 })
133 }
134}