I have just starter 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;
}
}
}
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 perfectly works, 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:
You are very much on track, except for one bit, your data and code are intertwined, which is not a good form of design. Different people use various approaches for handling i18N. I prefer using .ini files, you can have the data in different INI files and even add sections if you want. Using your example, you can do:
-- en.ini
PAGE_TITLE = 'My website page title'
HEADER_TITLE = 'My website header title'
SITE_NAME' = 'My Website'
SLOGAN = 'My slogan here'
HEADING = 'Heading'
-- it.ini
PAGE_TITLE = 'Il titolo della mia pagina'
HEADER_TITLE = 'Il mio titolo'
SITE_NAME = 'Il nome del mio sito'
SLOGAN = 'Uno slogan'
HEADING = 'Heading'
Then your code can be rewritten as :
<?php
class Language {
private $UserLng;
private $langSelected;
public $lang = array();
public function __construct($userLanguage){
$this->UserLng = $userLanguage;
//construct lang file
$langFile = '/path/to/ini/files/'. $this->UserLng . '.ini';
if(!file_exists($langFile)){
throw new Execption("Language could not be loaded"); //or default to a language
}
$this->lang = parse_ini_file($langFile);
}
public function userLanguage(){
return $this->lang;
}
}
This is the right way.
But, you can create some language files
- english.php
define('ABOUT_US', 'About us');
....
- french.php
....
it may be more optimizer.
Create a table in the database with all the different languages is an idea (vbulletin use this way), but it may take so much system resource.