Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I change the dir from LTR to RTL in ionic 4 programmatically

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?!!

like image 522
hesham shawky Avatar asked Jan 30 '19 08:01

hesham shawky


2 Answers

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);
}
like image 140
AFT Avatar answered Nov 09 '22 10:11

AFT


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

like image 2
Various Artist Avatar answered Nov 09 '22 10:11

Various Artist