mysql - PHP optimize website language Pack
I was thinking about creating a 'language table' for my customer's webshop, easily editable by them.
The idea is to have this kind of table :
id - key - en - fr - es
____________________________________
1 - greet - Hello - Bonjour - Ola
Then inside my php code, I would simply like to refer this using :
<h1><? echo i18n('greet'); ?></h1>
The language table would have about 500 rows, sometimes words, sometimes sentences.
I don't want to do a sql call everytime I call the i18n function, that could be 30..40 times per page.
Is there a way to analyse the current page, do 1 query with the needed keys? Should I simply load the whole language table (just key and used language) every time?
How can I optimize this?
Answer
Solution:
It is definitely worth your trouble to take a look at the WordPress internationalization / localization (I18N / L10N) system as you embark on this project. I have used this successfully to work with translators to localize a couple of rather complex chunks of WordPress php code. WordPress itself is available in dozens of national languages.
http://codex.wordpress.org/I18n_for_WordPress_Developers
It's based on the mature and excellent GNU gettext system for translation.
http://www.gnu.org/software/gettext/
It basically works by defining a suite of functions for enclosing text. The example you cited would be done this way
or this way, as a shortcut to
echo __()
There's quite a bit of elaboration in their function suite, which you can read about in the WordPress documentation.
You'll find excellent open-source and commercial software products for assisting both programmers and translators in producing the necessary data.
In WordPress, the various translations are stored as text files. There's no reason you couldn't adapt that to put your data in an RDMS.