Skip to content

Commit

Permalink
Merge pull request #75 from alex-pex/master
Browse files Browse the repository at this point in the history
Fixed Translator when multiple locale are loaded with dumped JS
  • Loading branch information
willdurand committed Dec 12, 2013
2 parents 52a58fb + fe72601 commit c298a73
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
26 changes: 25 additions & 1 deletion Resources/js/translatorTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ test('guesser', function() {
equal(Translator.get('boo.baz'), 'boo.baz', 'Returns the key as the key cannot be guessed');
});


test('fromJson', function () {
expect(6);

Expand All @@ -120,3 +119,28 @@ test('fromJson', function () {
deepEqual(Translator.defaultDomains, ['more_messages'], 'JSON parser processes defaultDomains from valid object literal');
equal(Translator.get('more_messages:moo'), 'mar', 'JSON parser processes messages from valid object literal');
});

test('multiple locales', function() {
expect(5);

// Simulate i18n/messages/en.js loading
Translator.locale = 'en';
Translator.defaultDomains = ["messages"];
Translator.add("messages:symfony2.great", "I like Symfony2");
Translator.add("messages:symfony2.powerful", "Symfony2 is powerful");

// Simulate i18n/messages/fr.js loading
Translator.locale = 'fr';
Translator.defaultDomains = ["messages"];
Translator.add("messages:symfony2.great", "J'aime Symfony2");

// Test with locale = fr
equal(Translator.get('messages:symfony2.great'), "J'aime Symfony2", 'Return translation based on current locale');
ok(!Translator.has('messages:symfony2.powerful'), 'Translation set for another locale is not available for current locale');

// Test with locale = en
Translator.locale = 'en';
equal(Translator.get('messages:symfony2.great'), "I like Symfony2", 'Return translation based on previous locale');
ok(Translator.has('messages:symfony2.powerful'), 'Translation set for previous locale is still available');
equal(Translator.get('messages:symfony2.powerful'), "Symfony2 is powerful", 'Return translation based on previous locale');
});
12 changes: 9 additions & 3 deletions Resources/public/js/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,11 @@ var Translator = (function () {
* @api public
*/
add: function(key, message) {
_messages[key] = message;
var _locale = Translator.locale || Translator.fallback;
if (!_messages[_locale]) {
_messages[_locale] = {};
}
_messages[_locale][key] = message;
return this;
},

Expand All @@ -380,7 +384,8 @@ var Translator = (function () {
* @return {String} The corresponding message if the key exists otherwise the key will be returned.
*/
get: function(key, placeholders, number) {
var _message = _messages[key],
var _locale = Translator.locale || Translator.fallback;
var _message = _messages[_locale][key],
_number = parseInt(number, 10),
_placeholders = placeholders || {};

Expand Down Expand Up @@ -409,7 +414,8 @@ var Translator = (function () {
* @api public
*/
has: function(key) {
return (_messages[key] ? true : false);
var _locale = Translator.locale || Translator.fallback;
return (_messages[_locale] && _messages[_locale][key]) ? true : false;
},

/**
Expand Down
2 changes: 1 addition & 1 deletion Resources/public/js/translator.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c298a73

Please sign in to comment.