Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change language without reloading page with PHP / Jquery

I am working on a multilangual website right now. I am currently including the related language file ('lange/_en.php') for language phrases. To change languages users will select their language from a < select > item. The thing i want to do is changing related phrases (and urls too if possible) in the page without refreshing or submitting the page.

I remember i saw something like this in web but i have no idea where.

Any help or any ideas about how this thing can be done?

like image 390
Mustafa Avatar asked Dec 06 '25 04:12

Mustafa


1 Answers

The issue with this is that a language change doesn't only affect a small section of the page, it affects the whole page. So really, you are left with three choices.

  1. The simple way which is indeed reloading the whole page. It's easy to implement, easy to maintain, and doesn't require you to make sure that JavaScript currently running on your page is aware of the new language at runtime.

  2. The complicated way which is getting all the new markup via AJAX and replacing the content of the <body> tag with the reloaded content. This will cause issues with other scripts running (such as image carousels, etc.) that holds a reference to an element so you have to reinitialize every single script that is running on settimeout() on your page.

  3. The close to impossible way which is to have a client side dictionary, selecting each relevant tag, and changing its contents with the new language. This is a pain to setup and a pain to maintain. You literally need a section tailored to each specific page. Again, if you have scripts with strings, you'll have to make sure that the strings they use are updated to the new language.

You are better off simply reloading the page. It will work without JavaScript and it's a one time deal that won't bother users.

like image 108
Andrew Moore Avatar answered Dec 08 '25 16:12

Andrew Moore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!