0

I have this really tiny (static) site, that is basically just my online business card and I want to have it in English and German. I detect the browser language and also let the user chose manually; so I have the right $lang variable ready.

My question is how are the multiple languages best implemented (or loaded)? Right now I do this with PHP switch statements but I feel this is not proper at all. My next guess is to use XMLs, one for every language, then load the right one but I wonder if there is a more clean, simple and proper way to do this; meaning easy to maintain and extend, easy to understand, compliant with standards and fast.

Thanks for the help!

Balz Guenat
  • 1,552
  • 2
  • 15
  • 35

2 Answers2

2

Have an array with all the texts, and place them in seperate files (one for German, one for English):

<? return array(
       'contact' => 'Kontakt'
);

Load that array and use it to display the text.

If you're using a CMS (content management system) there are other options.

Peter van der Wal
  • 11,141
  • 2
  • 21
  • 29
  • 2
    +1. Using arrays like this make it much easier to fallback to a given language if a translation does not exist, using array_merge for example. See http://3v4l.org/dQb9t – Anthony Sterling Nov 02 '13 at 14:06
  • Just to get this right... When I first load the page, I load the array of the corresponding language with an include. When I then want to insert the text I do array['contact'] or array['introduction']? What if I want to have HTML code inside the strings? Do I use CDATA? – Balz Guenat Nov 02 '13 at 14:13
  • In its simplest form, yep. HTML? CDATA would be irrelevant. – Anthony Sterling Nov 02 '13 at 14:16
  • 1
    Indeed. If your included file starts with `return` you can use `$lang = include('de.php')`. Or as Anthony suggested (+1 for that): `$lang = array_merge(include('en.php'), include('de.php'));` if English is your fallback language. – Peter van der Wal Nov 02 '13 at 14:16
0

The standard PHP way is to use gettext functions and .po/.mo files as explained here

Community
  • 1
  • 1
evalarezo
  • 1,134
  • 7
  • 13