i18n-engine.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. var LocalizedStrings = ivprogCore.LocalizedStrings;
  2. var StringTypes = ivprogCore.StringTypes;
  3. function i18n(identifier) {
  4. var opts = identifier.split(':');
  5. var type = opts[0].toLowerCase();
  6. var id = opts[1];
  7. if (StringTypes.ERROR === type) {
  8. return LocalizedStrings.getError(id);
  9. } else if (StringTypes.MESSAGE === type) {
  10. return LocalizedStrings.getMessage(id);
  11. } else if (StringTypes.UI === type) {
  12. return LocalizedStrings.getUI(id);
  13. } else {
  14. console.warn(" A string has been passed to the i18n helper function that was not in the form type:id -> " + identifier);
  15. return LocalizedStrings.getString(id, type);
  16. }
  17. }
  18. i18n.set = function(locale, identifier, translate) {
  19. if (!i18n.db[locale]) {
  20. i18n.db[locale] = {};
  21. }
  22. i18n.db[locale][identifier] = translate;
  23. }
  24. i18n.updateLocale = function(new_locale) {
  25. localStorage.setItem('ivprog.lang', new_locale);
  26. $( "data.i18n" ).each(function() {
  27. $( this ).text(i18n($( this ).val()));
  28. });
  29. }
  30. $( document ).ready(function() {
  31. $( "data.i18n" ).each(function() {
  32. $( this ).text(i18n($( this ).val()));
  33. });
  34. });