localization.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 ns = {}
  29. return cb(ns, {status: '200'})
  30. // let langParams = url.split('/')
  31. // graphQL.query({
  32. // query: localeQuery,
  33. // variables: {
  34. // locale: langParams[0],
  35. // namespace: langParams[1]
  36. // }
  37. // }).then(resp => {
  38. // let ns = {}
  39. // if (_.get(resp, 'data.localization.translations', []).length > 0) {
  40. // resp.data.localization.translations.forEach(entry => {
  41. // _.set(ns, entry.key, entry.value)
  42. // })
  43. // }
  44. // return cb(ns, {status: '200'})
  45. // }).catch(err => {
  46. // console.error(err)
  47. // return cb(null, {status: '404'})
  48. // })
  49. }
  50. }
  51. ]
  52. },
  53. defaultNS: 'common',
  54. lng: siteConfig.lang,
  55. load: 'currentOnly',
  56. lowerCaseLng: true,
  57. fallbackLng: siteConfig.lang,
  58. ns: ['common', 'auth']
  59. })
  60. return new VueI18Next(i18next)
  61. }
  62. }