2017-09-25 03:22:33 +00:00
|
|
|
import i18next from 'i18next'
|
|
|
|
import i18nextXHR from 'i18next-xhr-backend'
|
|
|
|
import i18nextCache from 'i18next-localstorage-cache'
|
|
|
|
import VueI18Next from '@panter/vue-i18next'
|
2018-03-19 03:12:56 +00:00
|
|
|
import _ from 'lodash'
|
2017-09-25 03:22:33 +00:00
|
|
|
|
2018-03-19 03:12:56 +00:00
|
|
|
/* global siteConfig, graphQL */
|
2017-09-25 03:22:33 +00:00
|
|
|
|
2019-01-12 23:33:30 +00:00
|
|
|
import localeQuery from 'gql/common/common-localization-query-translations.gql'
|
2018-06-17 21:29:19 +00:00
|
|
|
|
2018-09-04 04:46:24 +00:00
|
|
|
export default {
|
2017-09-25 03:22:33 +00:00
|
|
|
VueI18Next,
|
|
|
|
init() {
|
|
|
|
i18next
|
|
|
|
.use(i18nextXHR)
|
|
|
|
.use(i18nextCache)
|
|
|
|
.init({
|
|
|
|
backend: {
|
|
|
|
loadPath: '{{lng}}/{{ns}}',
|
|
|
|
parse: (data) => data,
|
|
|
|
ajax: (url, opts, cb, data) => {
|
|
|
|
let langParams = url.split('/')
|
2017-10-01 03:47:14 +00:00
|
|
|
graphQL.query({
|
2018-06-17 21:29:19 +00:00
|
|
|
query: localeQuery,
|
2017-10-01 03:47:14 +00:00
|
|
|
variables: {
|
|
|
|
locale: langParams[0],
|
|
|
|
namespace: langParams[1]
|
|
|
|
}
|
2017-09-25 03:22:33 +00:00
|
|
|
}).then(resp => {
|
|
|
|
let ns = {}
|
2019-01-12 23:33:30 +00:00
|
|
|
if (_.get(resp, 'data.localization.translations', []).length > 0) {
|
|
|
|
resp.data.localization.translations.forEach(entry => {
|
2018-03-19 03:12:56 +00:00
|
|
|
_.set(ns, entry.key, entry.value)
|
2017-09-25 03:22:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return cb(ns, {status: '200'})
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
return cb(null, {status: '404'})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
cache: {
|
|
|
|
enabled: true,
|
|
|
|
expiration: 60 * 60 * 1000
|
|
|
|
},
|
|
|
|
defaultNS: 'common',
|
|
|
|
lng: siteConfig.lang,
|
|
|
|
fallbackLng: siteConfig.lang,
|
2018-01-15 03:05:08 +00:00
|
|
|
ns: ['common', 'auth']
|
2017-09-25 03:22:33 +00:00
|
|
|
})
|
|
|
|
return new VueI18Next(i18next)
|
|
|
|
}
|
|
|
|
}
|