RTL without reload



  • hey everyone,
    I’m trying to get the language from the url paramete ,
    e.g localhost:8081/en or localhost:8081/en
    my code is something like this :

    import { I18nUtil } from '@/shared/i18n/i18n-util';
    export default {
      async beforeRouteEnter(to, from, next) {    
        let language = to.params.lang
        if(!language){
          language = "en"
        }
        console.log('language',language)
        console.log('lang code',getLanguageCode())
        I18nUtil.doChangeLanguage(language)
        localStorage.setItem('language', language)
    }
    }
    

    and the doChangeLanguage function is like this:

    static doChangeLanguage(language) {
       let hostname = location.hostname;
       let port = location.port;
       let protocol = window.location.protocol
       let host = window.location.host // js-tricks.info
       let search = window.location.search // ?s=flexbox
       let pathname = window.location.pathname // Returns path only : "check-path-example"
       let url = window.location.href // Returns full URL : 'https://js-tricks.info/check-path-example'
    
     
    
       if (hostname.includes('localhost')) {
     
         setLanguageCode(language);
        // document.location.reload();
         return
       } 
    
       if (language == 'en') {
         window.location = url.replace('ar.', '')
         return
       }
    
       if (language == 'ar') {
         host = host.replace('www.', '')
         window.location = url.replace(host, `ar.${host}`)
         return
       }
     }
    
    export function setLanguageCode(arg) {
      if (!languages[arg]) {
        throw new Error(`Invalid language ${arg}.`);
      }
    
      moment.locale(arg);
      localStorage.setItem('language', arg);
    
      if (getLanguage().dictionary.validation) {
        setYupLocale(getLanguage().dictionary.validation);
      }
    }
    

    using this code only the direction of the syntax changes to RTL or LTR accoring to the parameter in the route
    but to change the language I need to relaod the page
    does anyone know why this happens ?
    why the html is still rendered in english when i write localhost:8081/ar although the direction rendered in the html is RTL


Log in to reply