A) Because Drupal.t() loops through the placeholders in the order they are defined, placeholders that start with identical strings, for example "!price" and "!price_formatted", can cause problems.
Example
var params = {
"!foo": "foo",
"!bar": "bar",
"!foobar": "banana"
}
alert(Drupal.t('!foobar', params));
the expected output is 'banana', the actual output is 'foobar' (the value of !foo + the remaining literal 'bar').
Solution
One solution could be sorting the args by descending key first, making the longer strings occur before their shorter versions so they're replaced first.
B) By default, javascript's string.replace only replaces the first occurrence of a string.
Example
var params = {
"!foo": "foo",
"!bar": "bar",
"!foobar": "banana"
}
alert(Drupal.t('!foobar !foobar !foobar', params));
Expected result: "banana banana banana"
Actual result: "foobar banana !foobar"
edit: changed an error in the second example.
Comment | File | Size | Author |
---|---|---|---|
#6 | 1230986-6-drupal-t-placeholders.patch | 1.02 KB | mr.baileys |
#5 | 1230986-drupal-t-placeholders-3.patch | 994 bytes | mr.baileys |
#4 | 1230986-drupal-t-placeholders-2.patch | 2.03 KB | mr.baileys |
#3 | 1230986-drupal-t-placeholders.patch | 1.74 KB | mr.baileys |
Comments
Comment #1
plachUI translation issues belong to the Locale queue. I guess this needs backport to D7.
Anyway, the proposed solution looks sensible to me at a first sight, and it's consistent with what t() does: it uses strtr() which gives precedence to the longest keys.
Comment #2
mr.baileysUpdated the original bug report with an additional Drupal.t() string replacement issue.
Comment #3
mr.baileysAttached patch addresses the first issue.
Comment #4
mr.baileysAnd here's an attempt to address both issues.
Comment #5
mr.baileysAnd once more, this time without unrelated cruft in the patch. Sorry.
Comment #5.0
mr.baileysAdded a second, related, string-replacement issue.
Comment #5.1
mr.baileysUpdated issue summary.
Comment #6
mr.baileysRe-rolled.
Comment #7
dags CreditAttribution: dags commented#6: 1230986-6-drupal-t-placeholders.patch queued for re-testing.
Comment #8
nod_tag
Can you please add a test scenario to this page http://drupal.org/node/1777342 so that we can make sure we test it and possibly add tests to testswarm to make sure we fix it and keep it fixed? :)
Comment #9
mr.baileys@_nod: the only way I can think of to test this is to paste the examples in the original post in a console and run them. Is it ok to add those instructions to http://drupal.org/node/1777342? Those tests seem to be more of the click-and-verify-variety :)
Comment #10
nod_Yep, you can add them, they are the easy kind of tests to port to testswarm, they won't be in there for long :)
Comment #11
mr.baileysCool, added a test-case at the bottom http://drupal.org/node/1777342
Comment #12
droplet CreditAttribution: droplet commentedStill doesn't looks correctly.
PHP:
// string(14) "!foo !foo !foo"
JS:
// 1 1 1
see:
http://phpjs.org/functions/strtr:556
http://git.php.net/?p=php-src.git;a=blob;f=ext/standard/string.c#l2777
Comment #12.0
droplet CreditAttribution: droplet commentedUpdated issue summary.
Comment #13
droplet CreditAttribution: droplet commented