Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added user language setting #3875

Merged
merged 20 commits into from
May 5, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
6bd5c33
Added user language setting
kolaente May 1, 2018
2f1934b
Added translation string for setting
kolaente May 1, 2018
064eb06
Fixed import order + typo
kolaente May 1, 2018
5f7f2cc
improved checking if the user has a language saved in the db
kolaente May 1, 2018
047f488
The current saved language is now set a default inside the dropdown
kolaente May 1, 2018
d06e37f
fmt
kolaente May 1, 2018
5ed980d
When a user signs in and doesn't have a language saved, the current b…
kolaente May 1, 2018
0d51925
updated gitea-sdk
kolaente May 1, 2018
d72c183
Merge branch 'master' of https://github.com/go-gitea/gitea into save-…
kolaente May 1, 2018
08ef144
Merge branch 'master' of https://github.com/go-gitea/gitea into save-…
kolaente May 2, 2018
6b90197
Merge branch 'master' of https://github.com/go-gitea/gitea into save-…
kolaente May 2, 2018
dfe96f1
Made tests work again
kolaente May 3, 2018
0a25402
trigger CI
kolaente May 3, 2018
0b72ded
trigger CI
kolaente May 3, 2018
818fac3
fmt
kolaente May 3, 2018
425af39
re-trigger that FUCKING CI SO IT REALLY PICKS UP THE LATEST COMMIT IS…
kolaente May 3, 2018
1f02048
re-trigger that FUCKING CI SO IT REALLY PICKS UP THE LATEST COMMIT IS…
kolaente May 3, 2018
45213db
Merge branch 'save-user-language' of https://github.com/kolaente/gite…
kolaente May 3, 2018
cf65e37
When loggin in, only the language col gets updated instead of everything
kolaente May 4, 2018
5660ff0
Merge branch 'master' into save-user-language
lafriks May 4, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ var migrations = []Migration{
NewMigration("add is_fsck_enabled column for repos", addFsckEnabledToRepo),
// v61 -> v62
NewMigration("add size column for attachments", addSizeToAttachment),
// v62 -> v63
NewMigration("add language comumn for user setting", addLanguageSetting),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix typo

}

// Migrate database to current version
Expand Down
22 changes: 22 additions & 0 deletions models/migrations/v62.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2018 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import (
"fmt"
"github.com/go-xorm/xorm"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix import order

)

func addLanguageSetting(x *xorm.Engine) error {
type User struct {
Language string `xorm:"VARCHAR(5)"`
}

if err := x.Sync2(new(User)); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that if a user logs in after an upgrade of the database, the language he/she is currently using is set as language in database? Do you think that the default language should be en_US (at database migration)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think empty can be left as default after migration but when first logging in if it is empty it could be set to current ui language

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't thought of that, I'll implement it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

return fmt.Errorf("Sync2: %v", err)
}

return nil
}
2 changes: 2 additions & 0 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type User struct {
Website string
Rands string `xorm:"VARCHAR(10)"`
Salt string `xorm:"VARCHAR(10)"`
Language string `xorm:"VARCHAR(5)"`

CreatedUnix util.TimeStamp `xorm:"INDEX created"`
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
Expand Down Expand Up @@ -185,6 +186,7 @@ func (u *User) APIFormat() *api.User {
FullName: u.FullName,
Email: u.getEmail(),
AvatarURL: u.AvatarLink(),
Language: u.Language,
}
}

Expand Down
1 change: 1 addition & 0 deletions modules/auth/user_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ type UpdateProfileForm struct {
KeepEmailPrivate bool
Website string `binding:"ValidUrl;MaxSize(255)"`
Location string `binding:"MaxSize(50)"`
Language string `binding:"Size(5)"`
}

// Validate validates the fields
Expand Down
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ change_username = Your username has been changed.
change_username_prompt = Note: username changes also change your account URL.
continue = Continue
cancel = Cancel
language = Language

lookup_avatar_by_mail = Look Up Avatar by Email Address
federated_avatar_lookup = Federated Avatar Lookup
Expand Down
5 changes: 5 additions & 0 deletions public/swagger.v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -7236,6 +7236,11 @@
"format": "int64",
"x-go-name": "ID"
},
"language": {
"description": "User locale",
"type": "string",
"x-go-name": "Language"
},
"login": {
"description": "the user's username",
"type": "string",
Expand Down
6 changes: 6 additions & 0 deletions routers/user/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyR
ctx.Session.Set("uid", u.ID)
ctx.Session.Set("uname", u.Name)

// Language setting of the user overwrites the one previously set
if u.Language != "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

len(u.Language) != 0

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

ctx.SetCookie("lang", u.Language, nil, setting.AppSubURL)
}

// Clear whatever CSRF has right now, force to generate a new one
ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL)

Expand Down Expand Up @@ -698,6 +703,7 @@ func SignOut(ctx *context.Context) {
ctx.SetCookie(setting.CookieUserName, "", -1, setting.AppSubURL)
ctx.SetCookie(setting.CookieRememberName, "", -1, setting.AppSubURL)
ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL)
ctx.SetCookie("lang", "", -1, setting.AppSubURL) // Setting the lang cookie will trigger the middleware to reset the language ot previous state.
ctx.Redirect(setting.AppSubURL + "/")
}

Expand Down
7 changes: 6 additions & 1 deletion routers/user/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"github.com/Unknwon/i18n"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix import order

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

)

const (
Expand Down Expand Up @@ -105,6 +106,7 @@ func SettingsPost(ctx *context.Context, form auth.UpdateProfileForm) {
ctx.User.KeepEmailPrivate = form.KeepEmailPrivate
ctx.User.Website = form.Website
ctx.User.Location = form.Location
ctx.User.Language = form.Language
if err := models.UpdateUserSetting(ctx.User); err != nil {
if _, ok := err.(models.ErrEmailAlreadyUsed); ok {
ctx.Flash.Error(ctx.Tr("form.email_been_used"))
Expand All @@ -115,8 +117,11 @@ func SettingsPost(ctx *context.Context, form auth.UpdateProfileForm) {
return
}

// Update the language to the one we just set
ctx.SetCookie("lang", ctx.User.Language, nil, setting.AppSubURL)

log.Trace("User settings updated: %s", ctx.User.Name)
ctx.Flash.Success(ctx.Tr("settings.update_profile_success"))
ctx.Flash.Success(i18n.Tr(ctx.User.Language, "settings.update_profile_success"))
ctx.Redirect(setting.AppSubURL + "/user/settings")
}

Expand Down
14 changes: 14 additions & 0 deletions templates/user/settings/profile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@
<input id="location" name="location" value="{{.SignedUser.Location}}">
</div>

<div class="field">
<label for="language">{{.i18n.Tr "settings.language"}}</label>
<div class="ui language selection dropdown" id="language">
<input name="language" type="hidden">
<i class="dropdown icon"></i>
<div class="text">{{.LangName}}</div>
<div class="menu">
{{range .AllLangs}}
<div class="item{{if eq $.Lang .Lang}} active selected{{end}}" data-value="{{.Lang}}">{{.Name}}</div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should check if .Lang is equal with user's language in database and not with the current language.
If I set german as my language but change it manually by selecting another language (example: english) from the dropdown in the footer, the selected language in the form will be english. The expected selected language should be german.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed that.

{{end}}
</div>
</div>
</div>

<div class="field">
<button class="ui green button">{{$.i18n.Tr "settings.update_profile"}}</button>
</div>
Expand Down
2 changes: 2 additions & 0 deletions vendor/code.gitea.io/sdk/gitea/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.