Angular - 更新根 index.html 中的 html 属性
Angular - Update html attribute in root index.html
我的 index.html
中有以下内容
<!doctype html>
<html dir="ltr" lang="en">
<head>
<meta charset="utf-8">
<title>Halls Gate</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/png" href="assets/img/favicon.png">
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,500,600,700" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Istok+Web" rel="stylesheet">
</head>
<body>
<app-root></app-root>
</body>
</html>
我想根据所选语言更新组件 <html dir="ltr" lang="en">
中 html 元素中的 dir 和 lang 属性。
在 Angular 中有解决这个问题的方法吗?
谢谢。
这将是一种不好的做法。您应该改用 Internationalization。
国际化是设计和准备您的应用程序以使用不同语言的过程。本地化是将您的国际化应用程序翻译成特定语言环境的特定语言的过程。
开始:
ng serve --configuration=your locale id
我觉得,可以简单的改一下...
https://stackblitz.com/edit/angular-ih2drk?file=src%2Fapp%2Fapp.component.ts
import { Component, Renderer2 } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
name = 'Angular 6';
constructor(private renderer: Renderer2) {
this.renderer.setAttribute(document.querySelector('html'), 'lang', 'tr');
}
}
如果您正在使用 angular-i18n 并希望根据构建更改语言环境,您可以这样做
import {Component, Inject, LOCALE_ID} from '@angular/core';
import {DOCUMENT} from '@angular/common';
...
export class AppComponent implements OnInit {
...
constructor(@Inject(DOCUMENT) private document: Document, @Inject(LOCALE_ID) locale: string) {
this.document.documentElement.lang = locale;
}
...
}
我的 index.html
中有以下内容<!doctype html>
<html dir="ltr" lang="en">
<head>
<meta charset="utf-8">
<title>Halls Gate</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/png" href="assets/img/favicon.png">
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,500,600,700" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Istok+Web" rel="stylesheet">
</head>
<body>
<app-root></app-root>
</body>
</html>
我想根据所选语言更新组件 <html dir="ltr" lang="en">
中 html 元素中的 dir 和 lang 属性。
在 Angular 中有解决这个问题的方法吗?
谢谢。
这将是一种不好的做法。您应该改用 Internationalization。 国际化是设计和准备您的应用程序以使用不同语言的过程。本地化是将您的国际化应用程序翻译成特定语言环境的特定语言的过程。
开始:
ng serve --configuration=your locale id
我觉得,可以简单的改一下...
https://stackblitz.com/edit/angular-ih2drk?file=src%2Fapp%2Fapp.component.ts
import { Component, Renderer2 } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
name = 'Angular 6';
constructor(private renderer: Renderer2) {
this.renderer.setAttribute(document.querySelector('html'), 'lang', 'tr');
}
}
如果您正在使用 angular-i18n 并希望根据构建更改语言环境,您可以这样做
import {Component, Inject, LOCALE_ID} from '@angular/core';
import {DOCUMENT} from '@angular/common';
...
export class AppComponent implements OnInit {
...
constructor(@Inject(DOCUMENT) private document: Document, @Inject(LOCALE_ID) locale: string) {
this.document.documentElement.lang = locale;
}
...
}