I have tried to use templates to be able to make a multilingual web site, so i created a folder called languages containing en.php and fr.php
here is the code for en.php:
<?php
class Index {
public $filename;
public $assigned_vars = array();
public function set($key, $value) {
$this -> assigned_vars[$key] = $value;
}
public function display() {
if (file_exists($this -> filename)) {
$output = file_get_contents($this -> filename);
foreach ($this->assigned_vars as $key => $value) {
$output = preg_replace('/{' . $key . '}/', $value, $output);
}
echo $output;
} else {
echo "*** Missing template ***";
}
}
}
$index = new Index;
$index -> filename = "templates/index1.php";
$index -> set('page_title', "Welcome");
$index -> set('first_name', "Welcome to our website.");
$index -> display();
?>
and here is the code for fr.php:
<?php
class Index {
public $filename;
public $assigned_vars = array();
public function set($key, $value) {
$this -> assigned_vars[$key] = $value;
}
public function display() {
if (file_exists($this -> filename)) {
$output = file_get_contents($this -> filename);
foreach ($this->assigned_vars as $key => $value) {
$output = preg_replace('/{' . $key . '}/', $value, $output);
}
echo $output;
} else {
echo "*** Disparus modèle ***";
}
}
}
$index = new Index;
$index -> filename = "templates/index1.php";
$index -> set('page_title', "Bienvenue");
$index -> set('first_name', "Bienvenue sur notre site.");
$index -> display();
?>
then i created a folder called templates and created a file called index1.php
and here is index1.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{page_title}</title>
</head>
<body>
{first_name}
<br/>
<a href="?lang=en-us">english</a>
<a href ="?lang=fr">French</a><br />
<?php
echo strftime("Y", time());
?>
</body>
</html>
and then i created the regular index.php
and here is the code for index.php:
<?php
if (isset($_GET['lang'])) {
setcookie('language_test', $_GET['lang'], time() + (60 * 60 * 24 * 7));
if ($_GET['lang'] == "fr") {
require ('languages/fr.php');
} else {
require ('languages/en.php');
}
} else {
require ('languages/en.php');
}
?>
here is my problem when i try to show index.php in my browser it works fine but the php code in index1.php doesn’t show any of this strftime();, but when i try to view the source of this
index.php
it’s showing me this:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome</title>
</head>
<body>
Welcome to our website.
<br/>
<a href="?lang=en-us">english</a>
<a href ="?lang=fr">French</a><br />
<?php
echo strftime("Y", time());
?>
</body>
</html>
thanks in advance. and sorry for being so long and sorry for my awful english.
When you are invoking
file_get_contents($this -> filename);it just convertsstrftime("Y", time());part as string. So it does not execute.In the
index1.phpyou should use{time}instead ofSo It’ll look like
Now add time in
en.phporfr.phplike bellowIts better not to re-invent a template library. There are plenty available. See this blog post top 25 php template engines