Fixes issues about setting cookies in multi-language.

This commit is contained in:
kunw 2016-11-02 11:48:43 +08:00
parent 0506a950cc
commit 4a81f7796b
4 changed files with 52 additions and 48 deletions

View File

@ -43,24 +43,29 @@ func (b *BaseController) Prepare() {
var lang string
langCookie, err := b.Ctx.Request.Cookie("language")
if err != nil {
log.Errorf("Error occurred in Request.Cookie: %v", err)
}
if langCookie != nil {
lang = langCookie.Value
}
if len(lang) == 0 {
sessionLang := b.GetSession("lang")
if sessionLang != nil {
b.SetSession("Lang", lang)
lang = sessionLang.(string)
} else {
al := b.Ctx.Request.Header.Get("Accept-Language")
if len(al) > 4 {
al = al[:5] // Only compare first 5 letters.
if i18n.IsExist(al) {
lang = al
langRequest := b.GetString("lang")
if langRequest != "" {
lang = langRequest
} else {
langCookie, err := b.Ctx.Request.Cookie("language")
if err != nil {
log.Errorf("Error occurred in Request.Cookie: %v", err)
}
if langCookie != nil {
lang = langCookie.Value
}
if lang == "" {
sessionLang := b.GetSession("lang")
if sessionLang != nil {
b.SetSession("lang", lang)
lang = sessionLang.(string)
} else {
al := b.Ctx.Request.Header.Get("Accept-Language")
if len(al) > 4 {
al = al[:5] // Only compare first 5 letters.
if i18n.IsExist(al) {
lang = al
}
}
}
}
@ -70,8 +75,15 @@ func (b *BaseController) Prepare() {
lang = defaultLang //Set default language if not supported.
}
b.Ctx.SetCookie("language", lang, 0, "/")
b.SetSession("Lang", lang)
cookies := &http.Cookie{
Name: "language",
Value: lang,
HttpOnly: true,
Domain: "/",
}
http.SetCookie(b.Ctx.ResponseWriter, cookies)
b.SetSession("lang", lang)
curLang := langType{
Lang: lang,

View File

@ -25,21 +25,19 @@
function OptionalMenuController($scope, $window, I18nService, LogOutService, currentUser, $timeoutm, trFilter, $filter) {
var vm = this;
vm.currentLanguage = I18nService().getCurrentLanguage();
vm.languageName = I18nService().getLanguageName(vm.currentLanguage);
I18nService().setCurrentLanguage(vm.currentLanguage);
var i18n = I18nService();
i18n.setCurrentLanguage(vm.language);
vm.languageName = i18n.getLanguageName(vm.language);
console.log('current language:' + vm.languageName);
vm.supportLanguages = I18nService().getSupportLanguages();
vm.supportLanguages = i18n.getSupportLanguages();
vm.user = currentUser.get();
vm.setLanguage = setLanguage;
vm.logOut = logOut;
vm.about = about;
function setLanguage(language) {
I18nService().setCurrentLanguage(language);
vm.languageName = i18n.getLanguageName(vm.language);
var hash = $window.location.hash;
$window.location.href = '/language?lang=' + language + '&hash=' + encodeURIComponent(hash);
}
@ -72,7 +70,8 @@
'restrict': 'E',
'templateUrl': '/optional_menu?timestamp=' + new Date().getTime(),
'scope': {
'version': '@'
'version': '@',
'language': '@'
},
'controller': OptionalMenuController,
'controllerAs': 'vm',

View File

@ -20,14 +20,15 @@
.module('harbor.services.i18n')
.factory('I18nService', I18nService);
I18nService.$inject = ['$cookies', '$window'];
I18nService.$inject = ['$window'];
function I18nService($cookies, $window) {
function I18nService($window) {
var cookieOptions = {'path': '/'};
var messages = $.extend(true, {}, eval('locale_messages'));
var defaultLanguage = 'en-US';
var currentLanguage = defaultLanguage;
var supportLanguages = {
'en-US': 'English',
'zh-CN': '中文'
@ -47,30 +48,20 @@
return {
'setCurrentLanguage': function(language) {
if(!angular.isDefined(language) || !isSupportLanguage(language)) {
language = defaultLanguage;
}
$cookies.put('language', language, cookieOptions);
},
'setDefaultLanguage': function() {
$cookies.put('language', defaultLanguage, cookieOptions);
currentLanguage = language;
},
'getCurrentLanguage': function() {
return $cookies.get('language') || defaultLanguage;
return currentLanguage;
},
'getLanguageName': function(language) {
if(!angular.isDefined(language) || !isSupportLanguage(language)) {
language = defaultLanguage;
}
$cookies.put('language', language, cookieOptions);
return supportLanguages[language];
},
'getSupportLanguages': function() {
return supportLanguages;
},
'unset': function(){
$cookies.put('language', defaultLanguage, cookieOptions);
},
'getValue': function(key) {
return messages[key];
}

View File

@ -20,9 +20,9 @@
.module('harbor.session')
.controller('CurrentUserController', CurrentUserController);
CurrentUserController.$inject = ['$scope', 'CurrentUserService', 'currentUser', '$window', '$document', 'LogOutService'];
CurrentUserController.$inject = ['$scope', 'CurrentUserService', 'currentUser', '$window', '$document', 'LogOutService', '$timeout'];
function CurrentUserController($scope, CurrentUserService, currentUser, $window, $document, LogOutService) {
function CurrentUserController($scope, CurrentUserService, currentUser, $window, $document, LogOutService, $timeout) {
var vm = this;
@ -41,9 +41,11 @@
function getCurrentUserFailed(e){
console.log('Failed to get current user:' + e);
LogOutService()
.success(logOutSuccess)
.error(logOutFailed);
$timeout(function() {
LogOutService()
.success(logOutSuccess)
.error(logOutFailed);
}, 500);
}
function logOutSuccess(data, status) {