简体   繁体   English

在angular4应用程序上处理RTL

[英]Handling RTL on angular4 application

I am new to angular4, is there a way to integrate the rtlcss together with ngx-translate or directly on angular4 typescript? 我是angular4的新手,有没有办法将rtlcssngx-translate集成在一起或直接在angular4打字稿上集成?

RTLCSS: http://rtlcss.com/learn/ RTLCSS: http: //rtlcss.com/learn/

ngx-translate: https://www.npmjs.com/package/@ngx-translate/core ngx-translate: https ://www.npmjs.com/package/@ngx-translate/core

I already integrated the ngx-translate but I have an issue with RTL then I came to rtlcss.com which seems to be good to have but I don't how to implement it there as they don't provide it on docs. 我已经集成了ngx-translate,但是我对RTL有问题,然后来到rtlcss.com ,这似乎很好,但是由于他们没有在文档中提供它,所以我不打算在那里实现它。

Any suggestions or sample will be appreciated. 任何建议或样品将不胜感激。 Thank you. 谢谢。

RTLCss is a tool intended to be used as a global package not as a dependency, you give it a LTR based stylesheet, and you get the corresponding RTL css. RTLCss是一种旨在用作全局包而不是依赖项的工具,您给它提供了一个基于LTR的样式表,并获得了相应的RTL css。 It has also a CLI which simplify the process: RTLSCss CLI Guide . 它还具有一个可以简化过程的CLIRTLSCss CLI指南

As for handling the RTL direction with ngx-translate, I had a "dir" property in each language .json file that had a value of "ltr" or "rlt", see this plunker: https://plnkr.co/edit/oPXmAS?p=preview 至于使用ngx-translate处理RTL方向,我在每种语言.json文件中都有一个“ dir”属性,其值是“ ltr”或“ rlt”,请参见以下代码: https ://plnkr.co/edit / oPXmAS?p =预览

With that"dir" property, you can either set a global container div that has an attribute dir with that "dir" value, and also add a special class to it if that value is "rtl" (refer to the previous plunker). 使用该“ dir”属性,您可以设置一个全局容器div,该容器的dir属性具有该“ dir”值,并且如果该值是“ rtl”,则可以向其添加一个特殊类(请参阅前面的插件)。 And you can set any special css properties that you have for the "rtl" languages (font-family, size ...). 您可以为“ rtl”语言设置任何特殊的css属性(字体系列,大小...)。

@Component({
  selector: 'my-app',
  template: `
  <div dir="{{ 'dir' | translate }}" [class.rtl]="('dir' | translate) === 'rtl'">
    <h2>{{ 'HOME.TITLE' | translate }}</h2>
    <label>
      {{ 'HOME.SELECT' | translate }}
      <select #langSelect (change)="translate.use(langSelect.value)">
        <option *ngFor="let lang of translate.getLangs()" [value]="lang" [selected]="lang === translate.currentLang">{{ lang }}</option>
      </select>
    </label>
  </div>
  `,
  styles: ['.rtl { color: red; }']
})

If you don't want to use a global div and want to set this in the body tag you'll have to inject a rendrer in the app.component like stated in this answer: Angular2 add class to body tag 如果您不想使用全局div并将其设置在body标签中,则必须在app.component中注入一个rendrer,如此答案中所述: Angular2将class添加到body标签

I would use Angular CDK BidiModule https://material.angular.io/cdk/bidi/api to detect direction 我将使用Angular CDK BidiModule https://material.angular.io/cdk/bidi/api来检测方向

The bidi package provides a common system for components to get and respond to change in the application's LTR/RTL layout direction bidi软件包为组件提供了一个通用系统,以获取和响应应用程序的LTR / RTL布局方向的更改。

It should work for you, but it has nothing to do with ngx-translate 它应该为您工作,但与ngx-translate无关

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM