question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

lang attribute in html element

See original GitHub issue
[ ] bug report => check the FAQ and search github for a similar issue or PR before submitting
[ ] support request => check the FAQ and search github for a similar issue before submitting
[X] feature request

Current behavior The html element in the DOM don’t has the lang attribute.

Expected/desired behavior Having the lang attribute in html element. The DOM must look like: <html lang="en">

What is the motivation / use case for changing the behavior? Be more standard on language identification for SEO or other tools.

  • ngx-translate version: 6.0.1

  • Angular version: 4.0.1

  • Browser: [all]

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:19

github_iconTop GitHub Comments

4reactions
lpallicommented, Jun 15, 2017

As wokaround it’s possible to subscribe to onLangChange in the app.component and modify the DOM via ElementRef.

My code:

import { Component, ElementRef, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';

import { TranslateService } from '@ngx-translate/core';

@Component({
  selector: 'gp-app-root',
  encapsulation: ViewEncapsulation.None,
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit, OnDestroy {

  /**
   * The onLangChange subscription to update the component if the language change.
   * @type {any}
   */
  onLangChange: Subscription = undefined;

  /**
   * The constructor.
   * 
   * @param el the current element reference
   * @param translate the translate service
   */
  constructor(public el: ElementRef, public translate: TranslateService) { }

  ngOnInit() {
    this.updateLanguage();
    this.onLangChange = this.translate.onLangChange.subscribe(() => {
      this.updateLanguage();
    });
  }

  ngOnDestroy() {
    if (this.onLangChange !== undefined) {
      this.onLangChange.unsubscribe();
    }
  }

  /**
   * Update the language in the lang attribute of the html element.
   */
  updateLanguage(): void {
    const lang = document.createAttribute('lang');
    lang.value = this.translate.currentLang;
    this.el.nativeElement.parentElement.parentElement.attributes.setNamedItem(lang);
  }
}
1reaction
desferocommented, Jul 8, 2017

Maybe can be done as a new parameter to the module (what do you think about reflectInHtml)

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTML Global lang Attribute - W3Schools
The lang attribute specifies the language of the element's content. Common examples are "en" for English, "es" for Spanish, "fr" for French, and...
Read more >
Using the HTML lang attribute - TPGi
The HTML lang attribute is used to identify the language of text content on the web. This information helps search engines return language ......
Read more >
lang - HTML: HyperText Markup Language - MDN Web Docs
The lang global attribute helps define the language of an element: the language that non-editable elements are written in, or the language ...
Read more >
Declaring language in HTML - W3C
The xml:lang attribute is the standard way to identify language information in XML. Ensure that the values for both attributes are identical.
Read more >
<html> element must have a lang attribute | Axe Rules
Add a lang attribute to the html element (e.g. <html lang="en"> ) whose value represents the primary language of document. Make sure you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found