File: D:/HostingSpaces/yoda-ict/yoda-ict.nl/wwwroot/lib/general/translator.class.php
<?php
/**
* Created by Komma.pro
* User: mikevandersanden
* Date: 18/9/13
*/
class Translator
{
/*
* @property array options
* Languages used in this website.
* What files match with what languages
*/
private $_options = array();
/*
* @property Language lang
* Current language.
*/
private $_lang = null;
public function __construct($lang)
{
// set options
$this->_options = array('nl');
// Set language
$lang = strtolower($lang);
if( in_array($lang, $this->_options))
{
$object = 'Language_' . ucfirst($lang);
if( ! class_exists($object)) include DOCUMENT_ROOT.'/config/lang/' . $lang . '.php';
$this->_lang = new $object();
}
}
public function get(){
return $this->_lang->get();
}
public function getUrls(){
return $this->_lang->get('url');
}
public function getPages(){
return $this->_lang->get('pages');
}
/*
* Display the menu
*/
public function displayMenu()
{
$output = '';
foreach($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 = array();
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';
}
else if( ($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)
{
// Search in "Urls" array
$urls = $this->_lang->get('url');
foreach($urls as $converted => $url)
{
if(is_array($url))
{
if(in_array($key,$url))
{
return $converted;
}
}
else if($url == $key)
{
return $converted;
}
}
// Search in "Lang" array
$lang = $this->_lang->get();
foreach($lang as $converted => $label)
{
$url = Fn::encodeUrl($label);
if($url == $key)
{
return $converted;
}
}
return $key;
}
/*
* Convert language the other way
*/
public function getPageKey($page)
{
$pages = $this->_lang->get('pages');
foreach($pages as $converted => $value)
{
if(strtolower($value) == strtolower($page))
{
return $converted;
}
}
return $page;
}
}