We help IT Professionals succeed at work.

Multi Language Website - Best way with PHP + MySQL

BrighteyesDesign asked
I am about the build a multi language website using PHP + MySQL.

It's for a real estate website and the translations will be manually interpreted (rather than automated like Google translate).

I am really looking for the most efficient way of creating this site.

I'm guessing i'll need a database table per language: one in english, one in spanish and one in french.

Would I then need 3 duplicate sites/directories? Is there a better way to do this?

Watch Question

Most Valuable Expert 2011
Top Expert 2016

You might follow the general design that PHP.net uses.  They have separate directories for separate languages.

You might also want to let the client choose the language.  You can cookie the browser to remember the last choice.  This is a better strategy than trying to detect the client's IP address and choosing the language based on the IP.  Whether I was in Paris or Madrid, I would still prefer to use English.
RobOwner (Aidellio)
Most Valuable Expert 2015
@Ray as you say, PHP use separate directories but that can be masked by a http url rewrite?  In other words you don't need to duplicate the code but detect via the url what language is being requested.

I agree that it's a waste of time trying to detect the IP for the language as @Ray has said.

In terms of the database there'll be countless ways to do this i'm sure.  In the past I have had tables for logical elements of a site such as for the products you're selling with basic properties such as codes and identifiable descriptions.   I have another table that handles the language for common properties of the product such as description and title.  Each row in that table represents a language.
Most Valuable Expert 2011
Top Expert 2016

But why bother to mask a URL?  Why not allow clients to send a link that tells the true URL?  It just feels more user-friendly to me that way.


Thanks for your help,

With the PHP.net method what would be in the root directory? If the default language is english would the root index.php redirect to /en/index.php

Also, with the drop down that selects the different languages. I guess this redirects the user to uURL's along the lines of...

http://us.php.net/manual/en/(code to detect the current pages filename)
http://us.php.net/manual/es/(code to detect the current pages filename) etc...
I am doing exactly that right now.  

Much depends on how similar your content is going to be.

Assuming the content is quite similar, the way I am doing it is as if it were a data driven site in English.  That is, all text, including "alt" text comes from a database.  But each phrase in the database has three different versions, depending on the active language as selected by the user at the beginning.  

Note that sometimes you have a difficulty with this because the structure of the languages differ, so you can't get too finely grained in most cases.  For example, most languages have a different order for where they place the subject, verb, and direct object.   So whole phrases and sentences are better than breaking up the sentence into small pieces.   Except navigation words.  Depending on the language, those can often be broken down quite small.

I am not keeping duplicate sites.  That would be a lot of work and error prone every time something changes.  So all three languages will sit inside the main structure of the site.  But there will be different sub-directories within the site for language-specific images, documents, videos, etc.

Mostly, the above going will work if you are sharing navigation and structure amongst the sites.

If they are three distinct sites with their own structure, background, and navigation style, and they just happen to share a domain name and an option to choose a language at the beginning, then that is a whole 'nother ball game.  E.g., if the English version is going to be very business like, but the Spanish one is going to have a Latin flair, then I would go with three different directories.

Hope this helps.

Re figuring out the lang based on IP is problematic.  What is someone is a native Spanish speaker but they are travelling in Montreal, Quebec and they are accessing the site from a netbook?  

Even in the US, you can't assume English just because it is a US-based IP.  In some parts of L.A. even the roadside ads are in Spanish because the majority of the people who live in that area speak Spanish.