Reputation: 16276
I am the developer of a webapp (a task manager).
This app has to be available in many languages. Thus, the user has the usual language dropdown to switch to a different language.
I had to build a custom translation system having MySQL as backend because I had particoular constraints.
The main table on the system (including some records as examples) is the translation
table:
id | language_id | string_id | message
1 | en | WELCOME | welcome
2 | it | WELCOME | benvenuto
I can write a function getTranslatedMessage
that will get the string_id
as input and will
_ detect the current language
_ make a db query and retrieve one row
_ return the translated message
The problem of such a system is that will put a lot of strength on the database. If I have 20 strings to translate on a page, there will be 20 queries to the database per pageload.
Do you think I should go this path because it is the cleanest and easiest, using memcache
to keep the result of each getTranslatedMessage
output, using the language_id
and string_id
as key for the memcache entry?
Would you do that in a different way?
I was thinking maybe by loading all the messages for a language in a global array $lang
on startup.
Any suggestion will be more than welcome as this component will be key in my application.
Thanks,
Dan
Upvotes: 1
Views: 517
Reputation: 149
I have attempted to make my own translation based system.
I store my translations in a Database in order to make it easy to modify them. And then I use this code to make a super global. E.x. below!
$sql = mysql_query("SELECT * FROM `f_translations` where `language` = '".mysql_real_escape_string($setting[language])."'");
while ($a = mysql_fetch_array($sql)) { $name=$a['category'].'-'.$a['name']; $language[$name] = utf8_encode($a['translation']);}
Good Luck!
Upvotes: 1
Reputation: 145512
This isn't application data. It doesn't belong into the database. (See osCommerce as good example on not to follow this path). Since you are already planning to read it in one swoop, you could very well store the application UI texts in files.
Both the lengthy getTranslatedMessage()
function name and the mnemonic message ids are not a good idea either. Use raw english sentences rather than uppercase abbreviations. And make the translation function call less cumbersome. Otherwise it will make your application more inconvenient as soon as you reach beyond the expected 20 strings. (OTOH if you want to psychologically limit the amount of translations, the msgid and long function name are appropriate.)
This is boring advise. But you should consider going with the native PHP gettext functions. They have heaps of drawbacks, but fitness for this purpose isn't one.
Upvotes: 3
Reputation:
For multilanguage web sites I'm using global dictionaries, hash tables in separated files. For example:
---File with English translations---
$lang['hello'] = 'Hello';
---File with German translations---
$lang['hello'] = 'Hallo';
And I'm just including the required file. I think that it's quite cheap of resources and fast. It's easy to add another language file (even with web form) and to use it.
Upvotes: 2
Reputation: 318
How many records in your table do you have? If you have not too much (<10000), of course you must load it into one array and then get variables from this array.
$LANG = array();
$result = mysql_query("SELECT string_id, message FROM translation");
while ($row = mysql_fetch_row($result))
$LANG[$row[0]] = $row[1];
function __($id) {
global $LANG;
return (isset($LANG[$id])) ? $LANG[$id] : $id;
}
Use the name __() instead of getTranslatedMessage. It's much more easy.
Upvotes: 3