File: D:/HostingSpaces/SBogers64/klimroosbudel.nl/wwwroot/kms/lib/general/language_handler.class.php
<?php
/*
* languageHandler.class.php
* Created by Komma Mediadesign.
* Author: mike
* Date: 3/21/13
*/
class LanguageHandler
{
/*
* @property array options
* Languages used in this website.
* What files match with what languages
*/
private $_options = [];
/*
* @property Language lang
* Current language.
*/
private $_lang = null;
public function __construct()
{
// set options
$this->_options = ['nl'=> DOCUMENT_ROOT.'config/lang/nl.php',
'en'=> DOCUMENT_ROOT.'config/lang/en.php',
];
}
/*
* Set language
*/
public function set($lang)
{
$lang = strtolower($lang);
if (array_key_exists($lang, $this->_options)) {
$object = 'Language_'.ucfirst($lang);
if (! class_exists($object)) {
include DOCUMENT_ROOT.'/config/lang/'.$lang.'.php';
}
$this->_lang = new $object();
}
}
/*
* Get current Language
* 1. Include Language Class
* 2. Get the array
* 3. Return to the user
*/
public function get()
{
return $this->_lang->get();
}
/*
* Get current Language
* 1. Include Language Class
* 2. Get the array
* 3. Return urls to the user
*/
public function getUrls()
{
return $this->_lang->get('url');
}
/*
* Display the menu
*/
public function displayMenu()
{
$output = '';
foreach (array_keys($this->_options) as $country) {
$stringUrl = substr($_SERVER['REQUEST_URI'], 1);
$urls = explode('/', $stringUrl);
$kms = substr(SITE_ROOT, -4, 3) == 'kms';
//Do we need translation?
if ($country != URL_LANG) {
$i = 0;
$url = '';
//first search for all "keys" that compare with the url part
$keys = [];
foreach ($urls as $urlPart) {
if (! empty($urlPart)) {
if (($kms && $i > 1) || (! $kms && $i > 0 && $i != 2)) { // Laatste "of" is een extension keystud
// get the key that compares with the current url-part
$urlPart = $this->convert($urlPart);
}
$keys[] = $urlPart;
$i++;
}
}
// load the new count and get all translations
$this->set($country);
$convertedUrls = $this->_lang->get('url');
$i = 0;
foreach ($keys as $key) {
if ($i == 0 && $kms) {
$urlPart = 'kms';
} elseif (($kms && $i == 1) || (! $kms && $i == 0)) {
$urlPart = $country;
} else {
// use the key to translate the url-part to a new url-part
isset($convertedUrls[$key]) ? $urlPart = $convertedUrls[$key] : $urlPart = $key;
}
$url .= '/'.$urlPart;
$i++;
}
$url .= '/';
} else {
$url = '/'.$stringUrl;
}
$output .= ' <a href="'.$url.'" class="flag '.$country;
if ($country == URL_LANG) {
$output .= ' active';
}
$output .= '">';
if (! $kms) {
$output .= $country;
}
$output .= '</a>';
}
return $output;
}
/*
* Convert URL_SUB to a method within a controller
*/
public function convert($key)
{
$urls = $this->_lang->get('url');
foreach ($urls as $converted => $url) {
if (is_array($url)) {
if (in_array($key, $url)) {
return $converted;
}
} elseif ($url == $key) {
return $converted;
}
}
return $key;
}
}