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


$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?

$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!!

  • 2
    have you considered gettext ? Apr 9, 2012 at 9:49
  • @sagarchalise i have looked into it, but i want to build my own method, i,e any of the above two. So, i would like to know which method works faster and which is more reliable! :)
    – sree
    Apr 9, 2012 at 11:14
  • If you want to know in terms of speed, just do some test cases. What runs faster, 1M of times the first or the second method? Jan 3, 2014 at 1:02
  • 2
    Don't reinvent a wheel that works fine.
    – gbjbaanb
    Jul 2, 2014 at 22:35

2 Answers 2


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!

  • u look like a time traveller back into time :) tnx for the answer though!
    – sree
    Apr 8, 2014 at 16:29
  • 3
    @sree Yes! I'm coming from April 4, 2014! If you buy some Facebook or WhatsApp shares in 2012, you won't need to write PHP code in 2014!
    – Mahdi
    Apr 8, 2014 at 16:40
  • 1
    rofl agreed !!!
    – sree
    Apr 8, 2014 at 16:53

PHP has great built-in possibilities for localization. The most common way seems to be gettext - this tutorial shows how it's used.

Since PHP 5.3 there's also intl which offers a lot more features (to format numbers, dates and currencies depending on the locale for example).

  • Thank you for your answer , but, could i know from the above which method is better to construct in terms of speed and reliability.
    – sree
    Apr 9, 2012 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.