在Angular 7中如何从英语(LTR)加载阿拉伯语(RTL)的CSS?

huangapple go评论72阅读模式
英文:

In Angular 7 how to load CSS of Arabic language (RTL) from English language (LTR)

问题

我正在使用Angular 7开发一个应用程序,使用ngx-translate进行国际化,并基于Bootstrap的AdminLTE 3进行开发。
我有两个CSS文件:

  1. 一个用于从左到右(LTR)的语言。
  2. 另一个用于从右到左(RTL)的语言。

当我选择阿拉伯语(RTL方向)时,如何加载bootstrap_rtl.css并卸载bootstrap_ltr.css。

<div class="dropdown-menu dropdown-menu-sm dropdown-menu-right">
    <a class="dropdown-item" (click)="useLanguage('en')">
        English
    </a>
    <div class="dropdown-divider"></div>
    <a class="dropdown-item" (click)="useLanguage('ar')">
        Arabic
    </a>
    <div class="dropdown-divider"></div>
    <a class="dropdown-item" (click)="useLanguage('ta')">
        Russian
    </a>
</div>
export class NavbarComponent implements OnInit {
  constructor(private translate: TranslateService) { }
  ngOnInit() {
  }

  useLanguage(language: string) {
    this.translate.use(language);
  }
}
英文:

I am developing an application in Angular 7 using ngx-translate for internationalization and bootstrap based AdminLTE 3.
I have two css:

  1. One for LTR based languages
  2. another for RTL based languages.

When I select Arabic language which is a RTL direction, how to load the bootstrap_rtl.css and unload the bootstrap_ltr.css.

&lt;div class=&quot;dropdown-menu dropdown-menu-sm dropdown-menu-right&quot;&gt;
    &lt;a class=&quot;dropdown-item&quot; (click)=&quot;useLanguage(&#39;en&#39;)&quot;&gt;
      English
    &lt;/a&gt;
    &lt;div class=&quot;dropdown-divider&quot;&gt;&lt;/div&gt;
    &lt;a class=&quot;dropdown-item&quot; (click)=&quot;useLanguage(&#39;ar&#39;)&quot;&gt;
      Arabic
    &lt;/a&gt;
    &lt;div class=&quot;dropdown-divider&quot;&gt;&lt;/div&gt;
    &lt;a class=&quot;dropdown-item&quot; (click)=&quot;useLanguage(&#39;ta&#39;)&quot;&gt;
      Russian
    &lt;/a&gt;
&lt;/div&gt;

<!-- begin snippet: js hide: false console: true babel: false -->

&lt;!-- language: lang-html --&gt;

export class NavbarComponent implements OnInit {
  constructor(private translate: TranslateService) { }
  ngOnInit() {
  }

  useLanguage(language: string) {
    this.translate.use(language);
  }
}

&lt;!-- end snippet --&gt;

答案1

得分: 7

以下是已翻译的内容:

你可以在翻译文件中创建一个键,作为当前主题的标志,如果它是 rtlltr,这个值将根据你选择的语言而改变。

style.scss

.ltr {
  @import 'themes/_en';
}

.rtl {
    @import 'themes/_ar';
}

_ar.json

{
  "currentTheme":"rtl"
}

_en.json

{
  "currentTheme":"ltr"
}

app.template

<div  [ngClass]="'currentTheme' | translate">  // &#128072;
{{'currentTheme' | translate}}
<p class="base-align">
<hello name="{{ name }}"></hello>
  Start editing to see some magic happen :)
</p>

theme <button (click)="useLanguage('en')">English</button>
<button (click)="useLanguage('ar')">Arabic</button>
</div>

当语言改变时,currentTheme 的值将改变,样式也会改变。

英文:

you can create a key in the translation file as flage of the current theme if it's rtl or ltr and this value will change base of the language that you have select

style.scss

.ltr {
  @import &#39;themes/_en&#39;;
}

.rtl {
    @import &#39;themes/_ar&#39;;
}

_ar.json

{
  &quot;currentTheme&quot;:&quot;rtl&quot;
}

_en.json

{
  &quot;currentTheme&quot;:&quot;ltr&quot;
}

app.template

&lt;div  [ngClass]=&quot;&#39;currentTheme&#39; | translate&quot;&gt;  // &#128072;
{{&#39;currentTheme&#39; | translate}}
&lt;p class=&quot;base-align&quot;&gt;
&lt;hello name=&quot;{{ name }}&quot;&gt;&lt;/hello&gt;
  Start editing to see some magic happen :)
&lt;/p&gt;

theme &lt;button (click)=&quot;useLanguage(&#39;en&#39;)&quot;&gt;English&lt;/button&gt;
&lt;button (click)=&quot;useLanguage(&#39;ar&#39;)&quot;&gt;Arabic&lt;/button&gt;
&lt;/div&gt;

> when the languae change the value of currentTheme will chnage and the style will change

stackblitz demo 🚀

huangapple
  • 本文由 发表于 2020年1月6日 15:30:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定