[PHP] Caching my localization

Joined
Jul 26, 2006
Messages
3,626
Reaction score
1,006
So this project of mine involves localization, currently only dutch and english but I'm designing it for more languages in future.

All the localization strings are in a database table with fields "key", "en" and "nl" for the key, english translation and dutch translation respectively. In an include which is included in every page, I have this:
PHP:
$CONFIG["LANG"] = array();

$query = $db->query("SELECT * FROM `localization`");
while($result = $query->fetch_assoc()) {
	$CONFIG["LANG"][key($result)] = $result[$_SESSION["SELECTED_LANG"]];
}

This fills up an array ($CONFIG["LANG"]) for use in the currently opened file.

Now as this script will be executed every time the page is loaded for anyone, is it worth caching the entire localization table so the entire table (with probably a few hundred sentences in the end) doesn't have to be queried from the MySQL database every time but is just somewhere on disk?
 
I was thinking of just putting serialize(data pulled from table); in a text file (file_put_contents("lang.en.txt") or something like that) and then unserializing the file when loading it back into the PHP array like so: $CONFIG["LANG"] = unserialize(file_get_contents("lang.en.txt")); - I want to keep it simple, without an extensive caching class, just a few functions of my own.
 
I was thinking of just putting serialize(data pulled from table); in a text file (file_put_contents("lang.en.txt") or something like that) and then unserializing the file when loading it back into the PHP array like so: $CONFIG["LANG"] = unserialize(file_get_contents("lang.en.txt")); - I want to keep it simple, without an extensive caching class, just a few functions of my own.

I know few frameworks that sucessfully utilize method ( cakePHP would be one of'em ) you just described, sometimes simple is best, I would say that's that case.

Extensive caching class should be rather reffered to how to manage that cache, not a method to create it - so it should be more like the class should check if the cache exists, is it outdated etc.
 
You could rewrite it to:

PHP:
$db->query("SELECT * FROM `localization` WHERE key = " . $_SESSION["SELECTED_LANG"]);

That's not really the point, the point is to minimize database queries through caching. Also, `key` holds the key to a certain translation e.g. a table entry looks like this:
Code:
key = "LOGIN_FORM_INVALID_USERNAME"
    en = "You have entered an invalid username"
    nl = "Je hebt een ongeldige gebruikersnaam ingevuld"
etc...
 
Separate php files for each language would be the best option imo.

lang.en.php
lang.nl.php

then require_once( "lang/lang." . $_SESSION["SELECTED_LANG"] . ".php" );

format the language files like so:
PHP:
$languageConfig = array(
  "LOGIN_FORM_INVALID_USERNAME" => "You have entered an invalid username",
  "LOGIN_FORM_INVALID_PASSWORD" => "Etc..",
);

to use (after requiring it)
PHP:
echo $languageConfig["LOGIN_FORM_INVALID_USERNAME"];

The hardest part will be making the language files to begin with but with a bit of data manipulation you can easily do it (I did it for work once - not for languages but some other stuff)

All the best,
Richard Komakech.
 
I had an array like that but it became a pain to manage so I wrote a small admin panel to handle adding/changing keys. And to be quite frank, I just wanted to try my hand at caching something for the first time :P

But thanks nonetheless!
 
Last edited:
I had an array like that but it became a pain to manage so I wrote a small admin panel to handle adding/changing keys. And to be quite frank, I just wanted to try my hand at caching something for the first time :P

But thanks nonetheless!

My concept is caching.. Anything in which it is on file is a cache, when you display an image on a site that was uploaded locally to the site is a cache of that image.

Also it's very easy to edit arrays with a small admin panel... It's an array, display data -> save data.

it's called var_export, when you use it while manipulating a file it'll save it exactly how the code would read it, making a simple panel the answer the all your problems :)

All the best,
Richard Komakech.
 
Back