根据语言环境设置html dir属性

时间:2019-11-02 19:21:22

标签: angular angular-i18n

在angular.json的构建配置中将i18n-locale设置为像希伯来语这样的RTL语言,不会将html的dir属性更改为RTL。是否有另一种方法可以根据语言环境自动设置html或主体dir属性?

1 个答案:

答案 0 :(得分:1)

AppComponent应该听以下语言更改:

app.component.ts

@Component(...)
export class AppComponent {

  public dir: string;
  constructor(private translate: TranslateService) {
    translate.onLangChange.subscribe(x => this.dir = x == "en" ? "ltr" : "rtl")
  }

}

app.component.html:

<div [dir]="dir">
    <router-outlet></router-outlet>
</div>