1 |
|
2 |
|
3 | import VueI18n from './index'
|
4 | import { isPlainObject, warn, merge } from './util'
|
5 |
|
6 | export 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 |
|
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 | warn(`Cannot parse locale messages via custom blocks.`, e)
|
26 | }
|
27 | }
|
28 | }
|
29 | this._i18n = options.i18n
|
30 | this._i18nWatcher = this._i18n.watchI18nData()
|
31 | this._i18n.subscribeDataChanging(this)
|
32 | this._subscribing = true
|
33 | } else if (isPlainObject(options.i18n)) {
|
34 |
|
35 | if (this.$root && this.$root.$i18n && this.$root.$i18n instanceof VueI18n) {
|
36 | options.i18n.root = this.$root
|
37 | options.i18n.formatter = this.$root.$i18n.formatter
|
38 | options.i18n.fallbackLocale = this.$root.$i18n.fallbackLocale
|
39 | options.i18n.silentTranslationWarn = this.$root.$i18n.silentTranslationWarn
|
40 | options.i18n.silentFallbackWarn = this.$root.$i18n.silentFallbackWarn
|
41 | options.i18n.pluralizationRules = this.$root.$i18n.pluralizationRules
|
42 | options.i18n.preserveDirectiveContent = this.$root.$i18n.preserveDirectiveContent
|
43 | }
|
44 |
|
45 |
|
46 | if (options.__i18n) {
|
47 | try {
|
48 | let localeMessages = {}
|
49 | options.__i18n.forEach(resource => {
|
50 | localeMessages = merge(localeMessages, JSON.parse(resource))
|
51 | })
|
52 | options.i18n.messages = localeMessages
|
53 | } catch (e) {
|
54 | if (process.env.NODE_ENV !== 'production') {
|
55 | warn(`Cannot parse locale messages via custom blocks.`, e)
|
56 | }
|
57 | }
|
58 | }
|
59 |
|
60 | this._i18n = new VueI18n(options.i18n)
|
61 | this._i18nWatcher = this._i18n.watchI18nData()
|
62 | this._i18n.subscribeDataChanging(this)
|
63 | this._subscribing = true
|
64 |
|
65 | if (options.i18n.sync === undefined || !!options.i18n.sync) {
|
66 | this._localeWatcher = this.$i18n.watchLocale()
|
67 | }
|
68 | } else {
|
69 | if (process.env.NODE_ENV !== 'production') {
|
70 | warn(`Cannot be interpreted 'i18n' option.`)
|
71 | }
|
72 | }
|
73 | } else if (this.$root && this.$root.$i18n && this.$root.$i18n instanceof VueI18n) {
|
74 |
|
75 | this._i18n = this.$root.$i18n
|
76 | this._i18n.subscribeDataChanging(this)
|
77 | this._subscribing = true
|
78 | } else if (options.parent && options.parent.$i18n && options.parent.$i18n instanceof VueI18n) {
|
79 |
|
80 | this._i18n = options.parent.$i18n
|
81 | this._i18n.subscribeDataChanging(this)
|
82 | this._subscribing = true
|
83 | }
|
84 | },
|
85 |
|
86 | beforeDestroy (): void {
|
87 | if (!this._i18n) { return }
|
88 |
|
89 | const self = this
|
90 | this.$nextTick(() => {
|
91 | if (self._subscribing) {
|
92 | self._i18n.unsubscribeDataChanging(self)
|
93 | delete self._subscribing
|
94 | }
|
95 |
|
96 | if (self._i18nWatcher) {
|
97 | self._i18nWatcher()
|
98 | delete self._i18nWatcher
|
99 | }
|
100 |
|
101 | if (self._localeWatcher) {
|
102 | self._localeWatcher()
|
103 | delete self._localeWatcher
|
104 | }
|
105 |
|
106 | self._i18n = null
|
107 | })
|
108 | }
|
109 | }
|