Skip to content

Instantly share code, notes, and snippets.

@farithadnan
Last active March 21, 2021 00:56
Show Gist options
  • Select an option

  • Save farithadnan/711cb182b39fa3bd97c3890731803a7b to your computer and use it in GitHub Desktop.

Select an option

Save farithadnan/711cb182b39fa3bd97c3890731803a7b to your computer and use it in GitHub Desktop.
Simple multi-language features with PHP. This feature will let user change the language of a page based on their preference,

This is just an example on how to implement this feature to a page, for this purpose i implemented this feature to my registration page.

Requirement: 1. Target files 2. Language files

es.php, en.php, my.php -> is a language files, it contained translated description for my registration page registration.php -> is where the translation functionality will be use. PHP script will invoked the language files, based on the user languages preferences.

<?php
// English (Default) translation for registration page
const _REGISTER = "Register";
const _USERNAME = "Enter your username";
const _EMAIL = "Enter your email";
const _PASSWORD = "Enter your password";
?>
<?php
// Spanish translation for registration page
const _REGISTER = "Registrate";
const _USERNAME = "Nombre de Usuario";
const _EMAIL = "Tu Correo";
const _PASSWORD = "Tu Clave";
?>
<?php
// Malay translation for registration page
const _REGISTER = "Daftar Akaun";
const _USERNAME = "Masukkan Nama Anda";
const _EMAIL = "Masukkan Email Anda";
const _PASSWORD = "Masukkan Katalaluan anda";
?>
<?php
// ----------------------------------------------------------//
// SCRIPT THAT WILL BE EXECUTING THE TRANSLATION FUNCTION //
// ----------------------------------------------------------//
// SETTING LANGUAGE VARIABLE
if(isset($_GET['lang']) && !empty($_GET['lang']))
{
$_SESSION['lang'] = $_GET['lang'];
// WILL RELOAD THE PAGE IF THE SELECTION IS CHANGE
if(isset($_SESSION['lang']) && $_SESSION['lang'] != $_GET['lang']){
echo "<script type='text/javascript'>location.reload();</script>";
}
}
// IF THE SESSION SET IT WILL BE INCLUDING LANGUAGE BASED ON THE USER SELECTION
if(isset($_SESSION['lang'])){
include "includes/languages/" . $_SESSION['lang'] . ".php";
} else {
include "includes/languages/en.php";
}
?>
<!-- SELECTION TAG FOR CHOOSING LANGUAGE -->
<div class="container">
<!-- // THIS IS WHERE USER MAKE THE SELECTION, BY USING ONCHANGE EVENT-HANDLER IT WILL THEN PROCEED
// SUBMITTING THE FORM THRU JS SCRIPT (BELOW), BY USING SUBMIT() FUNCTION, AFTER IT HAVE DETECTED
// THE CHANGE (SELECTED) IN THE OPTION TAG. -->
<form method="get" class="navbar-form navbar-right " id="language_form" k>
<div class="form-group">
<select name="lang" class="form-control" onchange="changeLanguage()">
<option value="en" <?php if(isset($_SESSION['lang']) && $_SESSION['lang'] == 'en'){ echo "selected"; } ?>>English</option>
<option value="es" <?php if(isset($_SESSION['lang']) && $_SESSION['lang'] == 'es'){ echo "selected"; } ?>>Spanish</option>
<option value="my" <?php if(isset($_SESSION['lang']) && $_SESSION['lang'] == 'my'){ echo "selected"; } ?>>Malay</option>
</select>
</div>
</form>
<!-- REGISTRATION FORM START -->
<!-- IT'S REQUIRE TO CHANGE THE PLACEHOLDER IN THE INPUT VALUE, JUST LIKE THE EXAMPLE BELOW. -->
<section id="login">
<div class="container">
<div class="row">
<div class="col-xs-6 col-xs-offset-3">
<div class="form-wrap">
<h1><?php echo _REGISTER; ?></h1>
<form role="form" action="registration.php" method="post" id="login-form" autocomplete="on">
<div class="form-group">
<label for="username" class="sr-only">username</label>
<input type="text" name="username" id="username" class="form-control" placeholder="<?php echo _USERNAME; ?>" value="<?php echo isset($username) ? $username : ''; ?>">
</div>
<div class="form-group">
<label for="email" class="sr-only">Email</label>
<input type="email" name="email" id="email" class="form-control" placeholder="<?php echo _EMAIL; ?>" value="<?php echo isset($email) ? $email : ''; ?>">
</div>
<div class="form-group">
<label for="password" class="sr-only">Password</label>
<input type="password" name="password" id="key" class="form-control" placeholder="<?php echo _PASSWORD; ?>" >
</div>
<input type="submit" name="register" id="btn-login" class="btn btn-primary btn-lg btn-block" value="<?php echo _REGISTER; ?>">
</form>
</div>
</div> <!-- /.col-xs-12 -->
</div> <!-- /.row -->
</div> <!-- /.container -->
</section>
<hr>
<!-- REGISTRATION FORM END -->
<!-- SUBMITING FORM WITH JS - START -->
<script>
// IT WILL BE SUBMITING THE FORM (METHOD: GET) USING THE SUBMIT FUNCTIONALITY
function changeLanguage(){
document.getElementById('language_form').submit();
}
</script>
<!-- SUBMITING FORM WITH JS - END -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment