Skip to content

Commit

Permalink
geosolutions-it#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 committed Sep 10, 2024
1 parent 548407d commit 375292a
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 @@ -30,14 +30,23 @@ class Localized extends React.Component {
};
}

componentDidMount() {
this.updateDocumentLangAttribute();
}

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

render() {
let { children } = this.props;

if (this.props.messages && this.props.locale) {
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 @@ -62,6 +71,12 @@ class Localized extends React.Component {
};
}, {});
};

updateDocumentLangAttribute() {
if (document?.documentElement) {
document.documentElement.setAttribute("lang", this.props.locale);
}
}
}

export default Localized;

0 comments on commit 375292a

Please sign in to comment.