Are there any drawbacks using variables against arrays for localization of a project in PHP?
E.g:
en.php
$string1 = "Open";
de.php
$string1 = "Öffnen";
Are there any drawbacks using variables against arrays for localization of a project in PHP?
E.g:
en.php
$string1 = "Open";
de.php
$string1 = "Öffnen";
I see the drawback, that if you declare 1000 variables you have 1000 zval containers with 1000 values. If you would declare it as an array like:
en.php :
$translations = array (
'yes' => 'yes',
'no' => 'no'
);
... then just a single zval container is required (with a larger value portion of course). This will help to keep PHP's internal symbol tables smaller and save memory. If you ask me, use an array.
A more elaborated alternative would be to use the gettext
extension
Just for completeness, another drawback would be that using variables for translations could lead to almost any imaginable variable name, and therefore is highly prone for namespace pollution. If you should use variables (for whatever reason, I don't see), then you have to "namespace" them to avoid this, like:
$translation_yes = 'yes';
$translation_no = 'no';
...