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

Commit

Permalink
feat(service): allow using wildcards in language aliases
Browse files Browse the repository at this point in the history
Fixes #426

This allows usage of wildcards in language aliases defined by `registerAvailableLanguageKeys`. e.g.

```
$translateProvider.registerAvailableLanguageKeys(['en', 'en_GB', 'en_US'], {
	'en_*': 'en'
});
```
  • Loading branch information
Can Yilmaz authored and 0x-r4bbit committed May 31, 2014
1 parent 675e9a2 commit 6f0ae3b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 16 deletions.
10 changes: 8 additions & 2 deletions src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,14 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
if ($languageKeyAliases) {
var alias;
for (var langKeyAlias in $languageKeyAliases) {
if ($languageKeyAliases.hasOwnProperty(langKeyAlias) &&
angular.lowercase(langKeyAlias) === angular.lowercase(preferred)) {
var hasWildcardKey = false;
var hasExactKey = $languageKeyAliases.hasOwnProperty(langKeyAlias) &&
angular.lowercase(langKeyAlias) === angular.lowercase(preferred);

if (langKeyAlias.slice(-1) === '*') {
hasWildcardKey = langKeyAlias.slice(0, -1) === preferred.slice(0, langKeyAlias.length-1);
}
if (hasExactKey || hasWildcardKey) {
alias = $languageKeyAliases[langKeyAlias];
if (avail.indexOf(angular.lowercase(alias)) > -1) {
return alias;
Expand Down
65 changes: 51 additions & 14 deletions test/unit/service/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,13 @@ describe('pascalprecht.translate', function () {
.translations('de_DE', translationMock)
.translations('de_DE', { 'YET_ANOTHER': 'Hallo da!' })
.translations('en', { 'YET_ANOTHER': 'Hello there!' })
.registerAvailableLanguageKeys(['en', 'de_DE'], {
.translations('tr_TR', { 'YET_ANOTHER': 'Selam millet! (tr_TR)' })
.translations('tr', { 'YET_ANOTHER': 'Selam millet! (tr)' })
.registerAvailableLanguageKeys(['en', 'de_DE', 'tr', 'tr_TR'], {
'en_EN': 'en',
'en_US': 'en',
'en_GB': 'en'
'en_GB': 'en',
'tr_*': 'tr'
})
.preferredLanguage('de_DE');
}));
Expand Down Expand Up @@ -315,23 +318,57 @@ describe('pascalprecht.translate', function () {
$rootScope.$digest();
expect(value).toEqual('Hello there!');
});
});

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

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

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!');
});

$translate.use('en_GB');
$translate('YET_ANOTHER').then(function (translation) {
deferred.resolve(translation);
it('should load the language with the exact match first even if a wildcard is used', function () {
var deferred = $q.defer(),
promise = deferred.promise,
value;

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

$translate.use('tr_TR');
$translate('YET_ANOTHER').then(function (translation) {
deferred.resolve(translation);
});
$rootScope.$digest();
expect(value).toEqual('Selam millet! (tr_TR)');
});

it('should load the correct language if a wildcard is used', function () {
var deferred = $q.defer(),
promise = deferred.promise,
value;

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

$translate.use('tr_TURKISH'); // Silly language name
$translate('YET_ANOTHER').then(function (translation) {
deferred.resolve(translation);
});
$rootScope.$digest();
expect(value).toEqual('Selam millet! (tr)');
});
$rootScope.$digest();
expect(value).toEqual('Hello there!');
});

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

0 comments on commit 6f0ae3b

Please sign in to comment.