Using PHP 5.x and MySQL 5.x
So I asked a question yesterday about the best way of handling dynamic data in multilanguage, this question is what would be a good solution for the database structure to handle this. I have entries in the database for things like stories. My plan is to store a different version of the story for each language available in a db. So if the site supports english and spanish then in the admin tool they can add a english version of a story and a spanish version of a story.
My thoughts on that was to have seperate tables in the db, one for each language. So one story table for english version, one for spanish and one for whatever other languages. Then on the front end I simply allow the visitor to select what language to view the site in and via variables know what table to query to get that version of the story. But one issue is what if there isnt a spanish version but they selected spanish? Is that a good solution or is there a better way of doing this? Looking for suggestions.
Having multiple tables is really not necessary, unless you plan on having millions of stories… I usually go along with two tables; one for the item and another for the localized item
For example, a story would be like
table “story”
table “story_localized”
Performing the query is simply a matter of
JOINing the two tables :This configuration gives a few advantages :
** EDIT **
Just as a bonus, here is a “trick” to retrieve a story easily, regardless of the language it’s been written into
Will try to fetch the story in the
{desiredLanguage}, if the story is not available in that language, try to find it in{defaultLanguage}(ie. the site’s default language), and if still nothing found, it doesn’t matter which language to fetch the story, then, so fetch the first one found. All in one query, and all you need are 3 arguments: the story id, the desired language, and a default fallback language.Also, you can easily find out in what language the story is available into with a simple query :