Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP & MySQL - Best way to handle different database language contents

The question is as follows : what's the best way to handle different database language contents? Suppose you have a shopping cart whose products may have several languages, what would be the best approach to build the db scheme?

Cheers!

like image 503
yoda Avatar asked Jul 26 '11 18:07

yoda


People also ask

What is PHP is used for?

PHP (Hypertext Preprocessor) is known as a general-purpose scripting language that can be used to develop dynamic and interactive websites. It was among the first server-side languages that could be embedded into HTML, making it easier to add functionality to web pages without needing to call external files for data.

What coding is PHP?

The PHP Programming Language Meaning Explained. PHP is an open-source server-side scripting language that many devs use for web development. It is also a general-purpose language that you can use to make lots of projects, including Graphical User Interfaces (GUIs).

Is PHP used anymore?

PHP is known to be the most frequently used programming language. According to W3Techs, 78.8% of all websites are using PHP for their server-side. Interesting fact: PHP originally stood for Personal Home Page. Now PHP is widely known and thought of as Hypertext Preprocessor.


2 Answers

I would store a unique translation key in the table, and then either have another table or perhaps translation files so that you can reference the translation from the key.

table product:

id: 15
name: product.foo_widget
attr: ...
... etc

table translation:

lang: en
key: product.foo_widget
trans: Foo Widget
like image 146
Derek Stobbe Avatar answered Oct 12 '22 02:10

Derek Stobbe


Have the language of the current user as a property of your user object (or whatever pattern you use for user tracking). When you query the DB for strings, add a conditional to pull the language of the current user if it is available.

In this example, I put the language_id in $_SESSION; I usually would not do user tracking in this fashion but I believe the concept is illustrated. In your database, each product would have multiple entries for each language with 0 being the default of the site (presumably English). The query will grab the language-specific line item or fall back to the site default if there isn't a language-specific item available.

// the id of the item you're after
$item_id = 1;
$sql = 'SELECT id, name FROM product_names WHERE item_id = '.$item_id.' AND (language_id = '.$_SESSION['user']['language'].' OR language_id = 0) ORDER BY language_id DESC LIMIT 1';

I use this same concept for general strings around the site - I have a database table called "content_strings" with the fields id, text, and language_id.

like image 33
Chris Baker Avatar answered Oct 12 '22 03:10

Chris Baker