Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Universal html lang tag

In Angular Universal I have an index.html file. At the top it has

<html lang="en">

I would like to change this based on the page I am on. maldonadoattorney.com/es/jailreleases would be

<html lang="es">

maldonadoattorney.com/jailreleases would be

<html lang="en">

Is there an accepted way of doing this? Have read other questions and DOCUMENT is deprecated, so am wary of using it.

I have my site map set up with hreflang tags, but would like the html lang tag to be correct. Currently, I'm taking the lang="en" tag out of my index.html.

like image 787
flobacca Avatar asked Dec 31 '18 19:12

flobacca


1 Answers

You're right but DOCUMENT from @angular/platform-browser is deprecated in favor of DOCUMENT from @angular/common.

So you can use the following code:

import { DOCUMENT } from '@angular/common';

...

export class AppComponent implements OnInit {

  constructor(@Inject(DOCUMENT) private document: Document) {}

  ngOnInit() {
    this.document.documentElement.lang = 'es'; 
  }
  ...
}
like image 196
yurzui Avatar answered Oct 06 '22 14:10

yurzui