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?




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.

It's based on the mature and excellent GNU gettext system for translation.

It basically works by defining a suite of functions for enclosing text. The example you cited would be done this way

<h1><? echo __('greet'); ?></h1>

or this way, as a shortcut toecho __()

<h1><? _e('greet'); ?></h1>

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.

People are also looking for solutions to the problem: php - How to call a function onClick of a directory


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.