granula
Version:
i18n tool for angular.js applications
179 lines (170 loc) • 5.63 kB
JavaScript
(function() {
var argument, argumentParser, justText, noParser, pluralPartsToString, pluralizerParser;
justText = function(text) {
return {
apply: function(context) {
var _ref;
return (_ref = context.interpolate("string", text)) != null ? _ref : text;
}
};
};
argument = function(argName) {
return {
argName: argName,
apply: function(context) {
return context.interpolate('argument', {
argName: argName
});
}
};
};
argumentParser = function(startSymbol, endSymbol) {
if (startSymbol == null) {
startSymbol = "{{";
}
if (endSymbol == null) {
endSymbol = "}}";
}
return {
nextPosition: function(str, from) {
return str.indexOf(startSymbol, from);
},
process: function(str, from, context) {
var argName, endPos;
endPos = str.indexOf(endSymbol, from);
if (endPos === -1) {
throw new Error("Syntax error: uncompleted argument definition started at char " + from + ": " + str + " ");
}
argName = str.substring(from + startSymbol.length, endPos);
return {
part: argument(argName),
currentPos: endPos + endSymbol.length
};
}
};
};
noParser = function() {
return {
nextPosition: function() {
return -1;
}
};
};
pluralPartsToString = function(word, suffixes) {
return "" + word + "(" + (suffixes.join(',')) + ")";
};
pluralizerParser = function(preparePluralizationFn) {
var endSymbol, escape, exactVarSpec, nearestRight, plural, separator, startSymbol, varEnd, wordSeparator;
if (!preparePluralizationFn) {
return noParser();
}
startSymbol = "(";
endSymbol = ")";
escape = "\\";
separator = ",";
wordSeparator = /[\s,.!:;'\"-+=*%$#@{}()]/;
varEnd = /[\s,!:'\"+=*%$#@{}()-]/;
exactVarSpec = ":";
nearestRight = [">", ">"];
plural = function(word, suffixes, argName) {
var fn;
fn = preparePluralizationFn(word, suffixes);
return {
argument: typeof argName === "string" ? argument(argName) : null,
apply: function(context) {
return context.interpolate("pluralExpression", {
word: word,
suffixes: suffixes,
fn: fn
}, this.argument);
},
link: function(context, myIdx) {
var dir, i, searchIn, _i, _j, _k, _len, _ref, _results, _results1;
if (this.argument !== null) {
return;
}
if (argName.prev === true) {
searchIn = (function() {
_results = [];
for (var _i = myIdx; myIdx <= 0 ? _i <= 0 : _i >= 0; myIdx <= 0 ? _i++ : _i--){ _results.push(_i); }
return _results;
}).apply(this);
dir = "left";
} else if (argName.next === true) {
searchIn = (function() {
_results1 = [];
for (var _j = myIdx, _ref = context.parts.length - 1; myIdx <= _ref ? _j <= _ref : _j >= _ref; myIdx <= _ref ? _j++ : _j--){ _results1.push(_j); }
return _results1;
}).apply(this);
dir = "right";
} else {
throw new Error("invalid link " + argName + " - expected {prev:true} or {next:true}");
}
for (_k = 0, _len = searchIn.length; _k < _len; _k++) {
i = searchIn[_k];
if (context.parts[i].argName) {
this.argument = context.parts[i];
break;
}
}
if (this.argument === null) {
throw new Error("There is no argument nearest to the " + dir + " for plural expression '" + (pluralPartsToString(word, suffixes)) + ")'");
}
}
};
};
return {
nextPosition: function(str, from) {
var pos, _ref;
pos = str.indexOf(startSymbol, from);
while (pos >= from && !((_ref = str[pos - 1]) != null ? _ref : ' ').match(wordSeparator)) {
pos--;
}
return pos;
},
process: function(str, from, context) {
var argLink, cPos, end, exactVar, parts, pluralExpression, startVar;
end = str.indexOf(endSymbol, from);
pluralExpression = str.substring(from, end);
parts = pluralExpression.split(startSymbol);
if (parts[0].length > 0 && parts[0].slice(-1) === escape) {
return {
part: justText("" + (parts[0].slice(0, -1)) + startSymbol + parts[1] + endSymbol),
currentPos: end + endSymbol.length
};
} else {
argLink = {
prev: true
};
cPos = end + endSymbol.length;
if (str[end + endSymbol.length] === exactVarSpec) {
startVar = end + endSymbol.length + 1;
end = startVar;
while (end < str.length && !str[end].match(varEnd)) {
end++;
}
exactVar = str.substring(startVar, end);
if (nearestRight.indexOf(exactVar) > -1) {
argLink = {
next: true
};
cPos = end;
} else if (exactVar.length > 0) {
argLink = exactVar;
cPos = end;
}
}
return {
part: plural(parts[0], parts[1].split(separator), argLink),
currentPos: cPos
};
}
}
};
};
module.exports = {
argumentParser: argumentParser,
pluralizerParser: pluralizerParser,
justText: justText
};
}).call(this);