localizedStringsService.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. console.warn(`Internal Error. The language set at ivprog.lang is not valid: ${LanguageService.getLang()}`);
  13. i18nObj = Langs[LanguageService.getDefaultLang()];
  14. }
  15. if(!!!i18nObj[type]) {
  16. return "{MISSING_I18N_TYPE_IDENTIFIER}";
  17. } else if (!!!i18nObj[type][id]) {
  18. return "{MISSING_I18N_IDENTIFIER}";
  19. } else {
  20. return i18nObj[type][id];
  21. }
  22. },
  23. getOR: () => LocalizedStrings.getUI('join_or'),
  24. getError: (id, context = []) => {
  25. const text = LocalizedStrings.getString(id, StringTypes.ERROR);
  26. return LocalizedStrings.processString(text, context)
  27. },
  28. getMessage: (id, context = []) => {
  29. const text = LocalizedStrings.getString(id, StringTypes.MESSAGE);
  30. return LocalizedStrings.processString(text, context)
  31. },
  32. getUI: (id, context = []) => {
  33. const text = LocalizedStrings.getString(id, StringTypes.UI);
  34. return LocalizedStrings.processString(text, context)
  35. },
  36. processString: (text, context) => {
  37. for (let i = 0; i < context.length; i++) {
  38. const v = context[i];
  39. text = text.replace('\$'+i, v);
  40. }
  41. return text;
  42. }
  43. });