nls.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*!-----------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Version: 0.5.2(d49899a916fd99840f6f9178f2dd06e0e7013646)
  4. * Released under the MIT license
  5. * https://github.com/Microsoft/vscode/blob/master/LICENSE.txt
  6. *-----------------------------------------------------------*/
  7. /*---------------------------------------------------------------------------------------------
  8. * Copyright (c) Microsoft Corporation. All rights reserved.
  9. * Licensed under the MIT License. See License.txt in the project root for license information.
  10. *--------------------------------------------------------------------------------------------*/
  11. /*---------------------------------------------------------------------------------------------
  12. *---------------------------------------------------------------------------------------------
  13. *---------------------------------------------------------------------------------------------
  14. *---------------------------------------------------------------------------------------------
  15. *---------------------------------------------------------------------------------------------
  16. * Please make sure to make edits in the .ts file at https://github.com/Microsoft/vscode-loader/
  17. *---------------------------------------------------------------------------------------------
  18. *---------------------------------------------------------------------------------------------
  19. *---------------------------------------------------------------------------------------------
  20. *---------------------------------------------------------------------------------------------
  21. *--------------------------------------------------------------------------------------------*/
  22. 'use strict';
  23. var _nlsPluginGlobal = this;
  24. var NLSLoaderPlugin;
  25. (function (NLSLoaderPlugin) {
  26. var global = _nlsPluginGlobal;
  27. var Resources = global.Plugin && global.Plugin.Resources ? global.Plugin.Resources : undefined;
  28. var DEFAULT_TAG = 'i-default';
  29. var IS_PSEUDO = (global && global.document && global.document.location && global.document.location.hash.indexOf('pseudo=true') >= 0);
  30. var slice = Array.prototype.slice;
  31. function _format(message, args) {
  32. var result;
  33. if (args.length === 0) {
  34. result = message;
  35. }
  36. else {
  37. result = message.replace(/\{(\d+)\}/g, function (match, rest) {
  38. var index = rest[0];
  39. return typeof args[index] !== 'undefined' ? args[index] : match;
  40. });
  41. }
  42. if (IS_PSEUDO) {
  43. // FF3B and FF3D is the Unicode zenkaku representation for [ and ]
  44. result = '\uFF3B' + result.replace(/[aouei]/g, '$&$&') + '\uFF3D';
  45. }
  46. return result;
  47. }
  48. function findLanguageForModule(config, name) {
  49. var result = config[name];
  50. if (result)
  51. return result;
  52. result = config['*'];
  53. if (result)
  54. return result;
  55. return null;
  56. }
  57. function localize(data, message) {
  58. var args = [];
  59. for (var _i = 0; _i < (arguments.length - 2); _i++) {
  60. args[_i] = arguments[_i + 2];
  61. }
  62. return _format(message, args);
  63. }
  64. function createScopedLocalize(scope) {
  65. return function (idx, defaultValue) {
  66. var restArgs = slice.call(arguments, 2);
  67. return _format(scope[idx], restArgs);
  68. };
  69. }
  70. var NLSPlugin = (function () {
  71. function NLSPlugin() {
  72. this.localize = localize;
  73. }
  74. NLSPlugin.prototype.setPseudoTranslation = function (value) {
  75. IS_PSEUDO = value;
  76. };
  77. NLSPlugin.prototype.create = function (key, data) {
  78. return {
  79. localize: createScopedLocalize(data[key])
  80. };
  81. };
  82. NLSPlugin.prototype.load = function (name, req, load, config) {
  83. config = config || {};
  84. if (!name || name.length === 0) {
  85. load({
  86. localize: localize
  87. });
  88. }
  89. else {
  90. var suffix = void 0;
  91. if (Resources && Resources.getString) {
  92. suffix = '.nls.keys';
  93. req([name + suffix], function (keyMap) {
  94. load({
  95. localize: function (moduleKey, index) {
  96. if (!keyMap[moduleKey])
  97. return 'NLS error: unknown key ' + moduleKey;
  98. var mk = keyMap[moduleKey].keys;
  99. if (index >= mk.length)
  100. return 'NLS error unknow index ' + index;
  101. var subKey = mk[index];
  102. var args = [];
  103. args[0] = moduleKey + '_' + subKey;
  104. for (var _i = 0; _i < (arguments.length - 2); _i++) {
  105. args[_i + 1] = arguments[_i + 2];
  106. }
  107. return Resources.getString.apply(Resources, args);
  108. }
  109. });
  110. });
  111. }
  112. else {
  113. if (config.isBuild) {
  114. req([name + '.nls', name + '.nls.keys'], function (messages, keys) {
  115. NLSPlugin.BUILD_MAP[name] = messages;
  116. NLSPlugin.BUILD_MAP_KEYS[name] = keys;
  117. load(messages);
  118. });
  119. }
  120. else {
  121. var pluginConfig = config['vs/nls'] || {};
  122. var language = pluginConfig.availableLanguages ? findLanguageForModule(pluginConfig.availableLanguages, name) : null;
  123. suffix = '.nls';
  124. if (language !== null && language !== DEFAULT_TAG) {
  125. suffix = suffix + '.' + language;
  126. }
  127. req([name + suffix], function (messages) {
  128. if (Array.isArray(messages)) {
  129. messages.localize = createScopedLocalize(messages);
  130. }
  131. else {
  132. messages.localize = createScopedLocalize(messages[name]);
  133. }
  134. load(messages);
  135. });
  136. }
  137. }
  138. }
  139. };
  140. NLSPlugin.prototype._getEntryPointsMap = function () {
  141. global.nlsPluginEntryPoints = global.nlsPluginEntryPoints || {};
  142. return global.nlsPluginEntryPoints;
  143. };
  144. NLSPlugin.prototype.write = function (pluginName, moduleName, write) {
  145. // getEntryPoint is a Monaco extension to r.js
  146. var entryPoint = write.getEntryPoint();
  147. // r.js destroys the context of this plugin between calling 'write' and 'writeFile'
  148. // so the only option at this point is to leak the data to a global
  149. var entryPointsMap = this._getEntryPointsMap();
  150. entryPointsMap[entryPoint] = entryPointsMap[entryPoint] || [];
  151. entryPointsMap[entryPoint].push(moduleName);
  152. if (moduleName !== entryPoint) {
  153. write.asModule(pluginName + '!' + moduleName, 'define([\'vs/nls\', \'vs/nls!' + entryPoint + '\'], function(nls, data) { return nls.create("' + moduleName + '", data); });');
  154. }
  155. };
  156. NLSPlugin.prototype.writeFile = function (pluginName, moduleName, req, write, config) {
  157. var entryPointsMap = this._getEntryPointsMap();
  158. if (entryPointsMap.hasOwnProperty(moduleName)) {
  159. var fileName = req.toUrl(moduleName + '.nls.js');
  160. var contents = [
  161. '/*---------------------------------------------------------',
  162. ' * Copyright (c) Microsoft Corporation. All rights reserved.',
  163. ' *--------------------------------------------------------*/'
  164. ], entries = entryPointsMap[moduleName];
  165. var data = {};
  166. for (var i = 0; i < entries.length; i++) {
  167. data[entries[i]] = NLSPlugin.BUILD_MAP[entries[i]];
  168. }
  169. contents.push('define("' + moduleName + '.nls", ' + JSON.stringify(data, null, '\t') + ');');
  170. write(fileName, contents.join('\r\n'));
  171. }
  172. };
  173. NLSPlugin.prototype.finishBuild = function (write) {
  174. write('nls.metadata.json', JSON.stringify({
  175. keys: NLSPlugin.BUILD_MAP_KEYS,
  176. messages: NLSPlugin.BUILD_MAP,
  177. bundles: this._getEntryPointsMap()
  178. }, null, '\t'));
  179. };
  180. ;
  181. NLSPlugin.BUILD_MAP = {};
  182. NLSPlugin.BUILD_MAP_KEYS = {};
  183. return NLSPlugin;
  184. }());
  185. NLSLoaderPlugin.NLSPlugin = NLSPlugin;
  186. (function () {
  187. define('vs/nls', new NLSPlugin());
  188. })();
  189. })(NLSLoaderPlugin || (NLSLoaderPlugin = {}));
  190. //# sourceMappingURL=nls.js.map