Skip to content

Commit

Permalink
#10158: set HTML document language once per mount/update
Browse files Browse the repository at this point in the history
As requested, the document language is now set when the component is mounted or the language is changed instead of with every rerender.
  • Loading branch information
fkellner authored Sep 10, 2024
1 parent 548407d commit fed18c5
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion web/client/components/I18N/Localized.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class Localized extends React.Component {
if (typeof children === 'function') {
children = children();
}
document.documentElement.setAttribute("lang", this.props.locale);

return (<IntlProvider key={this.props.locale} locale={this.props.locale}
messages={this.flattenMessages(this.props.messages)}
Expand All @@ -51,6 +50,22 @@ class Localized extends React.Component {
return null;
}

componentDidMount() {
this.updateDocumentLangAttribute();
}

componentDidUpdate(prevProps) {
if (this.props.locale !== prevProps.locale) {

Check failure on line 58 in web/client/components/I18N/Localized.jsx

View workflow job for this annotation

GitHub Actions / test-front-end

Trailing spaces not allowed
this.updateDocumentLangAttribute();
}
}

updateDocumentLangAttribute() {
if (document?.documentElement) {

Check failure on line 64 in web/client/components/I18N/Localized.jsx

View workflow job for this annotation

GitHub Actions / test-front-end

Trailing spaces not allowed
document.documentElement.setAttribute("lang", this.props.locale);
}
}

flattenMessages = (messages, prefix = '') => {
return Object.keys(messages).reduce((previous, current) => {
return typeof messages[current] === 'string' ? {
Expand Down

0 comments on commit fed18c5

Please sign in to comment.