Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
namespace App;
use Nette;
use Tracy\Debugger;
use App\Model;
use Nette\Neon\Neon;
class Translator implements Nette\Localization\ITranslator
{
private $neonManager;
private $lang;
private $langFile = null;
private $langCache = null;
private $langSlovnikCache = null;
private $langKnihovny = array('website', 'administration');
private $web;
private $debugging = true;
private $debugTime = null;
public function __construct(Model\NeonManager $neonManager, Model\Web $web) {
$this->web = $web;
$this->neonManager = $neonManager;
$this->debugTime = date('c');
$this->langFile = null;
$this->langCache = null;
$this->langSlovnikCache = null;
}
public function set($lang, $knihovna) {
$this->lang = $lang;
$this->langFile = $this->zjistiSouborJazyka($this->lang);
$this->langCache = $this->chytJazykovySoubor($this->langFile);
$this->langSlovnikCache = $this->chytSlovnikCache($knihovna);
Debugger::barDump([$this->lang, $this->langSlovnikCache], "Translator::Aktivní knihovna");
return $this;
}
public function translate($klic, $parametry = array(), $count = 0) {
$x = $this->langSlovnikCache;
if(is_array($x)) {
if(in_array($klic, array_keys($x))) {
if(count($parametry) > 0) return str_replace(array_keys($parametry), array_values($parametry), $x[$klic]);
else return $x[$klic];
}
else return $this->translatorChyba("Translator :: Key \"".$klic."\" does not exists in file \"".$this->langFile."\".");
}
else return $this->translatorChyba("Translator :: Translate array is not defined in \"".$this->langFile."\".");
}
private function translatorChyba($log) {
if($this->debugging == true) $this->neonManager->debugWriteFile("../web/languages/logs/".$this->debugTime."_debug.log", $log);
return "Translator Error";
}
private function chytSlovnikCache($knihovna) {
if(!$this->langSlovnikCache) {
$x = $this->chytJazykovySoubor($this->langFile);
if(!empty($x)) {
if(isset($x['translation'][$knihovna])) {
$this->langSlovnikCache = $x['translation'][$knihovna];
}
else return null;
}
else {
$this->translatorChyba("Translator :: File \"".$this->langFile."\" does not exists or this file is corrupted.");
return null;
}
}
return $this->langSlovnikCache;
}
private function chytJazykovySoubor($soubor) {
if(!$this->langCache) {
$this->langCache = $this->neonManager->readNeonFileAsArray('../web/languages/'.$soubor.'.neon');
$templateLangFile = __DIR__."/../../web/templates/".$this->web->nastaveni()['website_template']."/Assets/languages/".$soubor.".neon";
if(file_exists($templateLangFile)) {
$neonString = file_get_contents($templateLangFile);
try { $neon = Neon::decode($neonString); }
catch (Nette\Neon\Exception $e) { $neon = null; }
if((is_array($neon) || is_object($neon)) && $neon != null) {
$templateLang = $neon['translation'];
foreach($templateLang as $knihovna => $preklady) {
if(in_array($knihovna, $this->langKnihovny)) {
if(isset($preklady) && is_array($preklady) && count($preklady) > 0) {
foreach($preklady as $klic => $hodnota) {
$this->langCache['translation'][$knihovna][$klic] = $hodnota;
}
}
}
else $this->translatorChyba("Translator :: TemplateTranslator :: Unknown library \"".$knihovna."\".");
}
}
else $this->translatorChyba("Translator :: TemplateTranslator :: Wrong neon format.");
}
}
return $this->langCache;
}
private function zjistiSouborJazyka($klic) {
if($klic == null) $klic = $this->web->defaultniJazyk();
if(!$this->langFile) {
if(is_numeric($klic)) $this->langFile = $this->web->jazyky[$klic]->soubor;
else $this->langFile = $this->web->jazykyKlic[$klic]->soubor;
}
return $this->langFile;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.