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

Commit

Permalink
feat($translateProvider): adds determinePreferredLanguage()
Browse files Browse the repository at this point in the history
gives angular-translate the ability to determine the preferred language
on it's own depending on the browsers locale or a custom function.
  • Loading branch information
0x-r4bbit committed Dec 18, 2013
1 parent 3b1d1c6 commit 7cbfabe
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
$notFoundIndicatorRight,
NESTED_OBJECT_DELIMITER = '.';


// tries to determine the browsers locale
var getLocale = function () {
var nav = window.navigator;
var lang = ((
nav.language ||
nav.browserLanguage ||
nav.systemLanguage ||
nav.userLanguage
) || '').split('-');

if (angular.equals(lang.length, 2)) {

This comment has been minimized.

Copy link
@knalli

knalli Dec 18, 2013

Member

what is the benefit over lang.length ===2?

This comment has been minimized.

Copy link
@0x-r4bbit

0x-r4bbit Dec 18, 2013

Author Member

well, since angular.equals() is there, I use it. Same goes for all cases where we would actually do typeof foo === 'function' or so.

don't like it?

This comment has been minimized.

Copy link
@knalli

knalli Dec 18, 2013

Member

angular.isFunction is okay, but an expression like variable === literal seems to be smarten than a wrapped function call/scope

my 2cts

This comment has been minimized.

Copy link
@0x-r4bbit

0x-r4bbit Dec 18, 2013

Author Member

that's actually true. I'll update the code :) thanks!

This comment has been minimized.

Copy link
@knalli

knalli Dec 18, 2013

Member

equals could do more, in that case it is only an additional call (I hope the a === b check is the first check in that method)

return lang.join('_');
}
return lang;
};

/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#translations
Expand Down Expand Up @@ -491,6 +508,30 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
return this;
};

/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#determinePreferredLanguage
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Tells angular-translate to try to determine on its own which language key
* to set as preferred language. When `fn` is given, angular-translate uses it
* to determine a language key, otherwise it uses the built-in `getLocale()`
* method.
*
* The `getLocale()` returns a language key in the format `[lang]_[country]` or
* `[lang]` depending on what the browser provides.
*
* Use this method at your own risk, since not all browsers return a valid
* locale.
*
* @param {object=} fn Function to determine a browser's locale
*/
this.determinePreferredLanguage = function (fn) {
$preferredLanguage = (fn && angular.isFunction(fn)) ? fn() : getLocale();
return this;
};

/**
* @ngdoc object
* @name pascalprecht.translate.$translate
Expand Down
22 changes: 22 additions & 0 deletions test/unit/service/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1647,4 +1647,26 @@ describe('pascalprecht.translate', function () {

});
});

describe('determineLanguage()', function () {

beforeEach(module('pascalprecht.translate', function ($translateProvider) {
$translateProvider.translations('en_US', {
FOO: 'bar'
});

$translateProvider.translations('de_DE', {
FOO: 'foo'
});
$translateProvider.determinePreferredLanguage();
// mocking
window.navigator.lang = 'en_US';

This comment has been minimized.

Copy link
@knalli

knalli Dec 18, 2013

Member

$window?

This comment has been minimized.

Copy link
@0x-r4bbit

0x-r4bbit Dec 18, 2013

Author Member

Don't have $window at config phase

}));

it('should determine browser language', function () {
inject(function ($translate) {
expect($translate('FOO')).toEqual('bar');
});
});
});
});

0 comments on commit 7cbfabe

Please sign in to comment.