i18next.js
1 year ago
i18next.min.js
1 year ago
i18nextHttpBackend.js
1 year ago
i18nextHttpBackend.min.js
1 year ago
i18nextSprintfPostProcessor.js
1 year ago
i18nextSprintfPostProcessor.min.js
1 year ago
ng-i18next.js
1 year ago
ng-i18next.min.js
1 year ago
i18nextSprintfPostProcessor.js
241 lines
| 1 | (function (global, factory) { |
| 2 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : |
| 3 | typeof define === 'function' && define.amd ? define('i18nextSprintfPostProcessor', factory) : |
| 4 | (global.i18nextSprintfPostProcessor = factory()); |
| 5 | }(this, function () { 'use strict'; |
| 6 | |
| 7 | var babelHelpers = {}; |
| 8 | babelHelpers.typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { |
| 9 | return typeof obj; |
| 10 | } : function (obj) { |
| 11 | return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; |
| 12 | }; |
| 13 | babelHelpers; |
| 14 | |
| 15 | var re = { |
| 16 | not_string: /[^s]/, |
| 17 | number: /[diefg]/, |
| 18 | json: /[j]/, |
| 19 | not_json: /[^j]/, |
| 20 | text: /^[^\x25]+/, |
| 21 | modulo: /^\x25{2}/, |
| 22 | placeholder: /^\x25(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-gijosuxX])/, |
| 23 | key: /^([a-z_][a-z_\d]*)/i, |
| 24 | key_access: /^\.([a-z_][a-z_\d]*)/i, |
| 25 | index_access: /^\[(\d+)\]/, |
| 26 | sign: /^[\+\-]/ |
| 27 | }; |
| 28 | |
| 29 | function sprintf() { |
| 30 | var key = arguments[0], |
| 31 | cache = sprintf.cache; |
| 32 | if (!(cache[key] && cache.hasOwnProperty(key))) { |
| 33 | cache[key] = sprintf.parse(key); |
| 34 | } |
| 35 | return sprintf.format.call(null, cache[key], arguments); |
| 36 | } |
| 37 | |
| 38 | sprintf.format = function (parse_tree, argv) { |
| 39 | var cursor = 1, |
| 40 | tree_length = parse_tree.length, |
| 41 | node_type = "", |
| 42 | arg, |
| 43 | output = [], |
| 44 | i, |
| 45 | k, |
| 46 | match, |
| 47 | pad, |
| 48 | pad_character, |
| 49 | pad_length, |
| 50 | is_positive = true, |
| 51 | sign = ""; |
| 52 | for (i = 0; i < tree_length; i++) { |
| 53 | node_type = get_type(parse_tree[i]); |
| 54 | if (node_type === "string") { |
| 55 | output[output.length] = parse_tree[i]; |
| 56 | } else if (node_type === "array") { |
| 57 | match = parse_tree[i]; // convenience purposes only |
| 58 | if (match[2]) { |
| 59 | // keyword argument |
| 60 | arg = argv[cursor]; |
| 61 | for (k = 0; k < match[2].length; k++) { |
| 62 | if (!arg.hasOwnProperty(match[2][k])) { |
| 63 | throw new Error(sprintf("[sprintf] property '%s' does not exist", match[2][k])); |
| 64 | } |
| 65 | arg = arg[match[2][k]]; |
| 66 | } |
| 67 | } else if (match[1]) { |
| 68 | // positional argument (explicit) |
| 69 | arg = argv[match[1]]; |
| 70 | } else { |
| 71 | // positional argument (implicit) |
| 72 | arg = argv[cursor++]; |
| 73 | } |
| 74 | |
| 75 | if (get_type(arg) == "function") { |
| 76 | arg = arg(); |
| 77 | } |
| 78 | |
| 79 | if (re.not_string.test(match[8]) && re.not_json.test(match[8]) && get_type(arg) != "number" && isNaN(arg)) { |
| 80 | throw new TypeError(sprintf("[sprintf] expecting number but found %s", get_type(arg))); |
| 81 | } |
| 82 | |
| 83 | if (re.number.test(match[8])) { |
| 84 | is_positive = arg >= 0; |
| 85 | } |
| 86 | |
| 87 | switch (match[8]) { |
| 88 | case "b": |
| 89 | arg = arg.toString(2); |
| 90 | break; |
| 91 | case "c": |
| 92 | arg = String.fromCharCode(arg); |
| 93 | break; |
| 94 | case "d": |
| 95 | case "i": |
| 96 | arg = parseInt(arg, 10); |
| 97 | break; |
| 98 | case "j": |
| 99 | arg = JSON.stringify(arg, null, match[6] ? parseInt(match[6]) : 0); |
| 100 | break; |
| 101 | case "e": |
| 102 | arg = match[7] ? arg.toExponential(match[7]) : arg.toExponential(); |
| 103 | break; |
| 104 | case "f": |
| 105 | arg = match[7] ? parseFloat(arg).toFixed(match[7]) : parseFloat(arg); |
| 106 | break; |
| 107 | case "g": |
| 108 | arg = match[7] ? parseFloat(arg).toPrecision(match[7]) : parseFloat(arg); |
| 109 | break; |
| 110 | case "o": |
| 111 | arg = arg.toString(8); |
| 112 | break; |
| 113 | case "s": |
| 114 | arg = (arg = String(arg)) && match[7] ? arg.substring(0, match[7]) : arg; |
| 115 | break; |
| 116 | case "u": |
| 117 | arg = arg >>> 0; |
| 118 | break; |
| 119 | case "x": |
| 120 | arg = arg.toString(16); |
| 121 | break; |
| 122 | case "X": |
| 123 | arg = arg.toString(16).toUpperCase(); |
| 124 | break; |
| 125 | } |
| 126 | if (re.json.test(match[8])) { |
| 127 | output[output.length] = arg; |
| 128 | } else { |
| 129 | if (re.number.test(match[8]) && (!is_positive || match[3])) { |
| 130 | sign = is_positive ? "+" : "-"; |
| 131 | arg = arg.toString().replace(re.sign, ""); |
| 132 | } else { |
| 133 | sign = ""; |
| 134 | } |
| 135 | pad_character = match[4] ? match[4] === "0" ? "0" : match[4].charAt(1) : " "; |
| 136 | pad_length = match[6] - (sign + arg).length; |
| 137 | pad = match[6] ? pad_length > 0 ? str_repeat(pad_character, pad_length) : "" : ""; |
| 138 | output[output.length] = match[5] ? sign + arg + pad : pad_character === "0" ? sign + pad + arg : pad + sign + arg; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | return output.join(""); |
| 143 | }; |
| 144 | |
| 145 | sprintf.cache = {}; |
| 146 | |
| 147 | sprintf.parse = function (fmt) { |
| 148 | var _fmt = fmt, |
| 149 | match = [], |
| 150 | parse_tree = [], |
| 151 | arg_names = 0; |
| 152 | while (_fmt) { |
| 153 | if ((match = re.text.exec(_fmt)) !== null) { |
| 154 | parse_tree[parse_tree.length] = match[0]; |
| 155 | } else if ((match = re.modulo.exec(_fmt)) !== null) { |
| 156 | parse_tree[parse_tree.length] = "%"; |
| 157 | } else if ((match = re.placeholder.exec(_fmt)) !== null) { |
| 158 | if (match[2]) { |
| 159 | arg_names |= 1; |
| 160 | var field_list = [], |
| 161 | replacement_field = match[2], |
| 162 | field_match = []; |
| 163 | if ((field_match = re.key.exec(replacement_field)) !== null) { |
| 164 | field_list[field_list.length] = field_match[1]; |
| 165 | while ((replacement_field = replacement_field.substring(field_match[0].length)) !== "") { |
| 166 | if ((field_match = re.key_access.exec(replacement_field)) !== null) { |
| 167 | field_list[field_list.length] = field_match[1]; |
| 168 | } else if ((field_match = re.index_access.exec(replacement_field)) !== null) { |
| 169 | field_list[field_list.length] = field_match[1]; |
| 170 | } else { |
| 171 | throw new SyntaxError("[sprintf] failed to parse named argument key"); |
| 172 | } |
| 173 | } |
| 174 | } else { |
| 175 | throw new SyntaxError("[sprintf] failed to parse named argument key"); |
| 176 | } |
| 177 | match[2] = field_list; |
| 178 | } else { |
| 179 | arg_names |= 2; |
| 180 | } |
| 181 | if (arg_names === 3) { |
| 182 | throw new Error("[sprintf] mixing positional and named placeholders is not (yet) supported"); |
| 183 | } |
| 184 | parse_tree[parse_tree.length] = match; |
| 185 | } else { |
| 186 | throw new SyntaxError("[sprintf] unexpected placeholder"); |
| 187 | } |
| 188 | _fmt = _fmt.substring(match[0].length); |
| 189 | } |
| 190 | return parse_tree; |
| 191 | }; |
| 192 | |
| 193 | function vsprintf(fmt, argv, _argv) { |
| 194 | _argv = (argv || []).slice(0); |
| 195 | _argv.splice(0, 0, fmt); |
| 196 | return sprintf.apply(null, _argv); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * helpers |
| 201 | */ |
| 202 | function get_type(variable) { |
| 203 | return Object.prototype.toString.call(variable).slice(8, -1).toLowerCase(); |
| 204 | } |
| 205 | |
| 206 | function str_repeat(input, multiplier) { |
| 207 | return Array(multiplier + 1).join(input); |
| 208 | } |
| 209 | |
| 210 | var index = { |
| 211 | name: 'sprintf', |
| 212 | type: 'postProcessor', |
| 213 | |
| 214 | process: function process(value, key, options) { |
| 215 | if (!options.sprintf) return value; |
| 216 | |
| 217 | if (Object.prototype.toString.apply(options.sprintf) === '[object Array]') { |
| 218 | return vsprintf(value, options.sprintf); |
| 219 | } else if (babelHelpers.typeof(options.sprintf) === 'object') { |
| 220 | return sprintf(value, options.sprintf); |
| 221 | } |
| 222 | |
| 223 | return value; |
| 224 | }, |
| 225 | overloadTranslationOptionHandler: function overloadTranslationOptionHandler(args) { |
| 226 | var values = []; |
| 227 | |
| 228 | for (var i = 1; i < args.length; i++) { |
| 229 | values.push(args[i]); |
| 230 | } |
| 231 | |
| 232 | return { |
| 233 | postProcess: 'sprintf', |
| 234 | sprintf: values |
| 235 | }; |
| 236 | } |
| 237 | }; |
| 238 | |
| 239 | return index; |
| 240 | |
| 241 | })); |