您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

translation.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* global $, require, config, interfaceConfig */
  2. var i18n = require("i18next-client");
  3. var languages = require("../../service/translation/languages");
  4. var DEFAULT_LANG = languages.EN;
  5. i18n.addPostProcessor(
  6. "resolveAppName",
  7. value => value.replace("__app__", interfaceConfig.APP_NAME));
  8. var defaultOptions = {
  9. detectLngQS: "lang",
  10. useCookie: false,
  11. fallbackLng: DEFAULT_LANG,
  12. load: "unspecific",
  13. resGetPath: 'lang/__ns__-__lng__.json',
  14. ns: {
  15. namespaces: ['main', 'languages'],
  16. defaultNs: 'main'
  17. },
  18. lngWhitelist : languages.getLanguages(),
  19. fallbackOnNull: true,
  20. fallbackOnEmpty: true,
  21. useDataAttrOptions: true,
  22. app: interfaceConfig.APP_NAME,
  23. getAsync: true,
  24. defaultValueFromContent: false,
  25. customLoad: function(lng, ns, options, done) {
  26. var resPath = "lang/__ns__-__lng__.json";
  27. if(lng === languages.EN)
  28. resPath = "lang/__ns__.json";
  29. var url = i18n.functions.applyReplacement(resPath,
  30. { lng: lng, ns: ns });
  31. i18n.functions.ajax({
  32. url: url,
  33. success: function(data) {
  34. i18n.functions.log('loaded: ' + url);
  35. done(null, data);
  36. },
  37. error : function(xhr, status, error) {
  38. if ((status && status == 200) ||
  39. (xhr && xhr.status && xhr.status == 200)) {
  40. // file loaded but invalid json, stop waste time !
  41. i18n.functions.error('There is a typo in: ' + url);
  42. } else if ((status && status == 404) ||
  43. (xhr && xhr.status && xhr.status == 404)) {
  44. i18n.functions.log('Does not exist: ' + url);
  45. } else {
  46. var theStatus = status ? status :
  47. ((xhr && xhr.status) ? xhr.status : null);
  48. i18n.functions.log(theStatus + ' when loading ' + url);
  49. }
  50. done(error, {});
  51. },
  52. dataType: "json",
  53. async : options.getAsync
  54. });
  55. }
  56. // options for caching
  57. // useLocalStorage: true,
  58. // localStorageExpirationTime: 86400000 // in ms, default 1 week
  59. };
  60. function initCompleted() {
  61. $("[data-i18n]").i18n();
  62. }
  63. function getLangFromQuery() {
  64. var query = window.location.search.substring(1);
  65. var vars = query.split("&");
  66. for (var i=0;i<vars.length;i++) {
  67. var pair = vars[i].split("=");
  68. if(pair[0] == "lang")
  69. {
  70. return pair[1];
  71. }
  72. }
  73. return null;
  74. }
  75. module.exports = {
  76. init: function (settingsLang) {
  77. let options = defaultOptions;
  78. let lang = getLangFromQuery() || settingsLang || config.defaultLanguage;
  79. // XXX If none of the above has been set then the 'lang' will be
  80. // 'undefined' and the i18n lib will try to auto detect user's
  81. // preferred language based on browser's locale.
  82. // The interface config option allows to disable this auto detection
  83. // by specifying the fallback language in that case.
  84. let langDetection = interfaceConfig.LANG_DETECTION;
  85. if (!langDetection && !lang) {
  86. lang = DEFAULT_LANG;
  87. }
  88. if (lang) {
  89. options.lng = lang;
  90. }
  91. i18n.init(options, initCompleted);
  92. },
  93. setLanguage: function (lang) {
  94. if(!lang)
  95. lang = DEFAULT_LANG;
  96. i18n.setLng(lang, defaultOptions, initCompleted);
  97. },
  98. getCurrentLanguage: function () {
  99. return i18n.lng();
  100. },
  101. translateElement: function (selector, options) {
  102. // i18next expects undefined if options are missing, check if its null
  103. selector.i18n(
  104. options === null ? undefined : options);
  105. },
  106. generateTranslationHTML: function (key, options) {
  107. var str = "<span data-i18n=\"" + key + "\"";
  108. if (options) {
  109. str += " data-i18n-options='" + JSON.stringify(options) + "'";
  110. }
  111. str += ">";
  112. // i18next expects undefined if options ARE missing, check if its null
  113. str += i18n.t(key, options === null ? undefined : options);
  114. str += "</span>";
  115. return str;
  116. }
  117. };