views:

169

answers:

3

Hello everyone!

I'm writing Content Management software in PHP (which should not be bigger then 3kb when minified), but what engine should I use for languages (english, dutch, german, chinese, etc...)? I was thinking of creating a function called

function _(){}

that reads strings from a file (a .ini file or similar). But does somebody has an (preferably one with as less code as possible) engine that might be smaller or faster? Thanks in advance.

I'm not sure if these engines exist already, if not, please say and I will use the _() function.

+1  A: 

You can't use _() because this is an build-in function for internationalization. Otherwise you are free to roll your own function (call it __) or use the build-in one which uses the widespread gettext system.

sebasgo
A: 

Drupal, for example, uses function t() for this purposes.

Kuroki Kaze
+1  A: 

If I were you I would make my translation function like such (which I believe is very similar to gettext): make it into an sprintf()-like function and translate based on the format string, like so:

function __() {
   $a    = func_get_args();
   $a[0] = lookup_translation($a[0]);
   return call_user_func_array("sprintf", $a);
}

Now, you can use the function simply like this:

echo __("Thanks for logging in, %s!", $username);

And in a data file somewhere you have:

"Thanks for logging in, %s!"="Merci pour enlogger, %s!" (*)

The advantages of this are:

  • You don't have to think up identifiers for every single message: __("login_message", $username), __("logout_message", $username), etc...
  • You don't immediately have to write a translation for the string, which you would have to if you just used an identifier. You can defer the translation until later, once you're done coding and everything works in English.
  • (Similarly) You don't have to translate all strings for all languages at once, but you can do it in chunks

For maximum convenience, I would make the __ function log untranslated messages somewhere, so you don't have to go hunting for untranslated strings. Let the system tell you what needs to be translated!

(*) Disclaimer: I don't speak French ;)

rix0rrr