-
-
Save HendrikRoth/e51a65deff97ae8e455d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This code uses a regular expression to make the json safer. | |
An potentially safer alternative is https://github.com/joewalnes/filtrex (untested) at the cost of 130Kb. | |
Can be used for example with this JSON structure: | |
{ | |
"_meta_po_header": "nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);", | |
"COLLECTION_CONTENTS[0]": "Kolekcja zawiera %@ przepis.", | |
"COLLECTION_CONTENTS[1]": "Kolekcja zawiera %@ przepisy.", | |
"COLLECTION_CONTENTS[2]": "Kolekcja zawiera %@ przepisów." | |
} | |
*/ | |
var tm; | |
tm = tm || {}; | |
tm.plural = (function () { | |
"use strict"; | |
var RE_RULE = /^nplurals=\d;\s*plural=(.*);$/, // takes the rule | |
RE_STRIP_UNSAFE = /^[n\=\s\?\d\:\%<>\&\(\)\|\!]+$/g; // strips potentially unsafe characters | |
return { | |
// returns a function that takes a count parameter | |
makeRule: function (po_header) { | |
var ruleStrMatch, | |
safeRuleMatch, | |
ruleStr, | |
safeRule; | |
try { | |
ruleStrMatch = po_header.match(RE_RULE); | |
if (ruleStrMatch && ruleStrMatch[1]) { | |
ruleStr = ruleStrMatch[1]; | |
} | |
safeRuleMatch = ruleStr.match(RE_STRIP_UNSAFE); | |
if (safeRuleMatch && safeRuleMatch[0]) { | |
safeRule = safeRuleMatch[0]; | |
} | |
} catch (e) { | |
if (console && console.log) { | |
console.log("PO header contains illegal characters."); | |
} | |
} | |
try { | |
if (ruleStr === undefined) { | |
throw "Could not find PO rule string; perhaps PO header contains illegal characters"; | |
} | |
if (safeRule === undefined) { | |
throw "Could not create safe PO rule string; perhaps PO header contains illegal characters"; | |
} | |
} catch (e) { | |
if (console && console.log) { | |
console.log("PO header contains illegal characters."); | |
} | |
} | |
return function (n) { | |
return eval("+" + safeRule); | |
}; | |
}, | |
makeKey: function (str, num) { | |
return str + "[" + num + "]"; | |
} | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment