patternphpMinor
Multilanguage class that detects and sets language
Viewed 0 times
multilanguagelanguagethatanddetectssetsclass
Problem
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:
If I'm getting a language from
-
Almost the same check is made in the second
-
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
```
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'])) {
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 thecurrentLangproperty 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 if COOKIE['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'])) {
Solution
The class is small and simple but I just made some changes for better reading.
If you want, you can move each if statedments in the construct to an own function but in this case i would leave it like it is.
Hope this will help you.
- I'm a fan of exit early so if an if statement is checked and there is nothing more to do, exit the function. the rest of the function is not necessary.
- I also moved the duplicated check
in_array($lang, $this->allLangs) && $this->_langFileExist($lang)in an own function, DRY.
- Next thing is adding typehints for your IDE to detect your variable methods.
- The last thing is to add a getter for $currentLang, I like this more than get a public attribute.
If you want, you can move each if statedments in the construct to an own function but in this case i would leave it like it is.
Hope this will help you.
_langIsAvailable($_GET['lang'])) {
setcookie("lang", $_GET['lang'], time() + 3600 * 24 * 30);
// store current language
$this->currentLang = $_GET['lang'];
return;
}
// get lang from cookie
if(isset($_COOKIE['lang']) && $this->_langIsAvailable($_COOKIE['lang'])) {
// store current language from COOKIE
$this->currentLang = $_COOKIE['lang'];
return;
}
// if no lang is set
if(empty($this->currentLang)) {
// get first two letters from string
$browserLang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
// tries to detect browser language, if browser language exists in possible languages array and if language file exists
if($this->_langIsAvailable($browserLang)) {
// set current language from browser language
$this->currentLang = $browserLang;
return;
}
}
// set it to english by default
$this->currentLang = 'en';
}
/**
* @return string
*/
public function getCurrentLang()
{
return $this->currentLang;
}
/**
* @param string $lang
* @return bool
*/
private function _langIsAvailable($lang)
{
return in_array($lang, $this->allLangs) && $this->_langFileExist($lang);
}
/**
* @param string $lang
* @return bool
*/
private function _langFileExist($lang)
{
return file_exists('langs/'.strtolower($lang.'.php'));
}
}Code Snippets
<?php
/**
* Class Langs
*/
class Langs
{
/** @var string $currentLang for detecting current language */
private $currentLang = '';
/** @var array $allLangs all possible languages */
private $allLangs = ['lt', 'en'];
/**
* Langs constructor.
*/
public function __construct()
{
// get lang from url
if(isset($_GET['lang']) && $this->_langIsAvailable($_GET['lang'])) {
setcookie("lang", $_GET['lang'], time() + 3600 * 24 * 30);
// store current language
$this->currentLang = $_GET['lang'];
return;
}
// get lang from cookie
if(isset($_COOKIE['lang']) && $this->_langIsAvailable($_COOKIE['lang'])) {
// store current language from COOKIE
$this->currentLang = $_COOKIE['lang'];
return;
}
// if no lang is set
if(empty($this->currentLang)) {
// get first two letters from string
$browserLang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
// tries to detect browser language, if browser language exists in possible languages array and if language file exists
if($this->_langIsAvailable($browserLang)) {
// set current language from browser language
$this->currentLang = $browserLang;
return;
}
}
// set it to english by default
$this->currentLang = 'en';
}
/**
* @return string
*/
public function getCurrentLang()
{
return $this->currentLang;
}
/**
* @param string $lang
* @return bool
*/
private function _langIsAvailable($lang)
{
return in_array($lang, $this->allLangs) && $this->_langFileExist($lang);
}
/**
* @param string $lang
* @return bool
*/
private function _langFileExist($lang)
{
return file_exists('langs/'.strtolower($lang.'.php'));
}
}Context
StackExchange Code Review Q#121737, answer score: 2
Revisions (0)
No revisions yet.