Skip to content

Commit

Permalink
[xprototype#4/fix] Fix problem with magic reloader of i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
wilcorrea committed Mar 28, 2019
1 parent 51acf72 commit b6d2c6b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 58 deletions.
32 changes: 32 additions & 0 deletions src/app/Prototype/Base.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-underscore-dangle */
import components from 'src/config/app/components'
import { apply } from 'src/app/Util'

/**
* @typedef {Base}
Expand Down Expand Up @@ -74,6 +75,11 @@ export default class Base {
*/
static mixins = []

/**
* @type {boolean}
*/
i18n = true

/**
* @param {Object} options
* @returns {this}
Expand All @@ -91,7 +97,10 @@ export default class Base {
this.__actions = {}
this.__sections = {}

this.__loaded = {}

this.init()
this.locale()

if (this.defaults && typeof this.defaults === 'function') {
this.defaults()
Expand All @@ -111,6 +120,29 @@ export default class Base {
this.constructor.mixins.forEach(mixin => this.mixin(mixin))
}

/**
*/
locale () {
if (!this.i18n) {
return
}
this.namespace = this.constructor.domain.replace(/\//, '.')
const map = (piece) => piece.charAt(0).toUpperCase() + piece.substring(1)
const domain = this.namespace.split('.').map(map).join('/')

const locale = window.app.i18n.locale
const path = `src/domains/${domain}/${locale}`
if (this.__loaded[path]) {
return
}
this.__loaded[path] = true

const messages = require(`src/domains/${domain}/${locale}`)
const translations = apply({}, `domains.${this.namespace}`, messages.default)

window.app.i18n.mergeLocaleMessage(locale, translations)
}

/**
* @param {Object} mixin
*/
Expand Down
79 changes: 23 additions & 56 deletions src/app/Prototype/Prototype.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable no-underscore-dangle */
import Skeleton from './Skeleton'
import { lang } from 'src/app/Util/Lang'
import { apply } from 'src/app/Util'

import Field from 'src/app/Prototype/Prototype/Field'
import FieldForm from 'src/app/Prototype/Prototype/FieldForm'
Expand All @@ -18,28 +17,6 @@ export default class Prototype extends Skeleton {
*/
static mixins = [Field, FieldForm, FieldIs, FieldTable, Action]

/**
* @type {boolean}
*/
i18n = true

/**
* @param {Function} callback
*/
locale (callback = undefined) {
this.namespace = this.domain.replace(/\//, '.')
const map = (piece) => piece.charAt(0).toUpperCase() + piece.substring(1)
const domain = this.namespace.split('.').map(map).join('/')
const locale = this.$i18n.locale

import(/* webpackChunkName: "lang-[request]" */ `src/domains/${domain}/${locale}`)
.then((messages) => {
const translations = apply({}, `domains.${this.namespace}`, messages.default)
this.$i18n.mergeLocaleMessage(locale, translations)
})
.finally(callback)
}

/**
* @param {String|Array} key
* @param {string} [fallback]
Expand Down Expand Up @@ -167,46 +144,36 @@ export default class Prototype extends Skeleton {
const prototype = this

this.hook('created:default', function () {
/**
*/
const run = () => {
// Call component setup method
if (this.setup && typeof this.setup === 'function') {
this.setup()
}

// Call global prototype configure
prototype.configure.call(this)
// Call component setup method
if (this.setup && typeof this.setup === 'function') {
this.setup()
}

// Call configure of each field
this.configure()
// Call global prototype configure
prototype.configure.call(this)

if (this.scope === 'index') {
// Call configure to index scope
return prototype.configureIndex.call(this)
}
// Call configure of each field
this.configure()

if (this.scope === 'update') {
// Call configure to update scope
return prototype.configureEdit.call(this)
}
if (this.scope === 'index') {
// Call configure to index scope
return prototype.configureIndex.call(this)
}

if (this.scope === 'read') {
// Call configure to read scope
return prototype.configureView.call(this)
}
if (this.scope === 'update') {
// Call configure to update scope
return prototype.configureEdit.call(this)
}

if (this.scope === 'create') {
// Call configure to create scope
return prototype.configureAdd.call(this)
}
if (this.scope === 'read') {
// Call configure to read scope
return prototype.configureView.call(this)
}
// load i18n async
if (prototype.i18n) {
prototype.locale.call(this, run)
return

if (this.scope === 'create') {
// Call configure to create scope
return prototype.configureAdd.call(this)
}
run()
})

this.action('add')
Expand Down
2 changes: 1 addition & 1 deletion src/domains/Example/Test/Prototype/TestWithHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default class TestWithHooks extends Test {
.actionIcon('send')
.actionColor('yellow')
.actionOrder(2)
.actionLabel(this.$lang(`domains.${TestWithHooks.domain}.actions.goToTest`))
.actionLabel(this.$lang(`domains.example.test.actions.goToTest`))
.actionOn('click', function ({ $event, context }) {
this.$log('~> $event', $event)
this.$log('~> context', context)
Expand Down
2 changes: 1 addition & 1 deletion src/i18n/en-us/prototype/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default {
success: 'Record created successfully'
},
update: {
success: 'Record update successfully'
success: 'Record updated successfully'
}
},
action: {
Expand Down

0 comments on commit b6d2c6b

Please sign in to comment.