Switching direction in Vue.js without needing to reload the page - Flip between rtl and ltr effortlessly

Currently, I am utilizing Vue.js along with vue-i18n to seamlessly switch between languages on my project without the necessity of refreshing the page. Nevertheless, when transitioning from Arabic to another language or vice versa, it requires changing the website's design direction from right-to-left to left-to-right.

The current setup necessitates a refresh to load the rtl.scss file:

Below is the code responsible for loading CSS upon refresh:

let locale = VueCookie.get('locale') || 'en'
if (locale === 'ar') {
  require('rtl.scss')
}

This next code snippet triggers a page refresh:

if (newLocale === 'ar' || oldLocale === 'ar') {
  window.location.reload()
}

Answer №1

Following Jacob Goh's suggestion, here is how you can make the necessary changes:

Revise your initial code snippet to this:

require('rtl.scss')
let localization = VueCookie.get('locale') || 'en'
if (localization === 'ar') {
  document.querySelector('body').classList.toggle('rtl');
}

Then update your second snippet to:

if (newLocale === 'ar' || oldLocale === 'ar') {
  document.querySelector('body').classList.toggle('rtl');
}

Finally, make adjustments to your rtl.scss file as shown below:

body.rtl {
  /* Previous scss styling */
}

Answer №2

Here is a straightforward option instead of the solution that was originally proposed:

const {t, locale} = i18n.global;

watchEffect(() => {
    document.dir = t('text_direction');
    document.documentElement.lang = locale.value;
})

To make this work, you need to include a text_direction attribute for each language (at least in the default one if applicable), set to either rtl or ltr. Whenever the language changes, this code will update the root element's direction accordingly.

If needed, you can use this property in your CSS styling as well.

This method eliminates the need for specific conditions for every language that requires a different text direction.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process by which jQuery delivers functions such as success and error?

I've examined the jquery script, but I'm still not sure how to create my own function that can return success or error similar to the example below: $.ajax({ type: "GET", url: "/some.xml", success: function() { /** Suc ...

I have noticed that the Javascript code is being loaded prior to the completion of my HTML/CSS page for some unknown

I am completely new to the world of web development. I'm encountering an issue where my JavaScript code (specifically alerts) is loading before my HTML/CSS page has fully loaded. I've been using sample alerts to test this out, and ideally, they s ...

The concept of JavaScript function aliasing appears to be ineffective

After coming across this specific question, I decided to experiment with the alias method rather than using the function-wrapper method. Unfortunately, I faced issues getting it to function properly in both Firefox 3 and 3.5beta4, as well as in Google Chro ...

What is the best way to alter the header in Django when a user is authenticated?

In my project, I have two headers: header.html and headersuccess.html. When a user is logged in, I need to change the header from header.html to headersuccess.html. How can I implement that? Here is an excerpt from my views.py file where I render loginsuc ...

Is there a way to make my DIVS update their content dynamically like buttons do, without manually adding an onclick function in the HTML code?

I am currently customizing a WordPress theme that lacks the option for onclick events on div elements. However, I can assign classes and IDs to them. In my design, I have four spans in a row, and when each span is clicked, I intend for the corresponding p ...

Avoiding Memory Leaks and Managing JS Heap Size While Polling Large Amounts of Data Every 5 Seconds with Vuex

I'm currently working on a Vue application that utilizes Vuex for state management. Within this application, I have several store modules set up at the root level. Periodically, every 5 seconds, I retrieve a large amount of data and update the store s ...

What is the best method for integrating both Jquery and Angularjs into a single project?

As we prepare to incorporate Jquery and AngularJs into our upcoming project, I've come across conflicting information. Some sources suggest using Jquery before AngularJs, while others recommend the opposite. This has left me a bit perplexed. For exam ...

Encountering difficulties with displaying error messages on the Material UI datepicker

I am currently utilizing React Material UI There is a need to display an error based on certain conditions that will be calculated on the backend. Although I have implemented Material UI date picker, I am facing issues with displaying errors. import * as ...

Seeking assistance to prevent data duplication when transferring information from list1 to list2 in React.js

As I work on developing two lists in React.js, I encountered an issue with avoiding repetitions when transferring items from list-1 to list-2. I need assistance creating a function that prevents duplicate items in list-2 while also ensuring that the items ...

Use multer-ftp to change the name of a file

I've been working on uploading a file to an FTP server using multer-ftp. The file is successfully uploaded, but I need to change the name of the file. Is there a way to accomplish this? var upload = multer({ storage: new FTPStorage({ basepath: ...

What is the best way to modify this state without altering the original array?

I am seeking guidance on how to avoid mutating state in a specific scenario: In my React Hooks setup, I have a TodoList component that displays a list of TodoItems. Each TodoItem can be clicked to trigger a function called toggle, which switches its compl ...

Avoiding unnecessary Ajax requests using pushstate for pagination

I encountered an issue with ajax pagination using the HTML API Pushstate. Here is the code I am working with: <ul class="small"> <li> <p>a</p> </li> </ul> <ul class="paging"> <li><a ...

Steps to assign the identifier as the current index in the v-for iteration

Struggling to assign the id based on the index of a v-for loop. I attempted: <li v-for="(item, index) in items" v-bind:key="item.id"> <p>{{item.name}}</p> <input id= {{index}} type= "number"> < ...

What could be the reason for the peculiar data being returned by Microsoft Graph list subscriptions?

I am currently in the process of configuring webhooks to automatically update my application's database whenever a contact is modified in Outlook. I need to modify my existing subscriptions before they expire, so I decided to retrieve a list of curren ...

Cross domain request in a simple HTML document

I'm currently working on an app that is strictly in plain HTML files without a server. I'm facing difficulties with cross domain requests from JavaScript. Whenever I try to make a request, the browser displays this error message: XMLHttpRequest ...

Troubleshooting Issue with jQuery replaceWith in Loop

Trying to make it so that when the update button is clicked, the text on the left side becomes an input box: Click the update button and the text on the left will be an input box However, instead of just one input box appearing, all the text on the left s ...

My picture is refusing to load... Why does it keep saying "image not found"? Any thoughts on why this might be

I've been trying to display a picture of myself on my html canvas, the image is stored in the correct folder. However, I keep encountering a strange error (shown above) and I can't seem to figure out what's causing it. If you have any insigh ...

Is there a way to get an iframe to mimic the behavior of other media elements within a horizontal scrolling container?

Take a look at this code snippet: $(document).ready(function() { $('.scrollable-area').on('wheel', function(e) { var scrollLeft = $(this).scrollLeft(); var width = $(this).get(0).scrollWidth - $(this).width(); var delta ...

Having trouble finding a solution to prevent code from automatically adding a class in jQuery/JavaScript?

I am currently in the process of customizing the FlexNav Plugin. I have made a modification to allow sub-menus to open on click instead of hover. However, a new issue has arisen where it requires two clicks to open a submenu. Upon investigation, I have i ...

What are the steps to implement timer control in Ajax using JavaScript?

Hello, I have been trying to establish communication with the server through my application. To achieve this, I made a call to the webservice using AJAX. Now, I am looking to incorporate a time interval in AJAX. Can anyone kindly provide guidance on how ...