diff --git a/src/ui/controllers/base.go b/src/ui/controllers/base.go index c8d2c7a39..cbae01b7c 100644 --- a/src/ui/controllers/base.go +++ b/src/ui/controllers/base.go @@ -42,19 +42,19 @@ var mappingLangNames map[string]string func (b *BaseController) Prepare() { var lang string + var langHasChanged bool - 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) + langRequest := b.GetString("lang") + if langRequest != "" { + lang = langRequest + langHasChanged = true + } 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 } else { al := b.Ctx.Request.Header.Get("Accept-Language") if len(al) > 4 { @@ -63,16 +63,23 @@ func (b *BaseController) Prepare() { lang = al } } + langHasChanged = true } } - if _, exist := supportLanguages[lang]; !exist { //Check if support the request language. - lang = defaultLang //Set default language if not supported. + if langHasChanged { + if _, exist := supportLanguages[lang]; !exist { //Check if support the request language. + lang = defaultLang //Set default language if not supported. + } + cookies := &http.Cookie{ + Name: "language", + Value: lang, + HttpOnly: true, + Domain: "/", + } + http.SetCookie(b.Ctx.ResponseWriter, cookies) } - b.Ctx.SetCookie("language", lang, 0, "/") - 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/views/sections/header-content.htm b/src/ui/views/sections/header-content.htm index 96b19475b..ab6a4b373 100644 --- a/src/ui/views/sections/header-content.htm +++ b/src/ui/views/sections/header-content.htm @@ -25,7 +25,7 @@