Changeset 7502
- Timestamp:
- 03/01/07 11:53:01 (21 months ago)
- Location:
- trunk
- Files:
-
- 3 modified
-
src/string/extras.js (modified) (2 diffs)
-
tests/i18n/nls/es/salutations.js (modified) (1 diff)
-
tests/i18n/nls/salutations.js (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/string/extras.js
r6927 r7502 5 5 dojo.require("dojo.lang.array"); 6 6 7 //TODO: should we use ${} substitution syntax instead, like widgets do? 8 dojo.string.substituteParams = function(/*string*/template, /* object - optional or ... */hash){ 7 dojo.string.substitute = function(/*String*/template, /*Object or Array*/map, /*Object?*/thisObject){ 9 8 // summary: 10 9 // Performs parameterized substitutions on a string. Throws an exception if any parameter is unmatched. … … 12 11 // description: 13 12 // For example, 14 // dojo.string.substituteParams("File '%{0}' is not found in directory '%{1}'.","foo.html","/temp"); 15 // returns 13 // dojo.string.substituteParams("File '${0}' is not found in directory '${1}'.",["foo.html","/temp"]); 14 // dojo.string.substituteParams("File '${name}' is not found in directory '${info.dir}'.",{name: "foo.html", info: {dir: "/temp"}}); 15 // both return 16 16 // "File 'foo.html' is not found in directory '/temp'." 17 17 // 18 // template: the original string template with %{values} to be replaced 19 // hash: name/value pairs (type object) to provide substitutions. Alternatively, substitutions may be 20 // included as arguments 1..n to this function, corresponding to template parameters 0..n-1 21 22 var map = (typeof hash == 'object') ? hash : dojo.lang.toArray(arguments, 1); 23 24 return template.replace(/\%\{(\w+)\}/g, function(match, key){ 25 if(typeof(map[key]) != "undefined" && map[key] != null){ 26 return map[key]; 27 } 28 dojo.raise("Substitution not found: " + key); 18 // template: a string with expressions in the form ${key} to be replaced or ${key:format} which specifies a format function. NOTE syntax has changed from %{key} 19 // map: where to look for substitutions 20 // thisObject: where to look for optional format function 21 22 return template.replace(/\$\{([^\s\:]+)(?:\:(\S+))?\}/g, function(match, key, format){ 23 var value = dojo.getObject(key,false,map); 24 if(typeof(value) == "undefined"){ 25 dojo.raise("Missing key: " + key); 26 } 27 return format ? dojo.getObject(format,false,thisObject)(value) : value; 29 28 }); // string 30 29 }; -
trunk/tests/i18n/nls/es/salutations.js
r4734 r7502 2 2 es: "Español", 3 3 hello: "Hola", 4 hello_dojo: "¡ %{hello}, %{dojo}!"4 hello_dojo: "¡${hello}, ${dojo}!" 5 5 } -
trunk/tests/i18n/nls/salutations.js
r4930 r7502 28 28 hello: "Hello", 29 29 dojo: "Dojo", 30 hello_dojo: " %{hello}, %{dojo}!",31 file_not_found:"The file you requested, %{0}, is not found."30 hello_dojo: "${hello}, ${dojo}!", 31 file_not_found:"The file you requested, ${0}, is not found." 32 32 }