diff options
author | Peter <Peter.Gijsels@gmail.com> | 2010-02-28 12:08:02 (GMT) |
---|---|---|
committer | Peter <Peter.Gijsels@gmail.com> | 2010-02-28 12:08:02 (GMT) |
commit | c2ffd7a66e70c286df5b886e6afac4820522284e (patch) | |
tree | 1d3777d6ad22e4bcedd54953ac876f0f0ba45842 | |
parent | 349c7f57ea1abd0b16cc3f6d7c9a8398e0227101 (diff) |
added $.i18n.ngettext
usage: ngettext(msgid1, msgid2, n)
Depending on n the correct msg is chosen.
E.g.
ngettext("Delete the selected file?",
"Delete the selected files?",
n);
-rwxr-xr-x | examples/6_Maths_matchingAnglesAndShapes/js/lesson.js | 60 | ||||
-rw-r--r-- | examples/6_Maths_matchingAnglesAndShapes/js/messages.es.json | 11 | ||||
-rw-r--r-- | js/jquery.i18n.js | 17 |
3 files changed, 84 insertions, 4 deletions
diff --git a/examples/6_Maths_matchingAnglesAndShapes/js/lesson.js b/examples/6_Maths_matchingAnglesAndShapes/js/lesson.js index 15d75cc..9dcded6 100755 --- a/examples/6_Maths_matchingAnglesAndShapes/js/lesson.js +++ b/examples/6_Maths_matchingAnglesAndShapes/js/lesson.js @@ -1,6 +1,6 @@ /* Bugs (firefox 3.5.7): -* sometimes multiple cards are shown +* sometimes mulitple cards are shown * the image of one of the angles is not showing properly * if you have a text without a hyphen it doesn't work (e.g. angulo recto iso angulo-recto) @@ -17,6 +17,31 @@ Peeves: */ +// TBD: use jquery plugin instead, http://plugins.jquery.com/project/psprintf +function format(format_string /*, args*/) { + var args = [].slice.call(arguments); // arguments is not a real array + args.shift(); + var result = ''; + for (var i = 0; i < format_string.length; i += 1) { + var c = format_string.charAt(i); + if (c == '%') { + i += 1; // Breaks on format_string ending with % + var c2 = format_string.charAt(i); + if (c2 == '%') { + result += '%' + } else if (c2 == 'd') { + result += args.shift(); + } else { + alert('unsupported format character: ' + c2); + } + } else { + result += c; + } + } + return result; +} + + $(document).ready(function() { var _ = $._; var i = 0, j = 0, flag = 0; @@ -70,15 +95,42 @@ $(document).ready(function() { }; + // example of ngettext usage + alert(function(clickCounter, h, m, s) { + return format($.i18n.ngettext('You have completed the game in <span class="specialText">%d</span> clicks within <span class="specialText">%d</span> hour,', + 'You have completed the game in <span class="specialText">%d</span> clicks within <span class="specialText">%d</span> hours,', + h), + clickCounter, h) + + format($.i18n.ngettext('<span class="specialText">%d</span> minute and ', + '<span class="specialText">%d</span> minutes and ', + m), + m) + + format ($.i18n.ngettext('<span class="specialText">%d</span> second.', + '<span class="specialText">%d</span> seconds.', + s), + s); + }(10, 2, 3, 1)); var check_game_over = function(){ if(numMatched === NUM_OBJECTS){ //show all play = 0; $('#content').html(''); $('#content').append('<div id="gameOver">GAME OVER<br/>Congratulations!!!</div>'); - $('#content').append('<div id="gameOverInfo">You have completed the game in <span class="specialText">'+clickCounter+ - '</span> clicks within <span class="specialText">'+h+'</span> hour <span class="specialText">'+m+ - '</span> minutes and <span class="specialText">'+s+'</span> seconds .</div>'); + $('#content').append($(document.createElement('div')) + .attr({id: 'gameOverInfo'}) + // clicks < 2 is impossible + .html(format($.i18n.ngettext('You have completed the game in <span class="specialText">%d</span> clicks within <span class="specialText">%d</span> hour,', + 'You have completed the game in <span class="specialText">%d</span> clicks within <span class="specialText">%d</span> hours,', + h), + clickCounter, h) + + format($.i18n.ngettext('<span class="specialText">%d</span> minute and ', + '<span class="specialText">%d</span> minutes and ', + m), + m) + + format ($.i18n.ngettext('<span class="specialText">%d</span> second.', + '<span class="specialText">%d</span> seconds.', + s), + s))); } }; diff --git a/examples/6_Maths_matchingAnglesAndShapes/js/messages.es.json b/examples/6_Maths_matchingAnglesAndShapes/js/messages.es.json index 13dbad9..d7dffd9 100644 --- a/examples/6_Maths_matchingAnglesAndShapes/js/messages.es.json +++ b/examples/6_Maths_matchingAnglesAndShapes/js/messages.es.json @@ -16,4 +16,15 @@ $.i18n.es.strings = { 'Maths: Matching Angles with Shapes' : 'Matemáticas: reunir angulos y formas' }; +// You can put these inside the dict above, but for now this will be easier to merge. +$.i18n.es.strings['You have completed the game in <span class="specialText">%d</span> clicks within <span class="specialText">%d</span> hour,'] = + ['Has acabado el juego en <span class="specialText">%d</span> clics en <span class="specialText">%d</span> hora,', + 'Has acabado el juego en <span class="specialText">%d</span> clics en <span class="specialText">%d</span> horas,']; +$.i18n.es.strings['<span class="specialText">%d</span> minute and '] = + ['<span class="specialText">%d</span> minuto y ', + '<span class="specialText">%d</span> minutos y ']; +$.i18n.es.strings['<span class="specialText">%d</span> second.'] = + ['<span class="specialText">%d</span> segundo.', + '<span class="specialText">%d</span> segundos.']; + $(function() { $.i18n.setLocale('es'); }); diff --git a/js/jquery.i18n.js b/js/jquery.i18n.js index 0543dcf..4a9f862 100644 --- a/js/jquery.i18n.js +++ b/js/jquery.i18n.js @@ -6,6 +6,7 @@ */ (function($){ + // PG: Maybe put everything in namespace i18n and rename the following function $.i18n.gettext? $.i18n = function(string, locale){ var lang = locale || $.i18n.lang; @@ -15,6 +16,22 @@ return $.i18n[lang].strings[string]||string; }; + // You can override this in messages.<lang>.json. + $.i18n.choose_pluralized_msg = function (choices, n) { + return n == 1 ? choices[0] : choices[1]; + } + + $.i18n.ngettext = function (msgid1, msgid2, n) { + var lang = $.i18n.lang; + if (!$.i18n[lang] || !$.i18n[lang].strings) { + return $.choose_pluralized_msg([msgid1, msgid2], n); + } + // Is using msgid1 as the key ok? + return $.i18n.choose_pluralized_msg($.i18n[lang].strings[msgid1] + || [msgid1, msgid2], + n) + } + $._ = $.i18n; $.i18n.setLocale = function (locale){ |