gosho_ot_pochivka gosho_ot_pochivka - 5 months ago 40
Ini Question

Best way to manage text displayed to users in PHP

Ok for sure this has been asked and answered already but i somehow can't find a proper tutorial.
I want to keep the text displayed to users somewhere else and to prevent my code from becoming too large and unreadable.
My site won't be internationalized. I just want to have some kind of file with key-value structure and get the text from there. I want to keep the text in files, not in the database as some tutorials suggest.
I found a solution which will work but i am not sure whether this is a good approach.
I am thinking of using parse_ini_file and to keep my texts in .ini file. Is there something wrong with this approach? Could you suggest something better?


I put all language data in arrays. Its easy and also we can add multi-language support


 return array(  
   'index' => 'Homepage',
   'feedback' => 'Feedback'
   'logout' => 'Logout from profile',


 return array(
   'logout' => 'Выйти из профиля',

Then we can load languages:

$lang = include('lang/en.php');
   $lang = array_merge($lang, include('lang/ru.php'));

After all it $lang will look like:

   [index] => Homepage
   [feedback] => Feedback
   [logout] => Выйти из профиля

And we can very simple use it:

function __($name) {
    global $lang;
    return $lang[$name];

Somewhere in the site template: