mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-23 02:35:17 +01:00
Merge pull request #993 from wknet123/dev-language-cookies
Fixes issues about setting cookies in multi-language
This commit is contained in:
commit
f9b84e4a7c
@ -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,
|
||||
}
|
||||
|
@ -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',
|
||||
|
@ -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];
|
||||
}
|
||||
|
@ -25,7 +25,7 @@
|
||||
</div>
|
||||
<!-- Collect the nav links, forms, and other content for toggling -->
|
||||
<div class="collapse navbar-collapse" id="bs-harbor-navbar-collapse-1">
|
||||
<optional-menu version="{{.Version}}" login-status="//vm.loginStatus//"></optional-menu>
|
||||
<optional-menu version="{{.Version}}" language="{{.Lang}}" login-status="//vm.loginStatus//"></optional-menu>
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
<li>
|
||||
<navigation-header></navigation-header>
|
||||
|
Loading…
Reference in New Issue
Block a user