mirror of
https://github.com/pcottle/learnGitBranching.git
synced 2025-06-28 08:50:06 +02:00
[INTL] Switch to browsers locale based on headers Resolves #175
This commit is contained in:
parent
d259a2db51
commit
40d60c4bcc
2 changed files with 53 additions and 0 deletions
|
@ -198,6 +198,8 @@ var initDemo = function(sandbox) {
|
|||
if (params.locale !== undefined && params.locale.length) {
|
||||
GlobalState.locale = params.locale;
|
||||
events.trigger('localeChanged');
|
||||
} else {
|
||||
tryLocaleDetect();
|
||||
}
|
||||
|
||||
if (params.command) {
|
||||
|
@ -209,6 +211,45 @@ var initDemo = function(sandbox) {
|
|||
|
||||
};
|
||||
|
||||
function tryLocaleDetect() {
|
||||
// lets fire off a request to get our headers which then
|
||||
// can help us identify what locale the browser is in.
|
||||
// wrap everything in a try since this is a third party service
|
||||
try {
|
||||
$.ajax({
|
||||
url: 'http://ajaxhttpheaders.appspot.com',
|
||||
dataType: 'jsonp',
|
||||
success: function(headers) {
|
||||
changeLocaleFromHeaders(headers['Accept-Language']);
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
console.warn('locale detect fail', e);
|
||||
}
|
||||
}
|
||||
|
||||
function changeLocaleFromHeaders(langString) {
|
||||
try {
|
||||
var languages = langString.split(',');
|
||||
var desiredLocale;
|
||||
for (var i = 0; i < languages.length; i++) {
|
||||
var lang = languages[i].slice(0, 2);
|
||||
if (intl.langLocaleMap[lang]) {
|
||||
desiredLocale = intl.langLocaleMap[lang];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!desiredLocale || desiredLocale == intl.getLocale()) {
|
||||
return;
|
||||
}
|
||||
// actually change it here
|
||||
GlobalState.locale = desiredLocale;
|
||||
events.trigger('localeChanged');
|
||||
} catch (e) {
|
||||
console.warn('locale change fail', e);
|
||||
}
|
||||
}
|
||||
|
||||
if (require('../util').isBrowser()) {
|
||||
// this file gets included via node sometimes as well
|
||||
$(document).ready(init);
|
||||
|
|
|
@ -9,6 +9,18 @@ var getDefaultLocale = exports.getDefaultLocale = function() {
|
|||
return 'en_US';
|
||||
};
|
||||
|
||||
// resolve the messy mapping between browser language
|
||||
// and our supported locales
|
||||
var langLocaleMap = exports.langLocaleMap = {
|
||||
en: 'en_US',
|
||||
zh: 'zh_CN',
|
||||
ja: 'ja',
|
||||
ko: 'ko',
|
||||
es: 'es_AR',
|
||||
fr: 'fr_FR',
|
||||
de: 'de_DE',
|
||||
};
|
||||
|
||||
var fallbackMap = {
|
||||
'zh_TW': 'zh_CN'
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue