From 701cf42eef17963fc57159e5383c020e76c0181f Mon Sep 17 00:00:00 2001 From: czaks Date: Tue, 2 Jul 2013 23:10:00 -0400 Subject: [PATCH] main.js: added preliminary translation functions for javascript --- templates/main.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/templates/main.js b/templates/main.js index b3219b3c..78e7cbf1 100644 --- a/templates/main.js +++ b/templates/main.js @@ -1,5 +1,27 @@ {% raw %} +/* gettext-compatible _ function, example of usage: + * + * > // Loading pl_PL.json here (containing polish translation strings generated by tools/locale_compile.php) + * > alert(_("Hello!")); + * Witaj! + */ +function _(s) { + return (typeof tb_l10n != 'undefined' && typeof tb_l10n[s] != 'undefined') ? tb_l10n[s] : s; +} + +/* printf-like formatting function, example of usage: + * + * > alert(fmt("There are {0} birds on {1} trees", [3,4])); + * There are 3 birds on 4 trees + * > // Loading pl_PL.json here (containing polish translation strings generated by tools/locale_compile.php) + * > alert(fmt(_("{0} users"), [3])); + * 3 uzytkownikow + */ +function fmt(s,a) { + return s.replace(/\{([0-9]+)\}/g, function(x) { return a[x[1]]; }); +} + var saved = {};