diff --git a/build/t9n.js b/build/t9n.js index 99ff736..d37f2bf 100644 --- a/build/t9n.js +++ b/build/t9n.js @@ -5,7 +5,6 @@ if (typeof Meteor !== "undefined" && Meteor !== null) { Meteor.startup(function() { if (Meteor.isClient) { - console.log('startup4'); return typeof Template !== "undefined" && Template !== null ? Template.registerHelper('t9n', function(x, params) { return T9n.get(x, true, params.hash); }) : void 0; @@ -139,33 +138,21 @@ } static replaceParams(str, args) { - var arg, index1, index2, line, lines, strCopy, token, tokenName, value; + var index1, index2, strCopy, token, tokenName, value; index1 = 0; strCopy = str; while (index1 > -1) { index1 = strCopy.indexOf('@{'); if (index1 > -1) { - index2 = strCopy.substring(index1).indexOf('}'); + index2 = strCopy.substring(index1).indexOf('}'); // no nested tokens token = strCopy.substring(index1, index2 + 1); if (token.indexOf('->') > -1) { - lines = strCopy.split(/\n/); - tokenName = token.substring(2, token.indexOf(' ')); - arg = args[tokenName]; - line = lines.find(function(l) { - return l.indexOf(`[${arg}]`) > -1; - }); - if (line) { - value = line.substring(line.lastIndexOf(']') + 1).trim(); - } else { - line = lines.find(function(l) { - return l.indexOf('[*]') > -1; - }); - value = line.substring(line.lastIndexOf(']') + 1).trim().replace(new RegExp(`\\$${tokenName}`), args[tokenName]); - } - str = str.replace(token, value); + value = this.handleSelector(strCopy, args, token); + str = str.replace(token, value); // no selector, simply replace the token } else { tokenName = token.substring(2, token.indexOf('}')); - str = str.replace(token, args[tokenName]); + str = str.replace(new RegExp(token, 'g'), args[tokenName]); + str = str.replace(new RegExp(`\\$${tokenName}`, 'g'), args[tokenName]); } strCopy = strCopy.substring(index2 + 2).trim(); } @@ -173,6 +160,25 @@ return str; } + static handleSelector(str, args, token) { + var foundLine, lineMap, tokenName; + tokenName = token.substring(2, token.indexOf(' ')); + lineMap = str.split(/\n/).slice(1).map(function(line) { + var regexString; + regexString = line.trim().split(/\s/)[0]; + return { + regexString, + line: line.substring(line.indexOf(regexString) + regexString.length) + }; + }); + foundLine = lineMap.find(function(map) { + return new RegExp(map.regexString).test(args[tokenName]); + }); + if (foundLine) { + return foundLine.line.substring(foundLine.line.lastIndexOf(']') + 1).trim().replace(new RegExp(`\\$${tokenName}`), args[tokenName]); + } + } + }; T9n.maps = {}; diff --git a/package.js b/package.js index 7be7b81..7301978 100644 --- a/package.js +++ b/package.js @@ -1,6 +1,6 @@ Package.describe({ summary: "Almost i18n, with standard translations for basic meteor packages.", - version: "2.4.0", + version: "2.5.0", name: "softwarerero:accounts-t9n", git: "https://github.com/softwarerero/meteor-accounts-t9n.git", }); diff --git a/package.json b/package.json index 4e86574..f69ae33 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "meteor-accounts-t9n", - "version": "2.4.0", + "version": "2.5.0", "description": "Translations for Meteor projects, almost i18n", "repository": "https://github.com/softwarerero/meteor-accounts-t9n", "bugs": "https://github.com/softwarerero/meteor-accounts-t9n/issues", diff --git a/smart.json b/smart.json index 0ac754e..a106101 100644 --- a/smart.json +++ b/smart.json @@ -3,7 +3,7 @@ "description": "Translations for the meteor account's error messages", "homepage": "https://github.com/softwarerero/meteor-accounts-t9n", "author": "Stefan Undorf ", - "version": "2.4.0", + "version": "2.5.0", "git": "https://github.com/softwarerero/meteor-accounts-t9n.git", "packages": {} } diff --git a/t9n.coffee b/t9n.coffee index 4c13cf5..0691f00 100644 --- a/t9n.coffee +++ b/t9n.coffee @@ -1,6 +1,5 @@ Meteor?.startup -> if Meteor.isClient - console.log('startup4') Template?.registerHelper 't9n', (x, params) -> T9n.get(x, true, params.hash) @@ -75,36 +74,36 @@ class T9n throw Error "language #{language} does not exist" @language = language @depLanguage?.changed() - - + @replaceParams = (str, args) -> index1 = 0 strCopy = str while index1 > -1 index1 = strCopy.indexOf('@{') if index1 > -1 - index2 = strCopy.substring(index1).indexOf('}') + index2 = strCopy.substring(index1).indexOf('}') # no nested tokens token = strCopy.substring(index1, index2 + 1) - if token.indexOf('->') > -1 - lines = strCopy.split(/\n/) - tokenName = token.substring(2, token.indexOf(' ')) - arg = args[tokenName] - line = lines.find((l) -> l.indexOf("[#{arg}]") > -1) - if line - value = line.substring(line.lastIndexOf(']') + 1).trim() - else - line = lines.find((l) -> l.indexOf('[*]') > -1) - value = - line.substring(line.lastIndexOf(']') + 1) - .trim() - .replace(new RegExp("\\$#{tokenName}"), args[tokenName]) + if token.indexOf('->') > -1 + value = @handleSelector(strCopy, args, token) str = str.replace(token, value) - else + else # no selector, simply replace the token tokenName = token.substring(2, token.indexOf('}')) - str = str.replace(token, args[tokenName]) + str = str.replace(new RegExp(token, 'g'), args[tokenName]) + str = str.replace(new RegExp("\\$#{tokenName}", 'g'), args[tokenName]) strCopy = strCopy.substring(index2+2).trim() return str + @handleSelector = (str, args, token) -> + tokenName = token.substring(2, token.indexOf(' ')) + lineMap = str.split(/\n/).slice(1).map (line) -> + regexString = line.trim().split(/\s/)[0] + {regexString, line: line.substring(line.indexOf(regexString) + regexString.length)} + foundLine = lineMap.find (map) -> new RegExp(map.regexString).test(args[tokenName]) + if foundLine + foundLine.line.substring(foundLine.line.lastIndexOf(']') + 1) + .trim() + .replace(new RegExp("\\$#{tokenName}"), args[tokenName]) + @T9n = T9n @t9n = (x, includePrefix, params) -> T9n.get(x) module?.exports.T9n = T9n