logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://anongit.hacktivis.me/git/pleroma-fe.git/

messages.js (2057B)


  1. // When contributing, please sort JSON before committing so it would be easier to see what's missing and what's being added compared to English and other languages. It's not obligatory, but just an advice.
  2. // To sort json use jq https://stedolan.github.io/jq and invoke it like `jq -S . xx.json > xx.sorted.json`, AFAIK, there's no inplace edit option like in sed
  3. // Also, when adding a new language to "messages" variable, please do it alphabetically by language code so that users can search or check their custom language easily.
  4. // For anyone contributing to old huge messages.js and in need to quickly convert it to JSON
  5. // sed command for converting currently formatted JS to JSON:
  6. // sed -i -e "s/'//gm" -e 's/"/\\"/gm' -re 's/^( +)(.+?): ((.+?))?(,?)(\{?)$/\1"\2": "\4"/gm' -e 's/\"\{\"/{/g' -e 's/,"$/",/g' file.json
  7. // There's only problem that apostrophe character ' gets replaced by \\ so you have to fix it manually, sorry.
  8. import { isEqual } from 'lodash'
  9. import { languages, langCodeToJsonName } from './languages.js'
  10. const ULTIMATE_FALLBACK_LOCALE = 'en'
  11. const hasLanguageFile = (code) => languages.includes(code)
  12. const loadLanguageFile = (code) => {
  13. return import(
  14. /* webpackInclude: /\.json$/ */
  15. /* webpackChunkName: "i18n/[request]" */
  16. `./${langCodeToJsonName(code)}.json`
  17. )
  18. }
  19. const messages = {
  20. languages,
  21. default: {
  22. en: require('./en.json').default
  23. },
  24. setLanguage: async (i18n, language) => {
  25. const languages = (Array.isArray(language) ? language : [language]).filter(k => k)
  26. if (!languages.includes(ULTIMATE_FALLBACK_LOCALE)) {
  27. languages.push(ULTIMATE_FALLBACK_LOCALE)
  28. }
  29. const [first, ...rest] = languages
  30. if (first === i18n.locale && isEqual(rest, i18n.fallbackLocale)) {
  31. return
  32. }
  33. for (const lang of languages) {
  34. if (hasLanguageFile(lang)) {
  35. const messages = await loadLanguageFile(lang)
  36. i18n.setLocaleMessage(lang, messages.default)
  37. }
  38. }
  39. i18n.fallbackLocale = rest
  40. i18n.locale = first
  41. }
  42. }
  43. export default messages