mixin.js 4.7 KB

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