Created
October 25, 2015 05:29
-
-
Save roidayan/e35b226e1c67047acb5d to your computer and use it in GitHub Desktop.
Angular language service to detection locale in phonegap and browser.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function(){ | |
| 'use strict'; | |
| angular | |
| .module('app') | |
| .service('langService', [ | |
| langService | |
| ]); | |
| function langService() { | |
| var detectLanguage = function(successCB, errorCB) { | |
| //Phonegap browser detection | |
| if (navigator.globalization !== null && navigator.globalization !== undefined) { | |
| navigator.globalization.getPreferredLanguage( | |
| function (language) { if (successCB) { successCB(normalize(language)); } }, | |
| function (error) { if (errorCB) { errorCB(error); } } | |
| ); | |
| //Normal browser detection | |
| } else { | |
| if(window.navigator.language !== null && window.navigator.language !== undefined) { | |
| if (successCB) { successCB( normalize( window.navigator.language ) ); } | |
| } | |
| } | |
| }; | |
| var normalize = function(language) { | |
| if (typeof(language) !== "string") | |
| return ''; | |
| return language.toLowerCase().substr(0,2); | |
| }; | |
| return { | |
| detectLanguage: detectLanguage, | |
| normalize: normalize | |
| }; | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment