-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()}); | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, I could also agree so. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
</script> |
There was a problem hiding this comment.
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 indocument.documentElement.lang
).There was a problem hiding this comment.
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 Giteadocument.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.
There was a problem hiding this comment.
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. aauto
language could be added that defaults towindow.navigator.language
, similar to theauto
theme.