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

Demo for making backend code know browser's locale and timezone #21443

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,9 @@ func Contexter(ctx context.Context) func(next http.Handler) http.Handler {
ctx.Req = WithContext(req, &ctx)
ctx.csrf = PrepareCSRFProtector(csrfOpts, &ctx)

browserSyncJson := ctx.GetCookie("_gitea_bsj")
ctx.PageData["browserSyncJson"] = browserSyncJson

// Get flash.
flashCookie := ctx.GetCookie("macaron_flash")
vals, _ := url.ParseQuery(flashCookie)
Expand Down
9 changes: 9 additions & 0 deletions templates/base/head_script.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,12 @@ If you introduce mistakes in it, Gitea JavaScript code wouldn't run correctly.
{{/* in case some pages don't render the pageData, we make sure it is an object to prevent null access */}}
window.config.pageData = window.config.pageData || {};
</script>

<script type="module">
const browserSyncJson = JSON.stringify({lc: window.navigator.language, tz: new Date().getTimezoneOffset()});
Copy link
Member

@silverwind silverwind Oct 13, 2022

Choose a reason for hiding this comment

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

We don't need to communicate window.navigator.language, as we already have the language from the chosen language setting (reflected in document.documentElement.lang).

Copy link
Contributor Author

@wxiaoguang wxiaoguang Oct 13, 2022

Choose a reason for hiding this comment

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

That's another question needs to be discussed. window.navigator.language is user browser's default language, which is different from the user chosen language for Gitea document.documentElement.lang.

So I just report the navigator language as it is, then backend can know: browser's default language, user chosen language.


Indeed the navigator.language could be removed if it's useless in the future.

Copy link
Member

@silverwind silverwind Oct 14, 2022

Choose a reason for hiding this comment

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

I think the only place we should rely on window.navigator.language is to determine initial language for users that have set none, e.g. a auto language could be added that defaults to window.navigator.language, similar to the auto theme.

if (window.config.pageData.browserSyncJson != null && window.config.pageData.browserSyncJson !== browserSyncJson) {
document.cookie = '_gitea_bsj=' + encodeURIComponent(browserSyncJson) + '; path=/'; // tell the backend about locale and timezone
document.body.style.setProperty('display', 'none', 'important'); // hide the page to prevent flash during reload
setTimeout(() => window.location.reload(), 1000); // use a setTimeout to simulate a slow network
Copy link
Member

@silverwind silverwind Oct 13, 2022

Choose a reason for hiding this comment

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

I would not do this reload, just live with the fact that it works only on second page load. There is certainly the danger of a reload loop when something goes wrong otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, I could also agree so.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ps: cookie won't be a problem, the CSRF token and Gitea session cookie are all there, unless only the document.cookie doesn't work ....

}
</script>
3 changes: 3 additions & 0 deletions templates/home.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{{template "base/head" .}}
<div>
Backend has the user browser's locale and timezone offset now: {{.PageData.browserSyncJson}}
</div>
<div class="page-content home">
<div class="ui stackable middle very relaxed page grid">
<div class="sixteen wide center aligned centered column">
Expand Down