From 4a81f7796bbe15228b236b2a6aa729abd769bffd Mon Sep 17 00:00:00 2001 From: kunw Date: Wed, 2 Nov 2016 11:48:43 +0800 Subject: [PATCH 1/4] Fixes issues about setting cookies in multi-language. --- src/ui/controllers/base.go | 52 ++++++++++++------- .../optional-menu/optional-menu.directive.js | 17 +++--- .../js/services/i18n/services.i18n.js | 19 ++----- .../js/session/session.current-user.js | 12 +++-- 4 files changed, 52 insertions(+), 48 deletions(-) diff --git a/src/ui/controllers/base.go b/src/ui/controllers/base.go index c8d2c7a39..150476c87 100644 --- a/src/ui/controllers/base.go +++ b/src/ui/controllers/base.go @@ -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, diff --git a/src/ui/static/resources/js/components/optional-menu/optional-menu.directive.js b/src/ui/static/resources/js/components/optional-menu/optional-menu.directive.js index 64b7121ef..df78217e2 100644 --- a/src/ui/static/resources/js/components/optional-menu/optional-menu.directive.js +++ b/src/ui/static/resources/js/components/optional-menu/optional-menu.directive.js @@ -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', diff --git a/src/ui/static/resources/js/services/i18n/services.i18n.js b/src/ui/static/resources/js/services/i18n/services.i18n.js index 1f77a7c4f..acdb4de35 100644 --- a/src/ui/static/resources/js/services/i18n/services.i18n.js +++ b/src/ui/static/resources/js/services/i18n/services.i18n.js @@ -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]; } diff --git a/src/ui/static/resources/js/session/session.current-user.js b/src/ui/static/resources/js/session/session.current-user.js index a02355c35..9815f93a2 100644 --- a/src/ui/static/resources/js/session/session.current-user.js +++ b/src/ui/static/resources/js/session/session.current-user.js @@ -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) { From 075c9e378d55a5c91bbc4f9ea8ce150330e7d442 Mon Sep 17 00:00:00 2001 From: kunw Date: Wed, 2 Nov 2016 12:03:05 +0800 Subject: [PATCH 2/4] Change template file for multi-language settings. --- src/ui/views/sections/header-content.htm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ui/views/sections/header-content.htm b/src/ui/views/sections/header-content.htm index 96b19475b..ceee2ced5 100644 --- a/src/ui/views/sections/header-content.htm +++ b/src/ui/views/sections/header-content.htm @@ -25,7 +25,7 @@