Skip to content

Commit bfddc8e

Browse files
committed
Use regex for selectors
1 parent 7708d79 commit bfddc8e

File tree

5 files changed

+46
-41
lines changed

5 files changed

+46
-41
lines changed

Diff for: build/t9n.js

+25-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package.describe({
22
summary: "Almost i18n, with standard translations for basic meteor packages.",
3-
version: "2.4.0",
3+
version: "2.5.0",
44
name: "softwarerero:accounts-t9n",
55
git: "https://github.com/softwarerero/meteor-accounts-t9n.git",
66
});

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "meteor-accounts-t9n",
3-
"version": "2.4.0",
3+
"version": "2.5.0",
44
"description": "Translations for Meteor projects, almost i18n",
55
"repository": "https://github.com/softwarerero/meteor-accounts-t9n",
66
"bugs": "https://github.com/softwarerero/meteor-accounts-t9n/issues",

Diff for: smart.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Translations for the meteor account's error messages",
44
"homepage": "https://github.com/softwarerero/meteor-accounts-t9n",
55
"author": "Stefan Undorf <[email protected]>",
6-
"version": "2.4.0",
6+
"version": "2.5.0",
77
"git": "https://github.com/softwarerero/meteor-accounts-t9n.git",
88
"packages": {}
99
}

Diff for: t9n.coffee

+18-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
Meteor?.startup ->
22
if Meteor.isClient
3-
console.log('startup4')
43
Template?.registerHelper 't9n', (x, params) ->
54
T9n.get(x, true, params.hash)
65

@@ -75,36 +74,36 @@ class T9n
7574
throw Error "language #{language} does not exist"
7675
@language = language
7776
@depLanguage?.changed()
78-
79-
77+
8078
@replaceParams = (str, args) ->
8179
index1 = 0
8280
strCopy = str
8381
while index1 > -1
8482
index1 = strCopy.indexOf('@{')
8583
if index1 > -1
86-
index2 = strCopy.substring(index1).indexOf('}')
84+
index2 = strCopy.substring(index1).indexOf('}') # no nested tokens
8785
token = strCopy.substring(index1, index2 + 1)
88-
if token.indexOf('->') > -1
89-
lines = strCopy.split(/\n/)
90-
tokenName = token.substring(2, token.indexOf(' '))
91-
arg = args[tokenName]
92-
line = lines.find((l) -> l.indexOf("[#{arg}]") > -1)
93-
if line
94-
value = line.substring(line.lastIndexOf(']') + 1).trim()
95-
else
96-
line = lines.find((l) -> l.indexOf('[*]') > -1)
97-
value =
98-
line.substring(line.lastIndexOf(']') + 1)
99-
.trim()
100-
.replace(new RegExp("\\$#{tokenName}"), args[tokenName])
86+
if token.indexOf('->') > -1
87+
value = @handleSelector(strCopy, args, token)
10188
str = str.replace(token, value)
102-
else
89+
else # no selector, simply replace the token
10390
tokenName = token.substring(2, token.indexOf('}'))
104-
str = str.replace(token, args[tokenName])
91+
str = str.replace(new RegExp(token, 'g'), args[tokenName])
92+
str = str.replace(new RegExp("\\$#{tokenName}", 'g'), args[tokenName])
10593
strCopy = strCopy.substring(index2+2).trim()
10694
return str
10795

96+
@handleSelector = (str, args, token) ->
97+
tokenName = token.substring(2, token.indexOf(' '))
98+
lineMap = str.split(/\n/).slice(1).map (line) ->
99+
regexString = line.trim().split(/\s/)[0]
100+
{regexString, line: line.substring(line.indexOf(regexString) + regexString.length)}
101+
foundLine = lineMap.find (map) -> new RegExp(map.regexString).test(args[tokenName])
102+
if foundLine
103+
foundLine.line.substring(foundLine.line.lastIndexOf(']') + 1)
104+
.trim()
105+
.replace(new RegExp("\\$#{tokenName}"), args[tokenName])
106+
108107
@T9n = T9n
109108
@t9n = (x, includePrefix, params) -> T9n.get(x)
110109
module?.exports.T9n = T9n

0 commit comments

Comments
 (0)