You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.5 KiB

  1. import i18next from 'i18next'
  2. import i18nextXHR from 'i18next-xhr-backend'
  3. import i18nextCache from 'i18next-localstorage-cache'
  4. import VueI18Next from '@panter/vue-i18next'
  5. import _ from 'lodash'
  6. /* global siteConfig, graphQL */
  7. import localeQuery from 'gql/common/common-locale-query.gql'
  8. module.exports = {
  9. VueI18Next,
  10. init() {
  11. i18next
  12. .use(i18nextXHR)
  13. .use(i18nextCache)
  14. .init({
  15. backend: {
  16. loadPath: '{{lng}}/{{ns}}',
  17. parse: (data) => data,
  18. ajax: (url, opts, cb, data) => {
  19. let langParams = url.split('/')
  20. graphQL.query({
  21. query: localeQuery,
  22. variables: {
  23. locale: langParams[0],
  24. namespace: langParams[1]
  25. }
  26. }).then(resp => {
  27. let ns = {}
  28. if (resp.data.translations.length > 0) {
  29. resp.data.translations.forEach(entry => {
  30. _.set(ns, entry.key, entry.value)
  31. })
  32. }
  33. return cb(ns, {status: '200'})
  34. }).catch(err => {
  35. console.error(err)
  36. return cb(null, {status: '404'})
  37. })
  38. }
  39. },
  40. cache: {
  41. enabled: true,
  42. expiration: 60 * 60 * 1000
  43. },
  44. defaultNS: 'common',
  45. lng: siteConfig.lang,
  46. fallbackLng: siteConfig.lang,
  47. ns: ['common', 'auth']
  48. })
  49. return new VueI18Next(i18next)
  50. }
  51. }