Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,12 @@ var message = "Welcome, $1";
$.i18n(message, 'Alice'); // This gives "Welcome, Alice"
```


To escape the $ sign, use double blackslash in json file. Example:
```javascript
{
"messageKey": "Price of {{PLURAL:$1|an item|$1 items}} is $2, you get \\$2 discount."
}
```
## Plurals

To make the syntax of sentence correct, plural forms are required. jquery.i18n support plural forms in the message using the syntax `{{PLURAL:$1|pluralform1|pluralform2|...}}`
Expand Down
16 changes: 11 additions & 5 deletions src/jquery.i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
source = 'i18n/' + $.i18n().locale + '.json';
locale = $.i18n().locale;
}
if ( typeof source === 'string' &&
if ( typeof source === 'string' &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What changed here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strange. dont know.

source.split( '.' ).pop() !== 'json'
) {
// Load specified locale then check for fallbacks when directory is specified in load()
Expand Down Expand Up @@ -260,16 +260,22 @@
$.i18n.parser = {
// The default parser only handles variable substitution
parse: function ( message, parameters ) {
return message.replace( /\$(\d+)/g, function ( str, match ) {
var index = parseInt( match, 10 ) - 1;
return parameters[ index ] !== undefined ? parameters[ index ] : '$' + match;
return message.replace( /(\\?\$\d+)/g, function ( str, match ) {
var index;

if ( match[ 0 ] === '\\' ) {
// Escaped.
return match.slice( 1 );
}
index = parseInt( match.slice( 1 ), 10 ) - 1;
return parameters[ index ] !== undefined ? parameters[ index ] : match;
} );
},
emitter: {}
};
$.i18n.fallbacks = {};
$.i18n.debug = false;
$.i18n.log = function ( /* arguments */ ) {
$.i18n.log = function () {
if ( window.console && $.i18n.debug ) {
window.console.log.apply( window.console, arguments );
}
Expand Down
11 changes: 8 additions & 3 deletions src/jquery.i18n.parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@
constructor: MessageParser,

simpleParse: function ( message, parameters ) {
return message.replace( /\$(\d+)/g, function ( str, match ) {
var index = parseInt( match, 10 ) - 1;
return message.replace( /(\\?\$\d+)/g, function ( str, match ) {
var index;

return parameters[ index ] !== undefined ? parameters[ index ] : '$' + match;
if ( match[ 0 ] === '\\' ) {
// Escaped.
return match.slice( 1 );
}
index = parseInt( match.slice( 1 ), 10 ) - 1;
return parameters[ index ] !== undefined ? parameters[ index ] : match;
} );
},

Expand Down
3 changes: 2 additions & 1 deletion test/i18n/test-en.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"message_1": "ONE",
"message_2": "TWO",
"message_3": "THREE"
"message_3": "THREE",
"message_4": "Total \\$1 of \\$1500"
}
7 changes: 6 additions & 1 deletion test/jquery.i18n.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
}
} );

QUnit.test( 'Message parse tests (en)', 14, function ( assert ) {
QUnit.test( 'Message parse tests (en)', 16, function ( assert ) {
var pluralAndGenderMessage,
pluralAndGenderMessageWithLessParaMS,
pluralAndGenderMessageWithCase,
Expand All @@ -49,13 +49,18 @@
QUnit.start();
assert.strictEqual( i18n.locale, 'en', 'Locale is English' );
assert.strictEqual( $.i18n( 'message_1' ), 'ONE', 'Simple message' );
assert.strictEqual( $.i18n( 'message_4', 5 ), 'Total $1 of $1500', 'Parameter replacement escaped' );
} );
assert.strictEqual(
$.i18n( 'This message key does not exist' ),
'This message key does not exist',
'This message key does not exist'
);
assert.strictEqual( $.i18n( 'Hello $1', 'Bob' ), 'Hello Bob', 'Parameter replacement' );
assert.strictEqual(
$.i18n( 'Price of {{PLURAL:$1|an item|$1 items}} is $2, you get \\$2 discount', 5, 2000 ),
'Price of 5 items is 2000, you get $2 discount', 'Parameter replacement with dollar sign escaped, not parsed using simplePaser'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The third param should be on its own line.

);
pluralAndGenderMessage = '$1 has $2 {{plural:$2|kitten|kittens}}. ' +
'{{gender:$3|He|She}} loves to play with {{plural:$2|it|them}}.';
pluralAndGenderMessageWithLessParaMS = '$1 has $2 {{plural:$2|kitten}}. ' +
Expand Down