Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

translator.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* global translations */
  2. /* Magic Mirror
  3. * Translator (l10n)
  4. *
  5. * By Christopher Fenner https://github.com/CFenner
  6. * MIT Licensed.
  7. */
  8. const Translator = (function () {
  9. /**
  10. * Load a JSON file via XHR.
  11. *
  12. * @param {string} file Path of the file we want to load.
  13. * @param {Function} callback Function called when done.
  14. */
  15. function loadJSON(file, callback) {
  16. const xhr = new XMLHttpRequest();
  17. xhr.overrideMimeType("application/json");
  18. xhr.open("GET", file, true);
  19. xhr.onreadystatechange = function () {
  20. if (xhr.readyState === 4 && xhr.status === 200) {
  21. // needs error handler try/catch at least
  22. let fileinfo = null;
  23. try {
  24. fileinfo = JSON.parse(xhr.responseText);
  25. } catch (exception) {
  26. // nothing here, but don't die
  27. Log.error(" loading json file =" + file + " failed");
  28. }
  29. callback(fileinfo);
  30. }
  31. };
  32. xhr.send(null);
  33. }
  34. return {
  35. coreTranslations: {},
  36. coreTranslationsFallback: {},
  37. translations: {},
  38. translationsFallback: {},
  39. /**
  40. * Load a translation for a given key for a given module.
  41. *
  42. * @param {Module} module The module to load the translation for.
  43. * @param {string} key The key of the text to translate.
  44. * @param {object} variables The variables to use within the translation template (optional)
  45. * @returns {string} the translated key
  46. */
  47. translate: function (module, key, variables) {
  48. variables = variables || {}; //Empty object by default
  49. /**
  50. * Combines template and variables like:
  51. * template: "Please wait for {timeToWait} before continuing with {work}."
  52. * variables: {timeToWait: "2 hours", work: "painting"}
  53. * to: "Please wait for 2 hours before continuing with painting."
  54. *
  55. * @param {string} template Text with placeholder
  56. * @param {object} variables Variables for the placeholder
  57. * @returns {string} the template filled with the variables
  58. */
  59. function createStringFromTemplate(template, variables) {
  60. if (Object.prototype.toString.call(template) !== "[object String]") {
  61. return template;
  62. }
  63. if (variables.fallback && !template.match(new RegExp("{.+}"))) {
  64. template = variables.fallback;
  65. }
  66. return template.replace(new RegExp("{([^}]+)}", "g"), function (_unused, varName) {
  67. return varName in variables ? variables[varName] : "{" + varName + "}";
  68. });
  69. }
  70. if (this.translations[module.name] && key in this.translations[module.name]) {
  71. // Log.log("Got translation for " + key + " from module translation: ");
  72. return createStringFromTemplate(this.translations[module.name][key], variables);
  73. }
  74. if (key in this.coreTranslations) {
  75. // Log.log("Got translation for " + key + " from core translation.");
  76. return createStringFromTemplate(this.coreTranslations[key], variables);
  77. }
  78. if (this.translationsFallback[module.name] && key in this.translationsFallback[module.name]) {
  79. // Log.log("Got translation for " + key + " from module translation fallback.");
  80. return createStringFromTemplate(this.translationsFallback[module.name][key], variables);
  81. }
  82. if (key in this.coreTranslationsFallback) {
  83. // Log.log("Got translation for " + key + " from core translation fallback.");
  84. return createStringFromTemplate(this.coreTranslationsFallback[key], variables);
  85. }
  86. return key;
  87. },
  88. /**
  89. * Load a translation file (json) and remember the data.
  90. *
  91. * @param {Module} module The module to load the translation file for.
  92. * @param {string} file Path of the file we want to load.
  93. * @param {boolean} isFallback Flag to indicate fallback translations.
  94. * @param {Function} callback Function called when done.
  95. */
  96. load(module, file, isFallback, callback) {
  97. Log.log(`${module.name} - Load translation${isFallback && " fallback"}: ${file}`);
  98. if (this.translationsFallback[module.name]) {
  99. callback();
  100. return;
  101. }
  102. loadJSON(module.file(file), (json) => {
  103. const property = isFallback ? "translationsFallback" : "translations";
  104. this[property][module.name] = json;
  105. callback();
  106. });
  107. },
  108. /**
  109. * Load the core translations.
  110. *
  111. * @param {string} lang The language identifier of the core language.
  112. */
  113. loadCoreTranslations: function (lang) {
  114. if (lang in translations) {
  115. Log.log("Loading core translation file: " + translations[lang]);
  116. loadJSON(translations[lang], (translations) => {
  117. this.coreTranslations = translations;
  118. });
  119. } else {
  120. Log.log("Configured language not found in core translations.");
  121. }
  122. this.loadCoreTranslationsFallback();
  123. },
  124. /**
  125. * Load the core translations fallback.
  126. * The first language defined in translations.js will be used.
  127. */
  128. loadCoreTranslationsFallback: function () {
  129. let first = Object.keys(translations)[0];
  130. if (first) {
  131. Log.log("Loading core translation fallback file: " + translations[first]);
  132. loadJSON(translations[first], (translations) => {
  133. this.coreTranslationsFallback = translations;
  134. });
  135. }
  136. }
  137. };
  138. })();
  139. window.Translator = Translator;