0

I'm localizing my site and using an array to store language strings. This is my architecture:

One file per language, for example: language_en.php for English, language_fr.php for French that gets included in the beginning of the script.

In each language file I have an array (consisting of about 2000 elements):

static $phrases = array();
$phrases['del']  = 'Remove';
...

Then I have a function:

function phrase($key) {
   global $phrases;
   return $phrases[$key]
}

Where-ever I need a localized string further in my app, I use:

phrase('del');

Couple of questions around this architecture:

  1. $language is global, does this have negative performance effects when the array gets larger and larger?
  2. Would it be better to add the array right into the phrase() function and avoid it to be global?
bart
  • 14,958
  • 21
  • 75
  • 105
  • There's nothing wrong regarding that approach. Even though arrays grow up sometime, it'll not hurt performance. Also, it's okay using global in this case. – felipsmartins Nov 14 '15 at 09:30

1 Answers1

1

There's nothing wrong regarding that approach.
Even though arrays grow up sometime, it shouldn't cause performance penalty. Also, it's okay using global in this case.
If you've "global paranoid" you can use closures to create a copy of global variable, like this:

$phrase = function() use ($phrases) 
{    
    return $phrases[$key];
};

I advise you to use symfony/translation component in order to manage the translation catalogs. Symfony trabslations is a great component.

Maybe you want take a look on tis answer (mine): Best way to organize your localized translation file

Community
  • 1
  • 1
felipsmartins
  • 13,269
  • 4
  • 48
  • 56