views:

144

answers:

4

I've created a language pack for a site before, but I'm not sure if what I'm doing is the best method.

Basically, all I have is a file with variables defining string constants. Usually a set of arrays where an array usually refers to a particular elements of the site.

Then the site code just includes the appropriate file based on a flag and then echo's out the appropriate array element.

What are some ways of doing this to reduce maintenance headaches and performance?

A: 

A couple of the most common ways to do this are:

1) Use define('SOME_STRING', 'Some string'); - simply have a different file stuffed with these defines for each language. This is very easy, and you don't have to use "global" all over the place.

2) Wrap strings in a function: translate('My String') - this is more flexible as you can parse your code to create lists of strings to translate, and add in features like variables, e.g. translate('I can count to [number]', 10); (of course, you normally pick a shorter function name, like "_")

Greg
+1  A: 

The best option you could have, with PHP, is GetText, but not all the server have it built-in, so it may be a show stopper if you're managing your server.

gizmo
+1  A: 

I suggest using Zend_Translate. It is somewhat a combination of the other suggestions people left here, only more maintainable and better designed.

You can switch adapters depending on your preference (supports gettext, csv and a multitude of others), you don't need defines or globals polluting your global scope and it is well documented.

Eran Galperin
I'll second this as in some cases Gettext is not thread safe so on some setups Gettext will be a thorn in your side, plus it depends on that extension being installed. Zend_Translate implements multiple common methods from Gettext to XML based translations and is integrated in other components
dcousineau
A: 

Thanks for the help guys, I'll try out zend translate since I build on the zend framework. Not sure why I didn't know about the object before :P

AndreLiem