Language file, Best practice

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Kimmo Laine wrote:
Quoted text here. Click to load it

Unless next_page.php generates PHP, the script with this include will
only get HTML.

Quoted text here. Click to load it


    if (isset($_GET['foo'])) {
      echo '<?php echo $_GET[\'foo\']; ?>';
    } else {
      echo '<?php echo \'Not available\'; ?>';

File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot

Re: Language file, Best practice

Quoted text here. Click to load it

I think phpBB and similar use files for language for portability and
ease of updating. I would save smaller strings (like messages, link
text, page titles) into a database and retrieve the needed language on
each page. I think SQL caching should be effective here because each
language should use the same query every time.

If you think it would be faster, you can save it in both the database
and into files (into a PHP file, with array assignments), and each
time it needs to be updated the form is filled with data from the
database, and then when the form is submitted the data would be
updated in the database and the language file would be regenerated
with that new data. Then the files that use the language would include
the appropriate language file instead of querying the database each
time (useful if the table gets big and the queries start taking too
long). I've benchmarked with 100 rows of data and 100 files, and found
that retrieving an indexed row from the database is about twice as
fast as reading the file named with that index. I've been told that
the database would be even faster in comparison if there were more
files and rows.

-Mike PII

Re: Language file, Best practice

I did some more tests (I am working on a multi-lingual site also so
this is useful to me too), and it looks like reading and unserializing
a serialized language array from a file is much quicker than reading
and unserializing from a database (on my server environment). The
first two times are using PHP to create a PHP file and then
include()ing it

    [create file] => 0.0070760250091553
    [direct read] => 0.0096750259399414
    [write serial] => 0.0053510665893555
    [read serial] => 0.00060105323791504
    [insert db serialized] => 0.054249048233032
    [get db serialized] => 0.037890911102295

Here's the (PHP5) test that you can try on your server:
$db = new mysqli( 'dbhost', 'dbuser', 'dbpass' );
$db->query( 'USE `dbtable`' );

$i = 0;
$lang = array();

define( 'DATAFILE', 'benchmarking.php' );

//create file
$data = '';
$times = array( 'create file' => microtime( true ) );
$data = "<?php\n$lang=array('asdfasdfasdf'";
for( $i = 20000; $i < 20500; ++$i )
    $data .= ",'asdfewqrhbeqstnasdfasdfasdfasdf'";
$data .= ');?>';
file_put_contents( DATAFILE, $data );
$times['create file'] = microtime( true ) - $times['create file'];

unset( $data );

//direct read
$times['direct read'] = microtime( true );
require( 'benchmarking.php' );
$times['direct read'] = microtime( true ) - $times['direct read'];

//write serialized
$times['write serial'] = microtime( true );
file_put_contents( DATAFILE, serialize( $lang ) );
$times['write serial'] = microtime( true ) - $times['write serial'];

$lang = array();

//read serialized
$times['read serial'] = microtime( true );
$lang = unserialize( file_get_contents( DATAFILE ) );
$times['read serial'] = microtime( true ) - $times['read serial'];

unlink( DATAFILE );

$data = '';

//db serialized
$times['insert db serialized'] = microtime( true );
$data = serialize( $lang );
$sql_query = 'INSERT INTO `bm2` ( `data` ) VALUES ('
    ."'$data' )";
$db->query( $sql_query )
    or trigger_error( $db->error, E_USER_ERROR );
$times['insert db serialized'] = microtime( true ) - $times['insert db

$data = '';
$lang = array();

//get db serialized
$times['get db serialized'] = microtime( true );
if( !( $sql = $db->query( "SELECT `data` FROM `bm2` WHERE `id` = $db-
Quoted text here. Click to load it
    trigger_error( $db->error, E_USER_ERROR );
list( $data ) = $sql->fetch_row();
$lang = unserialize( $data );
$times['get db serialized'] = microtime( true ) - $times['get db

print_r( $times );

The table looks like:
`bm2` (collation is utf-8)
    `id` PRIMARY INT NOT NULL auto_increment
    `data` TEXT

Site Timeline