Skip to content

Instantly share code, notes, and snippets.

@shahid249
Created March 26, 2018 15:19
Show Gist options
  • Save shahid249/977af11581dc81856f70c4953c3cb504 to your computer and use it in GitHub Desktop.
Save shahid249/977af11581dc81856f70c4953c3cb504 to your computer and use it in GitHub Desktop.
The Simple Custom Translator For Website Using Cookie | PHP
<?php
$cookie_name = "language"; //Cookie Name
$default_language = "en-us"; //Set here the default language code
$available_langs = array('en-us','bn'); //The Language we support list
//This will activate when language request submitted by users.
if(isset($_POST['lang'])){
// check if the language is available in array
if(in_array($_POST['lang'], $available_langs))
{
$cookie_value = $_POST['lang']; //This will set the cookie value
$lang_value = $_POST['lang']; //This will put temporary language code to output, to run it for the first time.
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); //Setting cookie, value 86400 = 1 day, 30 = 30 days.
}
}
//This will activate when no language code is submited, Or visiting website for first time, Or cookie expired.
else if (!isset($_COOKIE[$cookie_name])) {
$cookie_value = $default_language; //default language, This will set the cookie value
$lang_value = $default_language; //This will put temporary language code to output, to run it for the first time
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); //Setting cookie, value 86400 = 1 day, 30 = 30 days.
}
//This will activate when cookie is found or set.
else {$lang_value = $_COOKIE[$cookie_name];}
// This will include the page according to language codes example-> en-us.php
include($lang_value.'.php');
?>
<html>
<head>
</head>
<body>
<h1><?php echo $lang['valuel']; ?></h1>
<h3><?php echo $lang['value2']; ?></h3>
<p><?php echo $lang['value3']; ?></p>
<form action="" method="POST" id="changelangue">
<select name="lang" onchange="changelangue.submit()">
<option value="">Slect Language</option>
<option value="en-us">EN-US</option>
<option value="bn">BN</option>
</select>
</form>
</body>
</html>
<?php
// The value
$lang['valuel'] = 'Hello';
$lang['value2'] = 'Its Works';
$lang['value3'] = 'The Simple Custom Translator';
?>
<?php
// The value
$lang['valuel'] = 'হ্যালো';
$lang['value2'] = 'এটি কাজ করছে';
$lang['value3'] = 'সহজ কাস্টম অনুবাদক';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment