Update index.html lang attribute dynamically

Viewed 1001

This is pretty much an update of AngularJS dynamic lang attribute of html for Angular >= 2.

My application is localized in three languages which the user can change dynamically, without page refresh. I need to set the index.html language attribute accordingly.

<html lang="en">
<head>
    <meta charset="utf-8">
    <title>My application</title>
    <base href="/">
</head>
<body>
    <app-root></app-root>
</body>
</html>

I need help only on changing the tag attribute, the application is already localized and supports language change.

1 Answers

You can use document.querySelector('html')?.setAttribute('lang', 'de'); to access the root html element and change the language attribute on it.

Related