Changeset 7502

Show
Ignore:
Timestamp:
03/01/07 11:53:01 (21 months ago)
Author:
peller
Message:

Fixes #2350. Mike - this is a compromise. I added compound paths and whacked the polymorphic/varargs thing. ${} in favor of %{}, but formatting would have to be a separate pass prior to calling this method. Sound reasonable?

Location:
trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/string/extras.js

    r6927 r7502  
    55dojo.require("dojo.lang.array"); 
    66 
    7 //TODO: should we use ${} substitution syntax instead, like widgets do? 
    8 dojo.string.substituteParams = function(/*string*/template, /* object - optional or ... */hash){ 
     7dojo.string.substitute = function(/*String*/template, /*Object or Array*/map, /*Object?*/thisObject){ 
    98// summary: 
    109//      Performs parameterized substitutions on a string. Throws an exception if any parameter is unmatched. 
     
    1211// description: 
    1312//      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 
    1616//              "File 'foo.html' is not found in directory '/temp'." 
    1717// 
    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; 
    2928        }); // string 
    3029}; 
  • trunk/tests/i18n/nls/es/salutations.js

    r4734 r7502  
    22 es: "Español", 
    33 hello: "Hola", 
    4  hello_dojo: "¡%{hello}, %{dojo}!" 
     4 hello_dojo: "¡${hello}, ${dojo}!" 
    55} 
  • trunk/tests/i18n/nls/salutations.js

    r4930 r7502  
    2828 hello: "Hello", 
    2929 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." 
    3232}