duality_ duality_ - 1 month ago 14
HTML Question

Easy way to translate a website

Question



I have a fairly static website with just a few basic PHP usage. Now the customer would like to have this website translated. I do have a solution, but it's cumbersome and I was wondering how others are doing it and what is the standard (in frameworks, etc.).

My Way



My way (I have simplified it a bit for the sake of easier understanding): I generate a PHP array for each language from the database and store this array in a language file, like es.php for Spanish.

I then translate a string in HTML using a tr function like this:

Before:
<h1>Hello World</h1>


After:
<h1><?php echo tr('Hello World'); ?></h1>
which gives
<h1>Hola Mundo</h1>
for Spanish.


The Problem



This is cumbersome and error prone. I have to go through each .php file and replace all the hardcoded strings with this PHP tag with echo.

Is there a better way? How are others doing it? If needed, I can elaborate on my implementation.

Answer

A little late for you, I suppose but in case someone like me stumbles across this thread... Because I currently have the same problem you do. Unfortunately, there doesn't appear to be a "non-cumbersome way" to do this with PHP. Everything seems to involve lots of function-calls (if you have a lot of text).

Well... there is ONE convenient way. Not exactly safe though. Manipulating the output buffer before it's sent to the user: => http://dev-tips.com/featured/output-buffering-for-web-developers-a-beginners-guide

So you could depending on the language chosen just define an array filled with "from->to"-data and replace all the readable text in your buffer by looping through that.

But of course... if you e.g. replace "send" (English) with "senden" (German) and you link to a "send.html", it would break that link.

So if one has to translate not only long, definitely unique strings but also shorter ones, one would have to manipulate only the text that is readable to the user. There is a solution for that too - however, that is JavaScript based: => http://www.isogenicengine.com/documentation/jquery-multi-language-site-plugin/

Comments