jquery.i18n.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*!
  2. * jQuery i18n plugin
  3. * @requires jQuery v1.1 or later
  4. *
  5. * See https://github.com/recurser/jquery-i18n
  6. *
  7. * Licensed under the MIT license.
  8. *
  9. * Version: <%= pkg.version %> (<%= meta.date %>)
  10. */
  11. (function($) {
  12. /**
  13. * i18n provides a mechanism for translating strings using a jscript dictionary.
  14. *
  15. */
  16. var __slice = Array.prototype.slice;
  17. /*
  18. * i18n property list
  19. */
  20. var i18n = {
  21. dict: null,
  22. missingPattern: null,
  23. /**
  24. * load()
  25. *
  26. * Load translations.
  27. *
  28. * @param property_list i18nDict : The dictionary to use for translation.
  29. */
  30. load: function(i18nDict, missingPattern) {
  31. if (this.dict !== null) {
  32. $.extend(this.dict, i18nDict);
  33. } else {
  34. this.dict = i18nDict;
  35. }
  36. if (missingPattern) {
  37. this.missingPattern = missingPattern;
  38. }
  39. },
  40. /**
  41. * unload()
  42. *
  43. * Unloads translations and clears the dictionary.
  44. */
  45. unload: function() {
  46. this.dict = null;
  47. this.missingPattern = null;
  48. },
  49. /**
  50. * _()
  51. *
  52. * Looks the given string up in the dictionary and returns the translation if
  53. * one exists. If a translation is not found, returns the original word.
  54. *
  55. * @param string str : The string to translate.
  56. * @param property_list params.. : params for using printf() on the string.
  57. *
  58. * @return string : Translated word.
  59. */
  60. _: function (str) {
  61. dict = this.dict;
  62. if (dict && dict.hasOwnProperty(str)) {
  63. str = dict[str];
  64. } else if (this.missingPattern !== null) {
  65. return this.printf(this.missingPattern, str);
  66. }
  67. args = __slice.call(arguments);
  68. args[0] = str;
  69. // Substitute any params.
  70. return this.printf.apply(this, args);
  71. },
  72. /*
  73. * printf()
  74. *
  75. * Substitutes %s with parameters given in list. %%s is used to escape %s.
  76. *
  77. * @param string str : String to perform printf on.
  78. * @param string args : Array of arguments for printf.
  79. *
  80. * @return string result : Substituted string
  81. */
  82. printf: function(str, args) {
  83. if (arguments.length < 2) return str;
  84. args = $.isArray(args) ? args : __slice.call(arguments, 1);
  85. return str.replace(/([^%]|^)%(?:(\d+)\$)?s/g, function(p0, p, position) {
  86. if (position) {
  87. return p + args[parseInt(position)-1];
  88. }
  89. return p + args.shift();
  90. }).replace(/%%s/g, '%s');
  91. }
  92. };
  93. /*
  94. * _t()
  95. *
  96. * Allows you to translate a jQuery selector.
  97. *
  98. * eg $('h1')._t('some text')
  99. *
  100. * @param string str : The string to translate .
  101. * @param property_list params : Params for using printf() on the string.
  102. *
  103. * @return element : Chained and translated element(s).
  104. */
  105. $.fn._t = function(str, params) {
  106. return $(this).html(i18n._.apply(i18n, arguments));
  107. };
  108. $.i18n = i18n;
  109. })(jQuery);