Skip to content

Commit

Permalink
Use regex for selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
softwarerero committed May 4, 2019
1 parent 7708d79 commit bfddc8e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 41 deletions.
44 changes: 25 additions & 19 deletions build/t9n.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
@@ -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",
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion smart.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Translations for the meteor account's error messages",
"homepage": "https://github.com/softwarerero/meteor-accounts-t9n",
"author": "Stefan Undorf <el@softwarerero.com>",
"version": "2.4.0",
"version": "2.5.0",
"git": "https://github.com/softwarerero/meteor-accounts-t9n.git",
"packages": {}
}
37 changes: 18 additions & 19 deletions t9n.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Meteor?.startup ->
if Meteor.isClient
console.log('startup4')
Template?.registerHelper 't9n', (x, params) ->
T9n.get(x, true, params.hash)

Expand Down Expand Up @@ -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

0 comments on commit bfddc8e

Please sign in to comment.