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