multilingual system

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
vivendi
Forum Newbie
Posts: 3
Joined: Sat Oct 01, 2005 12:04 pm

multilingual system

Post by vivendi »

I want to setup my database in a way so that it supports multi-languages for certain things. For example, a menu structure + the content. But the problem is, is that i'm not sure how to set this up.

I thought of two possibilities.

1. I could setup my table like this:

Code: Select all

tablename: menu
id, menutitle,  content, lang
1   mainmenu  hello       en
2  hoofdmenu  hallo       nl
This way i could easily select the desired menu item + its content with a simple "WHERE lang = '$language'" in a query.

The downside is that i also want to show this in a CMS page. But i want it so that both languages can be editted at the same time. So suppose i show a html table in the CMS page, with 2 columns. In the left column i show the EN menutitle and content - and in the right column i show the NL menutitle and content. Below that is a Submit button that should save the menutitles and contents of both languages to the database. But how on earth am i going to know which POST values belong to EN and which VALUES belong to NL...???

On the other hand, i could also create a table like this:

tablename: menu
id, menutitle_en, menutitle_nl, content_en, content_nl

I know this isn't a normalized database structure, but it does make the job easier when i want to save the content to the database. Take the CMS page example that i described earlier, with the 2 column table.
In the left column i simple create textfields that match the name of the database column names. So that way i can always tell what POST data to save in what column. But this is most probably not the cleanest way to go...

So my question basically is, what is the correct way of doing this? Any of the above? Or something, completely different...?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: multilingual system

Post by Christopher »

Usually you would pick a base language to use as your keys:

Code: Select all

tablename: menu
id, menutitle,  content, lang
1  hallo       hello       en
2  hoofdmenu  mainmenu  en
Then when you ask for 'hallo' and the language is set to 'en' it will return 'hello'.
(#10850)
vivendi
Forum Newbie
Posts: 3
Joined: Sat Oct 01, 2005 12:04 pm

Re: multilingual system

Post by vivendi »

Christopher wrote:Usually you would pick a base language to use as your keys:

Code: Select all

tablename: menu
id, menutitle,  content, lang
1  hallo       hello       en
2  hoofdmenu  mainmenu  en
Then when you ask for 'hallo' and the language is set to 'en' it will return 'hello'.
But the problem is, is that i don't necessarily want to show one of the languages. I want to show both of them. Both the title and content can be editted on the webpage. So that means i'll probably have something like these elements:

<input type="text" name="menutitle_nl" value="<?= getValue('menutitle', 'nl'); ?>" />
<textarea name="content_nl"><?= getValue('menutitle', 'nl'); ?></textarea>

<input type="text" name="menutitle_en" value="<?= getValue($nameOfColumn, 'en'); ?>" />
<textarea name="content_en"><?= getValue('menutitle', 'en'); ?></textarea>

Now suppose i edited those textfields and submit them. Then all fields are stored in a POST variable. I can use a foreach() loop to loop through all the post values, but there's no way see which POST values belongs to what language code. So that's basically my issue...
jarofgreen
Forum Commoner
Posts: 71
Joined: Sun Jul 11, 2010 12:40 pm

Re: multilingual system

Post by jarofgreen »

vivendi wrote:but there's no way see which POST values belongs to what language code.
In your example HTML you have the following fields titles:
menutitle_nl
content_nl
menutitle_en
content_en

So, if it ends in _nl it's Netherlands, if it ends in _en its English ... that's how you tell, no? (Am I missing something?)
Post Reply