-
Notifications
You must be signed in to change notification settings - Fork 164
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
fix: use lang attribute in the html tag #16410
fix: use lang attribute in the html tag #16410
Conversation
(introducing language to the html) <html theme=""> -> <html lan="en" theme=""> The actual rule/way of working is quite similar how the Component getLocale() is working: 1. trying to get the locale from UI 2. if there is I18N provider then the first locale will be used 3. last option is to fallback to the Locale.getDefault()
…-passed-setup-locale-values-13437
…xHtmlRequestHandler)
// similar to Component implementation: | ||
// worth to extract the getLocale to LocaleUtil potentially? | ||
Locale locale = getLocale(); | ||
indexDocument.getElementsByTag("html").get(0).attr("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.
Should this only be done if NO lang attribute was found? It's recommended to customize the index.html and a lot of people already did so. For example it's required for a11y to define the main language of the page and therefore most application already had to deal with this in someway and either set the value static in the index.html or later in some kind of rendering hook. So I would suggest to only change / add the value from the UI if there isn't already a lang attribute present to reduce regressions and unexpected side effects for people that have e.g. used a hard coded Spanish value but the server is running e.g. on English and guessing the wrong language of the page.
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.
IndexHtmlRequestListener
s are called after this locale logic, so that will provide a possibility to modify the locale if needed. Also any existing modifications from there should work as before.
For the index.html
template, maybe we should remove the hardcoded lang="en"
from there and then add check to the above logic to only add locale attribute if it does not exist in the index.html
in order to cover cases where there is a customized index.html
with a pre-set lang attribute already in place?
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.
(got interrupted by some other issues, problems)
Thank you both of you, it makes sense, we just need to find a solution that helps for the requesters and old customers have custom solutions in place.
- I did remove the relevant burned English language attribute from index.html-s (in tests as well for consistency)
- Also add the check, to make sure we do not overwrite some intentional custom solution.
Got back to the original issue, thinking through it should work for both customers asking it there, in their cases, and do not need to use that workaround anymore and they can just create an I18NProvider to overwrite the language.
I will do some small refactoring, but basically, that's it here 🙇
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.
Refactoring idea to reduce duplicated html-tag search and getLocale-call if not needed:
var htmlElement = indexDocument.getElementsByTag("html");
// only apply local based lang if index.html does not already contain it
if (!htmlElement.hasAttr("lang")) {
htmlElement.attr("lang", getLocale().getLanguage());
}
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.
Changed a tiny bit I liked the idea so added, thanks for the optimization idea plus added as well refactor for extracting and using the same logic in multiple places (component and here).
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.
Wanna move the Locale search also in the if to reduce the 'expensive' call on the hot path? Or it is needed somewhere else?
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.
Yes, we can do it, good catch, if we spare just some calls/executions why not? 👍
…-passed-setup-locale-values-13437
…-passed-setup-locale-values-13437
… as from discussions seems no point to add the language as burned in (rather it will be calculated from Locale/I18NProvider and so)
- Merge remote-tracking branch 'origin/main' into fix/lan-html-tag-should-be-created-considred-passed-setup-locale-values-13437
…-passed-setup-locale-values-13437
- based on knoobie's suggested optimization to only once fetch html tag element, - and using the same logic/classes for Components and IndexHtmlRequestHandler for the getting the locale,
…-passed-setup-locale-values-13437
Tested once more locally/manually with all the changes, it is adding the proper tag. |
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
Hi @czp13 and @czp13, when i performed cherry-pick to this commit to 24.0, i have encountered the following issue. Can you take a look and pick it manually? |
Hi @czp13 and @czp13, when i performed cherry-pick to this commit to 23.3, i have encountered the following issue. Can you take a look and pick it manually? |
Hi @czp13 and @czp13, when i performed cherry-pick to this commit to 23.2, i have encountered the following issue. Can you take a look and pick it manually? |
Fix and feature add language to HTML tags dynamically: 0. It is checking if it is already added (if not, then it will try to add it based on the following rules: 1. trying to get the locale from the UI 2. if there is an I18N provider then the first locale will be used 3. last option is to fallback to the Locale.getDefault() This is the same logic as is used in the Component class as well, to have consistency within the application. Covering tests and optimization, refactoring happened and added. Thanks to @knoobie and @tepi for reviews, ideas, and optimizations. Fixes # (issue):
Fix and feature add language to HTML tags dynamically: 0. It is checking if it is already added (if not, then it will try to add it based on the following rules: 1. trying to get the locale from the UI 2. if there is an I18N provider then the first locale will be used 3. last option is to fallback to the Locale.getDefault() This is the same logic as is used in the Component class as well, to have consistency within the application. Covering tests and optimization, refactoring happened and added. Thanks to @knoobie and @tepi for reviews, ideas, and optimizations. Fixes # (issue):
… (CP:24.0) (may not need) (#16502) * fix/feature/optimization: use lang attribute in the html tag (#16410) Fix and feature add language to HTML tags dynamically: 0. It is checking if it is already added (if not, then it will try to add it based on the following rules: 1. trying to get the locale from the UI 2. if there is an I18N provider then the first locale will be used 3. last option is to fallback to the Locale.getDefault() This is the same logic as is used in the Component class as well, to have consistency within the application. Covering tests and optimization, refactoring happened and added. Thanks to @knoobie and @tepi for reviews, ideas, and optimizations. Fixes # (issue): * Fix build error (comment format :/, hopefully)
… (CP:23.3) (#16500) * fix/feature/optimization: use lang attribute in the html tag (#16410) Fix and feature add language to HTML tags dynamically: 0. It is checking if it is already added (if not, then it will try to add it based on the following rules: 1. trying to get the locale from the UI 2. if there is an I18N provider then the first locale will be used 3. last option is to fallback to the Locale.getDefault() This is the same logic as is used in the Component class as well, to have consistency within the application. Covering tests and optimization, refactoring happened and added. Thanks to @knoobie and @tepi for reviews, ideas, and optimizations. Fixes # (issue): * Fix my merge/conflict work/commit discrepencies. (Did not accept all the correc changes first, but with this one it shall be good) * mvn formatter:format executed. * Fix build error (comment format:/ hopefully!)
Description
(introducing language to the HTML)
to:
The actual rule/way of working is quite similar to how the Component getLocale() is working:
Now it is added to the
IndexHtmlRequestHandler
classPros:
Cos:
It could be added to other places to only do this once. The benefit would be to be more performant, but also it would not be dynamic.
I have tested it locally and it is adding the attribute for all requests:
Fixes # (issue)
#13437
Type of change
Checklist
Additional for
Feature
type of change