I18n.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * I18n.js
  3. *
  4. * Copyright, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://www.tinymce.com/license
  8. * Contributing: http://www.tinymce.com/contributing
  9. */
  10. /**
  11. * I18n class that handles translation of TinyMCE UI.
  12. * Uses po style with csharp style parameters.
  13. *
  14. * @class tinymce.util.I18n
  15. */
  16. define("tinymce/util/I18n", [], function() {
  17. "use strict";
  18. var data = {};
  19. return {
  20. /**
  21. * Property gets set to true if a RTL language pack was loaded.
  22. *
  23. * @property rtl
  24. * @type Boolean
  25. */
  26. rtl: false,
  27. /**
  28. * Adds translations for a specific language code.
  29. *
  30. * @method add
  31. * @param {String} code Language code like sv_SE.
  32. * @param {Array} items Name/value array with English en_US to sv_SE.
  33. */
  34. add: function(code, items) {
  35. for (var name in items) {
  36. data[name] = items[name];
  37. }
  38. this.rtl = this.rtl || data._dir === 'rtl';
  39. },
  40. /**
  41. * Translates the specified text.
  42. *
  43. * It has a few formats:
  44. * I18n.translate("Text");
  45. * I18n.translate(["Text {0}/{1}", 0, 1]);
  46. * I18n.translate({raw: "Raw string"});
  47. *
  48. * @method translate
  49. * @param {String/Object/Array} text Text to translate.
  50. * @return {String} String that got translated.
  51. */
  52. translate: function(text) {
  53. if (typeof(text) == "undefined") {
  54. return text;
  55. }
  56. if (typeof(text) != "string" && text.raw) {
  57. return text.raw;
  58. }
  59. if (text.push) {
  60. var values = text.slice(1);
  61. text = (data[text[0]] || text[0]).replace(/\{([^\}]+)\}/g, function(match1, match2) {
  62. return values[match2];
  63. });
  64. }
  65. return data[text] || text;
  66. },
  67. data: data
  68. };
  69. });