Skip to content

Instantly share code, notes, and snippets.

@Aurielle
Created February 3, 2011 16:59
Show Gist options
  • Save Aurielle/809774 to your computer and use it in GitHub Desktop.
Save Aurielle/809774 to your computer and use it in GitHub Desktop.
LanguageTable - překladová tabulka pro Route s podporou gettextového překladače
<?php
/**
* Avalon CMS
*
* Copyright (c) 2010 Vaclav Vrbka (http://www.php-info.cz)
*/
namespace Avalon\Application;
use Avalon, Nette;
/**
* LanguageTable
* Implementation of smart multi-language filtering table for routes
*
* Simplified implementation of actual LanguageTable cointained in Avalon CMS
*
* @author Vaclav Vrbka
*/
class LanguageTable extends Nette\Object
{
/** @var array */
protected static $styles = array(
'module' => array(
'original' => array(),
'translated' => array(),
),
'presenter' => array(
'original' => array(),
'translated' => array(),
),
'action' => array(
'original' => array(),
'translated' => array(),
),
);
/** @var array */
protected static $notSettable = array(
'#',
'?#',
'?module',
'?presenter',
'?action',
);
/** @var bool */
protected static $lang = 'en';
/**
* Changes current language
*/
public function activate($lang)
{
Avalon\Tools::getTranslator()->lang = $lang;
return self::$lang = $lang;
}
/**
* Registers new module
* @param string
* @return void
*/
public function registerModule($module)
{
// Some module/presenter and action registration
self::$styles['module']['original'][$module] = "x-avalon-route-$module";
foreach($presenters as $p)
{
$pres = $p->name;
self::$styles['presenter']['original'][$pres] = "x-avalon-route-$module-$pres";
$actions = Avalon\Tools::getPresenterTree()->getActions("$module:$pres");
if($actions === NULL)
continue;
foreach($actions as $action)
{
self::$styles['action']['original'][$action] = "x-avalon-route-$module-$pres-$action";
}
}
}
/**
* Translates given style to current language
* @param string
* @return void
*/
protected function translateStyle($style)
{
foreach(self::$styles[$style]['original'] as $k => $v)
{
self::$styles[$style]['translated'][self::$lang][$k] = Nette\String::webalize(__($v));
}
}
/**
* Translate given path and style
*/
public function path2table($style, $string)
{
if(!isset(self::$styles[$style]['translated'][self::$lang]) || empty(self::$styles[$style]['translated'][self::$lang]))
{
$this->translateStyle($style);
return $this->path2table($style, $string);
}
$result = array_search($string, self::$styles[$style]['translated'][self::$lang]);
if($result === FALSE)
{
switch($style)
{
case 'action':
$result = Route::path2action($string);
break;
case 'presenter':
case 'module':
$result = Route::path2presenter($string);
break;
default:
$result = NULL;
break;
}
}
return $result;
}
/**
* Translate given style to path
*/
public function table2path($style, $path)
{
$result = (isset(self::$styles[$style]['translated'][self::$lang][$path])) ? self::$styles[$style]['translated'][self::$lang][$path] : NULL;
if($result === NULL && empty(self::$styles[$style]['translated'][self::$lang]))
{
$this->translateStyle($style);
return $this->table2path($style, $path);
}
elseif($result === NULL)
{
switch($style)
{
case 'action':
$result = Route::action2path($path);
break;
case 'presenter':
case 'module':
$result = Route::presenter2path($path);
break;
default:
$result = NULL;
break;
}
}
return $result;
}
/**
* Register LanguageTable for use in Route
* @return void
*/
public function register()
{
foreach(self::$styles as $style => $data)
{
if(!isset(Route::$styles[$style]))
Route::addStyle($style, $data['parent']);
$canocanizedStyle = self::canocalizeStyle($style);
Route::setStyleProperty($style, Route::FILTER_IN, callback($this, "path2{$canocanizedStyle}"));
Route::setStyleProperty($style, Route::FILTER_OUT, callback($this, "{$canocanizedStyle}2path"));
}
}
/**
* Calls path2xxx and xxx2path functions
* @param string
* @param mixed
*/
public function __call($name, $args)
{
$matches = Nette\String::match($name, '#(path2)?(get|set)?([^2]*)#');
if($matches && $matches[2] === '')
{
// Get style
$style = self::uncanocalizeStyle($matches[3]);
// Get argument
$arg = array_shift($args); // Only one argument is given
// Determine function to use
if($matches[1] === 'path2')
$function = 'path2table';
else
$function = 'table2path';
// Call it and return value
return call_user_func_array(array($this, $function), array($style, $arg));
}
else
{
return parent::__call($name, $args);
}
}
/**
* Constructor
*/
public function __construct()
{
$this->activate(Nette\Environment::getVariable('lang')); // Activates default language
}
/**
* Returns PHP-friendly style name
* @param string
* @return string
*/
private static function canocalizeStyle($style)
{
return strtolower(str_replace(array('#', '?'), array('x_hash', 'x_qmark'), $style));
}
/**
* Returns PHP-friendly style name
* @param string
* @return string
*/
private static function uncanocalizeStyle($style)
{
return str_replace(array('x_hash', 'x_qmark'), array('#', '?'), $style);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment