-
-
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
Conversation
Hmm, this could work, but would only be active on second page load, but that's not a big issue. If we wanna go the backend route (and I would certainly prefer), we should probably reject #21440 now. |
The real question is thought: How does go's date formatting hold up to JS |
It is something still needs to be investigated, and I think we can wait for more time to collect more voices from other maintainers about the locale/date format problem. |
Yes, we need a decision between this PR and #21429 (comment). This PR:
My PR:
|
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 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.
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.
Yup, I could also agree so.
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.
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 ....
@@ -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()}); |
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 in document.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 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.
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. a auto
language could be added that defaults to window.navigator.language
, similar to the auto
theme.
My opinion: I still prefer #21429 because JS Intl APIs are more powerful and because it works on first page load too. And secondly, because I'm more fluent in JS than golang. |
I could also agree with #21429 if there would be a stable solution for the case: <script init.js />
<time id=time1 />
<!--
very long content, which need a few seconds to transfer,
then browser will partially render the content above,
while the remaining content will be rendered a few seconds later.
-->
<time id=time2 />
<script index.js /> |
I'm sure there is a solution, but it can't be doing |
A demo for
Then backend code can render contents with correct locale and timezone, there will be no UI flicker on frontend.
Try it in a private window.
Some backgrounds and discussions: #21429
In short, at the moment, backend code doesn't know user's timezone, so rendered date/time would be in wrong timezone.
There could be 2 approaches:
Which way should we go?