Php – Localization in php, best practice or approach

localizationPHP

I am Localizing my php application. I have a dilemma on choosing best method to accomplish the same.

Method 1: Currently am storing words to be localized in an array in a php file

<?php

$values = array (
                        'welcome' => 'bienvenida'
                ); 

?>

I am using a function to extract and return each word according to requirement

Method 2: Should I use a txt file that stores string of the same?

<?php
$welcome = 'bienvenida'; 
?>

My question is which is a better method, in terms of speed and effort to develop the same and why?

Edit: I would like to know which method out of two is faster in responding and why would that be? also, any improvement on the above code would be appreciated!!

Best Answer

First off, gettext is a good way to go, so don't dismiss if it doesn't sound easy at first; However there are other options as well that be useful to know. Before explaining that let's take a look at your suggestions first:

In your array-way of doing this, you have a pretty much straightforward solution. The good thing is that you can store your translations in a data storage, then load it into your PHP script as an array, and that's it. However if you want to store your array statically in a PHP file, then editing it needs a) PHP programmer or an exprienced user who is familiar with the PHP syntax, just to edit the file b) syncing between even four or five different language files could be quite annoying and prone to errors.

What you need to consider here is: How can later add new elements to the language file? Can I leave it up to a translator or there should be a PHP programmer?

Your variable-way of doing it is not practical. Why? I assume you will have couple of functions at least in your application, right? Then you need to pass all of these variables when you call the function, or you need to global them. If you think you will have more than ten variables like this, then I strongly discourage you to do this. There might be name-conflict as well -- you can potentially override the value of another variable with the same name, however this could be solved by adding a prefix even as simple as a an underscore , so you will have $_welcome for example. Anyways, if I were you I wouldn't think about it even; Don't do it.

One good way of doing it is to define Constants with a prefix. So for example in your en.lang.php you will have something like define( 'LABEL_WELCOME', 'Welcome' ); and in your no.lang.php you will have define ( 'LABEL_WELCOME', 'Velkommen' );. The good thing about using constant instead of variables is that they're always available in your script. So you don't need to inject or global them. In comparison to both arrays and variables, they are faster because of the way PHP handles them -- they take up less space in the memory. The drawback is that you can't pass them to the translator, so again you need a PHP developer. Also syncing between files could be a bit of pain again.

The other option is to have a Function or Class/Method to retrieve the translation. It will be less efficient -- however I don't think you need that kinda of micro-optimization at all, but the added advantage is that you can apply a custom logic while retrieving the translation. For example imagine someday your beloved project manager comes to you asking if you could covert all of those texts to uppercase. You can't refuse to do it, so having a function in between could help you a lot when you need to apply/change a pattern to all of your translations.

To wrap it up:

  1. Think about how translators can use it and come up with a working-KISS solution for them.

  2. Think about how you can stay sync between different languages.

  3. Think about if the same translation might be needed, let say for the mobile app. A cross platform solution could save your time. Don't dismiss JSON and ordinary Databases.

  4. Think about how you can apply/change the logic when retrieving the translations.

  5. Forgot about the performance. 99.9% of times you won't get that far to do micro-optimization like Constant vs. Variables vs. Function Calls. I assume your time as a developer/staff is more expensive rather than a processor time.

Update #1

My goodness, I post an answer for a year old question. Why no one is saying anything here? We need alerts for these cases.

Update #2

On the right side it says it's a year old question; However it's been asked two years ago! Everything is misleading here!

Related Topic