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.

63 lines
1.8 KiB

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