1
0

localizedStringsService.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { LanguageService } from "./languageService";
  2. import Langs from './../../i18n';
  3. export const StringTypes = Object.freeze({
  4. ERROR: 'error',
  5. MESSAGE: 'message',
  6. UI: 'ui'
  7. });
  8. export const LocalizedStrings = Object.freeze({
  9. getString: (id, type) => {
  10. let i18nObj = Langs[LanguageService.getLang()];
  11. if(!!!i18nObj) {
  12. i18nObj = Langs[LanguageService.getDefaultLang()];
  13. }
  14. if(!!!i18nObj[type]) {
  15. return "{MISSING_I18N_TYPE_IDENTIFIER}";
  16. } else if (i18nObj[type][id]) {
  17. return "{MISSING_I18N_TYPE_IDENTIFIER}";
  18. } else {
  19. return i18nObj[type][id];
  20. }
  21. },
  22. getError: (id, context = []) => {
  23. const text = LocalizedStrings.getString(id, StringTypes.ERROR);
  24. return LocalizedStrings.processString(text, context)
  25. },
  26. getMessage: (id, context = []) => {
  27. const text = LocalizedStrings.getString(id, StringTypes.MESSAGE);
  28. return LocalizedStrings.processString(text, context)
  29. },
  30. getUI: (id, context = []) => {
  31. const text = LocalizedStrings.getString(id, StringTypes.UI);
  32. return LocalizedStrings.processString(text, context)
  33. },
  34. processString: (text, context) => {
  35. for (let i = 0; i < context.length; i++) {
  36. const v = context[i];
  37. text = text.replace('\$'+i, v);
  38. }
  39. return text;
  40. }
  41. });