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.

translation.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright @ 2015 Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. var i18n = require("i18next-client");
  17. var languages = require("../../service/translation/languages");
  18. var Settings = require("../settings/Settings");
  19. var DEFAULT_LANG = languages.EN;
  20. i18n.addPostProcessor("resolveAppName", function(value, key, options) {
  21. return value.replace("__app__", interfaceConfig.APP_NAME);
  22. });
  23. var defaultOptions = {
  24. detectLngQS: "lang",
  25. useCookie: false,
  26. fallbackLng: DEFAULT_LANG,
  27. load: "unspecific",
  28. resGetPath: 'lang/__ns__-__lng__.json',
  29. ns: {
  30. namespaces: ['main', 'languages'],
  31. defaultNs: 'main'
  32. },
  33. lngWhitelist : languages.getLanguages(),
  34. fallbackOnNull: true,
  35. fallbackOnEmpty: true,
  36. useDataAttrOptions: true,
  37. defaultValueFromContent: false,
  38. app: interfaceConfig.APP_NAME,
  39. getAsync: false,
  40. defaultValueFromContent: false,
  41. customLoad: function(lng, ns, options, done) {
  42. var resPath = "lang/__ns__-__lng__.json";
  43. if(lng === languages.EN)
  44. resPath = "lang/__ns__.json";
  45. var url = i18n.functions.applyReplacement(resPath, { lng: lng, ns: ns });
  46. i18n.functions.ajax({
  47. url: url,
  48. success: function(data, status, xhr) {
  49. i18n.functions.log('loaded: ' + url);
  50. done(null, data);
  51. },
  52. error : function(xhr, status, error) {
  53. if ((status && status == 200) ||
  54. (xhr && xhr.status && xhr.status == 200)) {
  55. // file loaded but invalid json, stop waste time !
  56. i18n.functions.error('There is a typo in: ' + url);
  57. } else if ((status && status == 404) ||
  58. (xhr && xhr.status && xhr.status == 404)) {
  59. i18n.functions.log('Does not exist: ' + url);
  60. } else {
  61. var theStatus = status ? status :
  62. ((xhr && xhr.status) ? xhr.status : null);
  63. i18n.functions.log(theStatus + ' when loading ' + url);
  64. }
  65. done(error, {});
  66. },
  67. dataType: "json",
  68. async : options.getAsync
  69. });
  70. }
  71. // options for caching
  72. // useLocalStorage: true,
  73. // localStorageExpirationTime: 86400000 // in ms, default 1 week
  74. };
  75. function initCompleted(t)
  76. {
  77. $("[data-i18n]").i18n();
  78. }
  79. function checkForParameter() {
  80. var query = window.location.search.substring(1);
  81. var vars = query.split("&");
  82. for (var i=0;i<vars.length;i++) {
  83. var pair = vars[i].split("=");
  84. if(pair[0] == "lang")
  85. {
  86. return pair[1];
  87. }
  88. }
  89. return null;
  90. }
  91. module.exports = {
  92. init: function (lang) {
  93. var options = defaultOptions;
  94. if(!lang)
  95. {
  96. lang = checkForParameter();
  97. if(!lang)
  98. {
  99. var settings = Settings.getSettings();
  100. if(settings)
  101. lang = settings.language;
  102. if(!lang && config.defaultLanguage)
  103. {
  104. lang = config.defaultLanguage;
  105. }
  106. }
  107. }
  108. if(lang) {
  109. options.lng = lang;
  110. }
  111. i18n.init(options, initCompleted);
  112. },
  113. translateString: function (key, options) {
  114. return i18n.t(key, options);
  115. },
  116. setLanguage: function (lang) {
  117. if(!lang)
  118. lang = DEFAULT_LANG;
  119. i18n.setLng(lang, defaultOptions, initCompleted);
  120. },
  121. getCurrentLanguage: function () {
  122. return i18n.lng();
  123. },
  124. translateElement: function (selector) {
  125. selector.i18n();
  126. },
  127. generateTranslatonHTML: function (key, options) {
  128. var str = "<span data-i18n=\"" + key + "\"";
  129. if(options)
  130. {
  131. str += " data-i18n-options=\"" + JSON.stringify(options) + "\"";
  132. }
  133. str += ">";
  134. str += this.translateString(key, options);
  135. str += "</span>";
  136. return str;
  137. }
  138. };