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.

197 lines
8.8 KiB

2 years ago
  1. (function () {
  2. // add your module to this map so it gets used
  3. var map = {
  4. 'ca': 'Català',
  5. 'de': 'Deutsch',
  6. 'el': 'Ελληνικά',
  7. 'es': 'Español',
  8. 'fi': 'Suomalainen',
  9. 'fr': 'Français',
  10. //'hi': 'हिन्दी',
  11. 'it': 'Italiano',
  12. 'nb': 'Norwegian Bokmål',
  13. //'pl': 'Polski',
  14. 'pt-br': 'Português do Brasil',
  15. 'ro': 'Română',
  16. 'ru': 'Русский',
  17. //'sv': 'Svenska',
  18. //'te': 'తెలుగు',
  19. 'zh': '繁體中文',
  20. //'nl': 'Nederlands'
  21. };
  22. var messages = {};
  23. var LS_LANG = "CRYPTPAD_LANG";
  24. var getStoredLanguage = function () { return localStorage && localStorage.getItem(LS_LANG); };
  25. var getBrowserLanguage = function () { return navigator.language || navigator.userLanguage || ''; };
  26. var getLanguage = messages._getLanguage = function () {
  27. if (window.cryptpadLanguage) { return window.cryptpadLanguage; }
  28. if (getStoredLanguage()) { return getStoredLanguage(); }
  29. var l = getBrowserLanguage();
  30. // Edge returns 'fr-FR' --> transform it to 'fr' and check again
  31. return map[l] ? l :
  32. (map[l.split('-')[0]] ? l.split('-')[0] :
  33. (map[l.split('_')[0]] ? l.split('_')[0] : 'en'));
  34. };
  35. var language = getLanguage();
  36. // Translations files were migrated from requirejs modules to json.
  37. // To avoid asking every administrator to update their customized translation files,
  38. // we use a requirejs map to redirect the old path to the new one and to use the
  39. // requirejs json plugin
  40. var reqPaths = {
  41. "/common/translations/messages.js":"json!/common/translations/messages.json"
  42. };
  43. Object.keys(map).forEach(function (k) {
  44. reqPaths["/common/translations/messages."+k+".js"] = "json!/common/translations/messages."+k+".json";
  45. });
  46. require.config({
  47. map: {
  48. "*": reqPaths
  49. }
  50. });
  51. var req = [
  52. '/common/common-util.js',
  53. '/customize/application_config.js',
  54. '/customize/translations/messages.js'
  55. ];
  56. if (language && map[language]) { req.push('/customize/translations/messages.' + language + '.js'); }
  57. define(req, function(Util, AppConfig, Default, Language) {
  58. map.en = 'English';
  59. var defaultLanguage = 'en';
  60. if (AppConfig.availableLanguages) {
  61. if (AppConfig.availableLanguages.indexOf(language) === -1) {
  62. language = defaultLanguage;
  63. Language = Default;
  64. localStorage.setItem(LS_LANG, language);
  65. }
  66. Object.keys(map).forEach(function (l) {
  67. if (l === defaultLanguage) { return; }
  68. if (AppConfig.availableLanguages.indexOf(l) === -1) {
  69. delete map[l];
  70. }
  71. });
  72. }
  73. var extend = function (a, b) {
  74. for (var k in b) {
  75. if (Util.isObject(b[k])) {
  76. a[k] = Util.isObject(a[k]) ? a[k] : {};
  77. extend(a[k], b[k]);
  78. continue;
  79. }
  80. if (Array.isArray(b[k])) {
  81. a[k] = b[k].slice();
  82. continue;
  83. }
  84. a[k] = b[k] || a[k];
  85. }
  86. };
  87. extend(messages, Default);
  88. if (Language && language !== defaultLanguage) {
  89. // Add the translated keys to the returned object
  90. extend(messages, Language);
  91. }
  92. messages._languages = map;
  93. messages._languageUsed = language;
  94. messages._checkTranslationState = function (cb) {
  95. if (typeof(cb) !== "function") { return; }
  96. var allMissing = [];
  97. var reqs = [];
  98. Object.keys(map).forEach(function (code) {
  99. if (code === defaultLanguage) { return; }
  100. reqs.push('/customize/translations/messages.' + code + '.js');
  101. });
  102. require(reqs, function () {
  103. var langs = arguments;
  104. Object.keys(map).forEach(function (code, i) {
  105. if (code === defaultLanguage) { return; }
  106. var translation = langs[i];
  107. var missing = [];
  108. var checkInObject = function (ref, translated, path) {
  109. var updated = {};
  110. Object.keys(ref).forEach(function (k) {
  111. if (/^updated_[0-9]+_/.test(k) && !translated[k]) {
  112. var key = k.split('_').slice(2).join('_');
  113. // Make sure we don't already have an update for that key. It should not happen
  114. // but if it does, keep the latest version
  115. if (updated[key]) {
  116. var ek = updated[key];
  117. if (parseInt(ek.split('_')[1]) > parseInt(k.split('_')[1])) { return; }
  118. }
  119. updated[key] = k;
  120. }
  121. });
  122. Object.keys(ref).forEach(function (k) {
  123. if (/^_/.test(k) || k === 'driveReadme') { return; }
  124. var nPath = path.slice();
  125. nPath.push(k);
  126. if (!translated[k] || updated[k]) {
  127. if (updated[k]) {
  128. var uPath = path.slice();
  129. uPath.unshift('out');
  130. missing.push([code, nPath, 2, uPath.join('.') + '.' + updated[k]]);
  131. return;
  132. }
  133. return void missing.push([code, nPath, 1]);
  134. }
  135. if (typeof ref[k] !== typeof translated[k]) {
  136. return void missing.push([code, nPath, 3]);
  137. }
  138. if (typeof ref[k] === "object" && !Array.isArray(ref[k])) {
  139. checkInObject(ref[k], translated[k], nPath);
  140. }
  141. });
  142. Object.keys(translated).forEach(function (k) {
  143. if (/^_/.test(k) || k === 'driveReadme') { return; }
  144. var nPath = path.slice();
  145. nPath.push(k);
  146. if (typeof ref[k] === "undefined") {
  147. missing.push([code, nPath, 0]);
  148. }
  149. });
  150. };
  151. checkInObject(Default, translation, []);
  152. // Push the removals at the end
  153. missing.sort(function (a, b) {
  154. if (a[2] === 0 && b[2] !== 0) { return 1; }
  155. if (a[2] !== 0 && b[2] === 0) { return -1; }
  156. return 0;
  157. });
  158. Array.prototype.push.apply(allMissing, missing); // Destructive concat
  159. });
  160. cb(allMissing);
  161. });
  162. };
  163. // Get keys with parameters
  164. messages._getKey = function (key, argArray) {
  165. if (!messages[key]) { return '?'; }
  166. var text = messages[key];
  167. if (typeof(text) === 'string') {
  168. return text.replace(/\{(\d+)\}/g, function (str, p1) {
  169. if (typeof(argArray[p1]) === 'string' || typeof(argArray[p1]) === "number") {
  170. return argArray[p1];
  171. }
  172. console.error("Only strings and numbers can be used in _getKey params!");
  173. return '';
  174. });
  175. } else {
  176. return text;
  177. }
  178. };
  179. messages.driveReadme = '["BODY",{"class":"cke_editable cke_editable_themed cke_contents_ltr cke_show_borders","contenteditable":"true","spellcheck":"false","style":"color: rgb(51, 51, 51);"},' +
  180. '[["H1",{},["'+messages.readme_welcome+'"]],["P",{},["'+messages.readme_p1+'"]],["P",{},["'+messages.readme_p2+'"]],["HR",{},[]],["H2",{},["'+messages.readme_cat1+'",["BR",{},[]]]],["UL",{},[["LI",{},["'+messages._getKey("readme_cat1_l1", ['",["STRONG",{},["'+messages.newButton+'"]],"', '",["STRONG",{},["'+messages.type.pad+'"]],"'])+'"]],["LI",{},["'+messages.readme_cat1_l2+'"]],["LI",{},["'+messages._getKey("readme_cat1_l3", ['",["STRONG",{},["'+messages.fm_unsortedName+'"]],"'])+'",["UL",{},[["LI",{},["'+messages._getKey("readme_cat1_l3_l1", ['",["STRONG",{},["'+messages.fm_rootName+'"]],"'])+'"]],["LI",{},["'+messages.readme_cat1_l3_l2+'"]]]]]],["LI",{},["'+messages._getKey("readme_cat1_l4", ['",["STRONG",{},["'+messages.fm_trashName+'"]],"'])+'",["BR",{},[]]]]]],["P",{},[["BR",{},[]]]],["H2",{},["'+messages.readme_cat2+'",["BR",{},[]]]],["UL",{},[["LI",{},["'+messages._getKey("readme_cat2_l1", ['",["STRONG",{},["'+messages.shareButton+'"]],"', '",["STRONG",{},["'+messages.edit+'"]],"', '",["STRONG",{},["'+messages.view+'"]],"'])+'"]],["LI",{},["'+messages.readme_cat2_l2+'"]]]],["P",{},[["BR",{},[]]]],["H2",{},["'+messages.readme_cat3+'"]],["UL",{},[["LI",{},["'+messages.readme_cat3_l1+'"]],["LI",{},["'+messages.readme_cat3_l2+'"]],["LI",{},["'+messages.readme_cat3_l3+'",["BR",{},[]]]]]]],' +
  181. '{"metadata":{"defaultTitle":"' + messages.driveReadmeTitle + '","title":"' + messages.driveReadmeTitle + '"}}]';
  182. return messages;
  183. });
  184. }());