northamerican
northamerican

Reputation: 2332

opinion on my method for a multi language site

i'm working on my own way of handling a bilingual site. my method should allow proper search engine indexing and will keep everything on one page without external files.

one function would handle which content to display:

<?
    function l($en, $fr){
        echo ($_GET['lang'] === 'fr') ? $fr : $en ;
    }
?>

then the appropriate text will display according to language in URL (/?lang=en)

<h1><? l('welcome!', 'bienvenue!') ?></h1>

for an image this is my solution:

<img src="<? l('hi-en.png', 'hi-fr.png')?>" width="100" height="20">

can anyone name drawbacks to this method if used? is it unusual to have a single function handle language for pages which would include all language content?

Upvotes: 1

Views: 138

Answers (1)

pauljz
pauljz

Reputation: 10901

The general idea of using a singleton or global function like your l function is very common. You're definitely on the right track!

Your method does have some drawbacks, though:

  • If you have the same text or image that appears in numerous places in the code, you need to maintain the translation in every place.
  • Updating or correcting a translation requires wading through code, which is very difficult for inexperienced coders or non-coders (say, if you have a translator helping you).
  • If you were ever to add a new language, you would have to modify every source file, which would be excruciating. This may be unlikely, but if it ever happened, you'd be rather cross with yourself.

A more typical solution is to have the translations located in a separate file, either as a simple hash or as a structured data format like XML, and then your l function would just look like l('welcome'); the parameter is a key, and l will look up the correct translation in the given language from the separate file.

Upvotes: 1

Related Questions