From 8fb5e9c3a4929a6987872a84cfa88c29904639d1 Mon Sep 17 00:00:00 2001 From: colymba Date: Sun, 1 Jun 2014 16:15:52 +0300 Subject: [PATCH] API New JS sprintf and inject replacement functions Fix sprintf issues when replacement was at start of string + introduce new inject function using injection map similar to the PHP version of _t() --- docs/en/topics/i18n.md | 23 ++++++++++++++-- javascript/i18n.js | 59 +++++++++++++++++++++++++++--------------- javascript/i18nx.js | 35 +++++++++++++++++-------- 3 files changed, 83 insertions(+), 34 deletions(-) diff --git a/docs/en/topics/i18n.md b/docs/en/topics/i18n.md index 779133e64..709115576 100644 --- a/docs/en/topics/i18n.md +++ b/docs/en/topics/i18n.md @@ -375,10 +375,16 @@ format which can be processed more easily by external translation providers (see alert(ss.i18n._t('MYMODULE.MYENTITY')); -### Advanced Usage with sprintf() +### Advanced Use + +The `ss.i18n` object contain a couple functions to help and replace dynamic variable from within a string. + +#### Legacy sequential replacement with sprintf() + + `sprintf()` will substitute occurencies of `%s` in the main string with each of the following arguments passed to the function. The substitution is done sequentially. :::js - // MYMODULE.MYENTITY contains "Really delete %s articles by %s authors?" + // MYMODULE.MYENTITY contains "Really delete %s articles by %s?" alert(ss.i18n.sprintf( ss.i18n._t('MYMODULE.MYENTITY'), 42, @@ -387,6 +393,19 @@ format which can be processed more easily by external translation providers (see // Displays: "Really delete 42 articles by Douglas Adams?" +#### Variable injection with inject() + + `inject()` will substitute variables in the main string like `{myVar}` by the keys in the object passed as second argument. Each variable can be in any order and appear multiple times. + + :::js + // MYMODULE.MYENTITY contains "Really delete {count} articles by {author}?" + alert(ss.i18n.inject( + ss.i18n._t('MYMODULE.MYENTITY'), + {count: 42, author: 'Douglas Adams'} + )); + // Displays: "Really delete 42 articles by Douglas Adams?" + + ## Limitations * No detecting/conversion of character encodings (we rely fully on UTF-8) diff --git a/javascript/i18n.js b/javascript/i18n.js index 7cc19d698..d1426e2ca 100644 --- a/javascript/i18n.js +++ b/javascript/i18n.js @@ -138,33 +138,50 @@ ss.i18n = { return stripStr(parts.join(" ")); }, - /* - * printf() - * C-printf like function, which substitutes %s with parameters - * given in list. %%s is used to escape %s. - * - * Doesn't work in IE5.0 (splice) - * - * @param string S : string to perform printf on. - * @param string L : Array of arguments for printf() - */ + /** + * Substitutes %s with parameters + * given in list. %%s is used to escape %s. + * + * @param string S : The string to perform the substitutions on. + * @return string The new string with substitutions made + */ sprintf: function(S) { if (arguments.length == 1) return S; - var nS = ""; - var tS = S.split("%s"); - - var args = []; - for (var i=1, len = arguments.length; i