I have been learning object oriented programming for quite a while. I understand many concepts about it but I quickly realized that I made a big mistake just reading theory and not doing any actual coding . There was no practise at all and when I started to write some code, I felt it was harder to think and come up with great ideas of what exactly my code should do before I blindly go to write some code.
I'm trying to improve my multilanguage class and this is how it works:
- It first checks if a language from the
lang=
GET parameter is set and if that value exists in possible languages array. I then set that language to thecurrentLang
property and cookie to remember that language for a longer time. If I'm getting a language from$_GET
, then I assume that the user manually switched to another language. Almost the same check is made in the second
if
statement, but this time checking ifCOOKIE['lang']
is set. This condition is needed to check if the language was set previously.If $_GET['lang'] or $_COOKIE['lang'] is not set then I assume that no language was chosen manually nor last time user visited page, inside that condition I check if current browser language value can be found in possible languages array if so then I set current language from browser language string. If language from browser
HTTP_ACCEPT_LANGUAGE
can't be found then I set current language to 'English' as a default option.
class Langs {
// for detecting current language
public $currentLang = '';
// all possible languages
private $allLangs = ['lt', 'en'];
public function __construct() {
// get first two letters from string
$browserLang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
// if language in url is set and if that language value exists in possible languages array and if language file exists
if(isset($_GET['lang']) && in_array($_GET['lang'], $this->allLangs) && $this->_langFileExist($_GET['lang'])) {
setcookie("lang", $_GET['lang'], time() + 3600 * 24 * 30);
// store current language
$this->currentLang = $_GET['lang'];
// if language in cookie is set and if that language value exists in possible languages array and if language file exists
} elseif(isset($_COOKIE['lang']) && in_array($_COOKIE['lang'], $this->allLangs) && $this->_langFileExist($_COOKIE['lang'])) {
// store current language from COOKIE
$this->currentLang = $_COOKIE['lang'];
// if GET['lang'] or COOKIE['lang'] is not set
} elseif(!isset($_GET['lang']) || !isset($_COOKIE['lang'])) {
// tries to detect browser language, if browser language exists in possible languages array and if language file exists
if(in_array($browserLang, $this->allLangs) && $this->_langFileExist($browserLang)) {
// set current language from browser language
$this->currentLang = $browserLang;
} else {
// if browser lang detection failed or if that language file doesnt exist
// set it to english by default
$this->currentLang = 'en';
}
}
}
private function _langFileExist($langFile) {
if(file_exists('langs/'.strtolower($langFile.'.php')))
return true;
}
}
$langs = new Langs();
echo 'Current Language: '.$langs->currentLang;
require('langs/'.$langs->currentLang.'.php');
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $lang['title']; ?></title>
</head>
<body>
<h1><?php echo $lang['heading']; ?></h1>
<p>Description: <?php echo $lang['description']; ?></p>
<footer><?php echo $lang['copyright']; ?></footer>
</body>
</html>
I store language files in 'langs' directory and using a two-letter convention like 'ru', en', 'es' to identify languages. In every language file there are language values that are stored in a $lang
array.
Finally, I instantiate the Langs
class so I can access the currentLang
property. Then, the currentLang
value is placed inside require
to load the language file.
It's a very simple class and I want to keep it as simple as possible. I just need advice from you of how I can improve that class in almost every aspect to make it faster, more efficient and readable. I'd like to hear from someone what I'm doing wrong, or what could be done better. To be more precise I want to know if I should separate logic that is inside __construct()
method in smaller pieces? Maybe you can see ways of how can I simplify if
-elseif
or possibly reduce conditions?