here is my dilemma :
I am creating a multilingual platform. In that purpose I created json files containing all the translated text for each language.
Now when the user land on a their page I read from that file and store the array of translations in the $_SESSION variable such as
$_SESSION['website_text'] = json_decode(file_get_contents("content_".$language.".json"), true);
Then everytime I want to echo text on the views I access the element from the session array :
$text = $_SESSION['website_text']['paragraph2_headline'];
Now I am wondering, since the $_SESSION is stored on the server. Is it faster to read from the session like I do or to read everytime from the file and decode the json?
The second option would go like that :
$website_text = json_decode(file_get_contents("content_".$language.".json"), true);
$text = $website['paragraph2_headline'];
Thank you all for your help!
Most likely it’s faster when pulling the data from
$_SESSION, but$_SESSIONis not a good place to store localization data in because it will end up being duplicated for each user.When you retrieve the strings from
$_SESSIONthen PHP has to read the data from the session file (which it already does to read any other session data, so the cost of opening the file is somewhat amortized) and rununserializeon it; if you retrieve it from a JSON file then it has open the file, read it and runjson_decode.unserializeshould be faster thanjson_decode, but please don’t quote me on that.If you are interested in making this fast, it would be better to read the strings directly from a PHP file where they are stored as an array:
Even if your localization files are in JSON it would be very easy to “compile” the JSON into PHP and use the PHP code as a cache: