I have just started to use PHP OOP and I would like to write a class to make a multi-language website. I started from this but I wanted to use OOP so I came up with this:
Language.php
<?php
class Language {
private $UserLng;
private $langSelected;
public $lang = array();
public function __construct($userLanguage){
$this->UserLng = $userLanguage;
}
public function userLanguage(){
switch($this->UserLng){
/*
------------------
Language: English
------------------
*/
case "en":
$lang['PAGE_TITLE'] = 'My website page title';
$lang['HEADER_TITLE'] = 'My website header title';
$lang['SITE_NAME'] = 'My Website';
$lang['SLOGAN'] = 'My slogan here';
$lang['HEADING'] = 'Heading';
// Menu
$lang['MENU_LOGIN'] = 'Login';
$lang['MENU_SIGNUP'] = 'Sign up';
$lang['MENU_FIND_RIDE'] = 'Find Ride';
$lang['MENU_ADD_RIDE'] = 'Add Ride';
$lang['MENU_LOGOUT'] = 'Logout';
return $lang;
break;
/*
------------------
Language: Italian
------------------
*/
case "it":
$lang['PAGE_TITLE'] = 'Il titolo della mia pagina';
$lang['HEADER_TITLE'] = 'Il mio titolo';
$lang['SITE_NAME'] = 'Il nome del mio sito';
$lang['SLOGAN'] = 'Uno slogan';
$lang['HEADING'] = 'Heading';
// Menu
$lang['MENU_LOGIN'] = 'Entra';
$lang['MENU_SIGNUP'] = 'Registrati';
$lang['MENU_FIND_RIDE'] = 'Trova gruppi';
$lang['MENU_ADD_RIDE'] = 'Aggiungi gruppo';
$lang['MENU_LOGOUT'] = 'Esci';
return $lang;
break;
/*
------------------
Default Language
------------------
*/
default:
$lang['PAGE_TITLE'] = 'My website page title';
$lang['HEADER_TITLE'] = 'My website header title';
$lang['SITE_NAME'] = 'My Website';
$lang['SLOGAN'] = 'My slogan here';
$lang['HEADING'] = 'Heading';
// Menu
$lang['MENU_LOGIN'] = 'Login';
$lang['MENU_SIGNUP'] = 'Sign up';
$lang['MENU_FIND_RIDE'] = 'Find Ride';
$lang['MENU_ADD_RIDE'] = 'Add Ride';
$lang['MENU_LOGOUT'] = 'Logout';
return $lang;
break;
}
}
}
index.php
<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);//Detecting Default Browser language
$language = New Language($lang);
$langArray = array();
$langArray = $language->userLanguage();
?>
<div class="cssmenu">
<ul>
<li> class="active"><a href="/login"><?php echo $langArray['MENU_LOGIN']?></a></li>
<li><a href="/rides"><?php echo $langArray['MENU_FIND_RIDE']?></a></li>
<li><a id ="btnShow"><?php echo $langArray['MENU_ADD_RIDE']?></a></li>
<li><a href="/logout.php"><?php echo $langArray['MENU_LOGOUT']?> </a></li>
<li><a href="/register"><?php echo $langArray['MENU_SIGNUP']?></a></li>
</ul>
</div>
While this works perfectly, I am not sure this is the correct way to do it. I didn't find many tutorials on how to do this using OOP so I have the following doubts:
- Is this a correct way to do it?
- is this code maintainable?
- would it make more sense to create a table in the database with all the different languages?
- I am still struggling to understand "abstract classes". Would this be the case to create an abstract class
language.php
and then extend(I hope this is the correct terminology) that class with other languages class (english.php
,italian.php
etc).