最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How I change the dir from LTR to RTL in ionic 4 programmatically - Stack Overflow

programmeradmin0浏览0评论

I am working on ionic 4 multi-language app English and Arabic so I need when the user switching between langs the layout change!

I was doing this in ionic 3 by platform.setDir('rtl') at onChangLang event

    if (languageId === 'ar') {
      this.platform.setDir('rtl', true);
      this.translate.setDefaultLang(languageId);

    } else {
      this.platform.setDir('ltr', true);
      this.translate.setDefaultLang(languageId);
    }

But now in ionic 4 platform.setDir() has been deprecated ( removed ) for whatever reasons and the documentation doesn't include any replacement for platform.setDir() or how to change the dir programmatically!!

So my Question is how to change the layout direction programmatically using typescript like it was on ionic 3?!!

I am working on ionic 4 multi-language app English and Arabic so I need when the user switching between langs the layout change!

I was doing this in ionic 3 by platform.setDir('rtl') at onChangLang event

    if (languageId === 'ar') {
      this.platform.setDir('rtl', true);
      this.translate.setDefaultLang(languageId);

    } else {
      this.platform.setDir('ltr', true);
      this.translate.setDefaultLang(languageId);
    }

But now in ionic 4 platform.setDir() has been deprecated ( removed ) for whatever reasons and the documentation doesn't include any replacement for platform.setDir() or how to change the dir programmatically!!

So my Question is how to change the layout direction programmatically using typescript like it was on ionic 3?!!

Share Improve this question asked Jan 30, 2019 at 8:05 hesham shawkyhesham shawky 1,1516 gold badges22 silver badges48 bronze badges 2
  • cant you use two separate CSS file for English and Arabic – Nifal Nizar Commented Jan 30, 2019 at 10:10
  • Not an efficient solution a lot of RTL issues/conflicts happened when trying doing this! like the solution down there using JS Dom document.documentElement.dir = 'rtl'; – hesham shawky Commented Jan 30, 2019 at 20:18
Add a ment  | 

2 Answers 2

Reset to default 3

You can use document.documentElement.dir = "rtl";

if (languageId === 'ar') {
  document.documentElement.dir = "rtl";
  this.translate.setDefaultLang(languageId);

} else {
  document.documentElement.dir = "ltr";
  this.translate.setDefaultLang(languageId);
}

I think you'll find the answer here -- it worked for me. The author references a Mozilla/MDN link, and creates an Angular Service to reference the DOCUMENT and flip from 'ltr' to 'rtl'. Rather than copy that answer take a look at the links included in this post

发布评论

评论列表(0)

  1. 暂无评论