I'm quite new to php, and I was thinking about making languages change on a website by requiring a file with variables depending on a selected language.
For now, its just 2 languages, but will be more. Variables with blocks in English are stored in content.php file, and variables in Russian - in ru.php.
Here is my html form:
<div id="language">
<form method="post" action="lang.php" onchange="this.form.submit()">
<select name="language">
<option value="en">en</option>
<option value="ru">ru</option>
</select>
</form>
</div>
And here is the PHP I was trying to use (but no success):
$option = $_POST['language'];
if ($option == 'ru')
{
require('ru.php');
}
if ($option =='en')
{
require('content.php');
}
Please let me know if you know how I can make it work.
</div>
For persistence of the user's language choice throughout your website, storing their preference for a period of time, you have 3 main options:
I like $_SESSIONs for a variety of reasons. Learn more about sessions here.
The following PHP would have to be required at the top of every page (let's call it requires.php
). It could look like this:
requires.php
$timeout = 60*60*3; // 3 hour maximum session timeout
session_start([
'name' => 'my_groovy_session',
'gc_maxlifetime' => $timeout, // server session lifetime
'cookie_lifetime' => $timeout, // client cookie lifetime
]);
// set language if empty
if (empty($_SESSION['lang'])) $_SESSION['lang'] = 'en';
// choose file
if (is_file('languages/'. $_SESSION['lang'] .'.php')) {
require_once 'languages/'. $_SESSION['lang'] .'.php';
} else {
require_once 'languages/en.php';
}
Your lang.php
would just be something like this:
lang.php
$_SESSION['lang'] = $_POST['language'];
This method adds all the translations into the same directory. They could look something like this:
en.php
$words = [
'welcome' => 'Welcome to so-and-so',
'email' => 'Please email us at blah@blah.com',
'close' => 'Close',
];
ru.php
$words = [
'welcome' => 'Добро пожаловать в такое-то',
'email' => 'Пожалуйста, напишите нам на blah@blah.com',
'close' => 'Закрыть',
];
And then in any PHP file that used requires.php
, you could then output the words in the user's preferred language:
echo $words['welcome'];
Just be sure leave the array keys
themselves (welcome, email, close) un-translated.
Note: The above uses the short array syntax []
for arrays. If you're using PHP version < 5.4 you'll need to use array()
.
Below code structure works fine. You can add your required files to make it work.
form.html
I have changed the position of onchange="this.form.submit()"
<div id="language">
<form method="post" action="lang.php" >
<select name="language" onchange="this.form.submit()">
<option value="en">en</option>
<option value="ru">ru</option>
</select>
</form>
</div>
lang.php
<!DOCTYPE html>
<html>
<body>
<?php
$option = $_POST['language'];
if ($option == 'ru')
{
require('ru.php');
echo($word);
}
if ($option =='en')
{
require('en.php');
echo($word);
}
?>
</body>
</html>
en.php
<!DOCTYPE html>
<html>
<body>
<?php
$word = "i am english";
?>
</body>
</html>
ru.php
<!DOCTYPE html>
<html>
<body>
<?php
$word = "i am russian";
?>
</body>
</html>