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.

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