jquery.i18n.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * jQuery i18n plugin
  3. * @requires jQuery v1.1 or later
  4. *
  5. * See http://recursive-design.com/projects/jquery-i18n/
  6. *
  7. * Licensed under the MIT license:
  8. * http://www.opensource.org/licenses/mit-license.php
  9. *
  10. * Version: 1.0.0 (201210141329)
  11. */
  12. (function($) {
  13. /**
  14. * i18n provides a mechanism for translating strings using a jscript dictionary.
  15. *
  16. */
  17. /*
  18. * i18n property list
  19. */
  20. $.i18n = {
  21. dict: null,
  22. /**
  23. * setDictionary()
  24. *
  25. * Initialises the dictionary.
  26. *
  27. * @param property_list i18n_dict : The dictionary to use for translation.
  28. */
  29. setDictionary: function(i18n_dict) {
  30. this.dict = i18n_dict;
  31. },
  32. /**
  33. * _()
  34. *
  35. * Looks the given string up in the dictionary and returns the translation if
  36. * one exists. If a translation is not found, returns the original word.
  37. *
  38. * @param string str : The string to translate.
  39. * @param property_list params : params for using printf() on the string.
  40. *
  41. * @return string : Translated word.
  42. */
  43. _: function (str, params) {
  44. var result = str;
  45. if (this.dict && this.dict[str]) {
  46. result = this.dict[str];
  47. }
  48. // Substitute any params.
  49. return this.printf(result, params);
  50. },
  51. /*
  52. * printf()
  53. *
  54. * Substitutes %s with parameters given in list. %%s is used to escape %s.
  55. *
  56. * @param string str : String to perform printf on.
  57. * @param string args : Array of arguments for printf.
  58. *
  59. * @return string result : Substituted string
  60. */
  61. printf: function(str, args) {
  62. if (!args) return str;
  63. var result = '';
  64. var search = /%(\d+)\$s/g;
  65. // Replace %n1$ where n is a number.
  66. var matches = search.exec(str);
  67. while (matches) {
  68. var index = parseInt(matches[1], 10) - 1;
  69. str = str.replace('%' + matches[1] + '\$s', (args[index]));
  70. matches = search.exec(str);
  71. }
  72. var parts = str.split('%s');
  73. if (parts.length > 1) {
  74. for(var i = 0; i < args.length; i++) {
  75. // If the part ends with a '%' chatacter, we've encountered a literal
  76. // '%%s', which we should output as a '%s'. To achieve this, add an
  77. // 's' on the end and merge it with the next part.
  78. if (parts[i].length > 0 && parts[i].lastIndexOf('%') == (parts[i].length - 1)) {
  79. parts[i] += 's' + parts.splice(i + 1, 1)[0];
  80. }
  81. // Append the part and the substitution to the result.
  82. result += parts[i] + args[i];
  83. }
  84. }
  85. return result + parts[parts.length - 1];
  86. }
  87. };
  88. /*
  89. * _t()
  90. *
  91. * Allows you to translate a jQuery selector.
  92. *
  93. * eg $('h1')._t('some text')
  94. *
  95. * @param string str : The string to translate .
  96. * @param property_list params : Params for using printf() on the string.
  97. *
  98. * @return element : Chained and translated element(s).
  99. */
  100. $.fn._t = function(str, params) {
  101. return $(this).text($.i18n._(str, params));
  102. };
  103. })(jQuery);