Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
fix(service): use the aliased language key if available
Browse files Browse the repository at this point in the history
Fixes #530
  • Loading branch information
Can Yilmaz authored and 0x-r4bbit committed May 31, 2014
1 parent 3f9da45 commit 675e9a2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
21 changes: 21 additions & 0 deletions demo/l10n/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"load" : {
"static" : {
"TITLE" : "How to load static files?",
"HEADER" : "The module can lazzy load a translations table when it's needed.",
"SUBHEADER" : "So, you can load only those languages, which will be used.",
"ONLY_EN": "only english",
"EN_RU": "EN and RU - EN Edition"
},
"predefined" : {
"TITLE" : "How to use lazzy loading without autoupload?",
"HEADER" : "You can predefine a default language while still using a lazzy loading.",
"SUBHEADER" : "This way module will not load a default language from server once more."
},
"dynamic" : {
"TITLE" : "How to load dynamic files?",
"HEADER" : "The module can lazzy load a translations table from the dynamic page.",
"SUBHEADER" : "It will use the \"lang\" request param passed into GET."
}
}
}
11 changes: 10 additions & 1 deletion src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
}

if (avail.indexOf(locale) > -1) {
return locale;
return preferred;
}

if ($languageKeyAliases) {
Expand All @@ -77,6 +77,9 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
if (parts.length > 1 && avail.indexOf(angular.lowercase(parts[0])) > -1) {
return parts[0];
}

// If everything fails, just return the preferred, unchanged.
return preferred;

This comment has been minimized.

Copy link
@raulgomis

raulgomis Jul 16, 2014

If should be better to return null or undefined in order to know when to fallback to the default language.

};

/**
Expand Down Expand Up @@ -1343,6 +1346,12 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',

$rootScope.$emit('$translateChangeStart');

// Try to get the aliased language key
var aliasedKey = negotiateLocale(key);
if (aliasedKey) {
key = aliasedKey;
}

// if there isn't a translation table for the language we've requested,
// we load it asynchronously
if (!$translationTable[key] && $loaderFactory) {
Expand Down
24 changes: 23 additions & 1 deletion test/unit/service/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,12 @@ describe('pascalprecht.translate', function () {
$translateProvider
.translations('de_DE', translationMock)
.translations('de_DE', { 'YET_ANOTHER': 'Hallo da!' })
.translations('en_EN', { 'YET_ANOTHER': 'Hello there!' })
.translations('en', { 'YET_ANOTHER': 'Hello there!' })
.registerAvailableLanguageKeys(['en', 'de_DE'], {
'en_EN': 'en',
'en_US': 'en',
'en_GB': 'en'
})
.preferredLanguage('de_DE');
}));

Expand Down Expand Up @@ -312,6 +317,23 @@ describe('pascalprecht.translate', function () {
});
});

it('should respect the language aliases', function () {
var deferred = $q.defer(),
promise = deferred.promise,
value;

promise.then(function (translation) {
value = translation;
});

$translate.use('en_GB');
$translate('YET_ANOTHER').then(function (translation) {
deferred.resolve(translation);
});
$rootScope.$digest();
expect(value).toEqual('Hello there!');
});

describe('$translate#use() with async loading', function () {

var fastButRequestedSecond = 'en_US',
Expand Down

0 comments on commit 675e9a2

Please sign in to comment.