Search code examples
angularuniversallang

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.


Solution

  • 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'; 
      }
      ...
    }