Jump to content

How to pass a language variable through the website


transparencia

Recommended Posts

My website is multi-language and using mod_rewrite for clean urls. I have a $lang variable that stores the language code.

 

For example:

 

website.com/index.php?lang=en, which turns to website.com/en

 

I use a $_GET["lang"] to get the language and prepare the links to the other pages, for example a link to page1.php would be page1.php?lang=$_GET["lang"].

 

What I want to know is if this is the right way to do it or there is a better way.

Link to comment
Share on other sites

If you are having multiple PHP pages (page1.php / page2.php etc) then yes I would do it this way.

 

However, it's not the best way to do it really. I always design sites so that you have one access point for all page requests.

 

I would do pages as:

 

site.com/en/page1 - which reverts to site.com/en/index.php and you pull the page1 value out and display the relevant file (probaly /pages/html/page1.php etc.)

site.com/en/page2 - which reverts to site.com/en/index.php and you pull the page2 value out and display the relevant file (probaly /pages/html/page2.php etc.)

 

All links are then to site.com/en/page3 etc.

 

My last example is based on a site designed using a proper MVC system where you have one access point (logic is sperated from html and there is minimal code duplication). It also uses a clean url / mod_rewite as you want. index.php is the only file that is called frm the address bar and this file then calls the relevant other pages (page1, 2 etc.)

 

EDIT: (in reality it would revert to site.com/index.php - you're unlikely to have folders for each language!)

Link to comment
Share on other sites

I don't know about right way, but the way I do it is to figure out the user's language (they choose, or use a javascript to find out from the browser), then store their language in a session var that is available to all pages.  Then you don't have to worry about appending the var to the URL for every link.

 

Unless of course you want to have language in the URL so that they are different URLs for search engines, etc.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.