i18n.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. /**
  21. * i18n.js
  22. *
  23. * This will setup the i18n language files and locale data for your app.
  24. *
  25. */
  26. const addLocaleData = require('react-intl').addLocaleData
  27. const enLocaleData = require('react-intl/locale-data/en')
  28. const zhLocaleData = require('react-intl/locale-data/zh')
  29. const enTranslationMessages = require('./translations/en.json')
  30. const zhTranslationMessages = require('./translations/zh.json')
  31. addLocaleData(enLocaleData)
  32. addLocaleData(zhLocaleData)
  33. export const DEFAULT_LOCALE = 'en'
  34. export const appLocales = [
  35. 'en',
  36. 'zh'
  37. ]
  38. export const formatTranslationMessages = (locale, messages) => {
  39. const defaultFormattedMessages =
  40. locale !== DEFAULT_LOCALE
  41. ? formatTranslationMessages(DEFAULT_LOCALE, enTranslationMessages)
  42. : {}
  43. const flattenFormattedMessages = (formattedMessages, key) => {
  44. const formattedMessage =
  45. !messages[key] && locale !== DEFAULT_LOCALE
  46. ? defaultFormattedMessages[key]
  47. : messages[key]
  48. return {
  49. ...formattedMessages,
  50. [key]: formattedMessage
  51. }
  52. }
  53. return Object.keys(messages).reduce(flattenFormattedMessages, {})
  54. }
  55. export const translationMessages = {
  56. en: formatTranslationMessages('en', enTranslationMessages),
  57. zh: formatTranslationMessages('zh', zhTranslationMessages)
  58. }