Skip to content
Snippets Groups Projects
Commit 96e01c1b authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

testing for stud.ip 5.0's generic translation methods

parent fdf9d6ca
No related branches found
No related tags found
No related merge requests found
......@@ -18,62 +18,15 @@ use SearchWidget;
use SelectElement;
use SelectWidget;
use Sidebar;
use StudipController;
use PluginController;
use Trails_Flash;
use URLHelper;
class Controller extends StudipController
class Controller extends PluginController
{
protected $allow_nobody = false;
protected $temp_storage;
/**
* Constructs the controller and provide translations methods.
*
* @param object $dispatcher
* @see https://stackoverflow.com/a/12583603/982902 if you need to overwrite
* the constructor of the controller
*/
public function __construct($dispatcher)
{
parent::__construct($dispatcher);
$this->plugin = $dispatcher->current_plugin;
// Localization
$this->_ = function ($string) use ($dispatcher) {
return call_user_func_array(
[$dispatcher->current_plugin, '_'],
func_get_args()
);
};
$this->_n = function ($string0, $tring1, $n) use ($dispatcher) {
return call_user_func_array(
[$dispatcher->current_plugin, '_n'],
func_get_args()
);
};
}
/**
* Intercepts all non-resolvable method calls in order to correctly handle
* calls to _ and _n.
*
* @param string $method
* @param array $arguments
* @return mixed
* @throws RuntimeException when method is not found
*/
public function __call($method, $arguments)
{
$variables = get_object_vars($this);
if (isset($variables[$method]) && is_callable($variables[$method])) {
return call_user_func_array($variables[$method], $arguments);
}
return parent::__call($method, $arguments);
}
public function before_filter(&$action, &$args)
{
$this->flash = Trails_Flash::instance();
......
......@@ -7,15 +7,12 @@ use StudIPPlugin;
abstract class Plugin extends StudIPPlugin
{
const GETTEXT_DOMAIN = 'schwarzes-brett';
use \TranslatablePluginTrait;
public function __construct()
{
parent::__construct();
bindtextdomain(static::GETTEXT_DOMAIN, $this->getPluginPath() . '/locale');
bind_textdomain_codeset(static::GETTEXT_DOMAIN, 'UTF-8');
foreach (get_class_methods($this) as $method) {
if (!preg_match('/^on\w+(Did|Will)\w+$/', $method)) {
continue;
......@@ -26,60 +23,14 @@ abstract class Plugin extends StudIPPlugin
}
}
/**
* Plugin localization for a single string.
* This method supports sprintf()-like execution if you pass additional
* parameters.
*
* @param String $string String to translate
* @return translated string
*/
public function _($string)
public static function getTranslationDomain()
{
$result = static::GETTEXT_DOMAIN === null
? $string
: dcgettext(static::GETTEXT_DOMAIN, $string, LC_MESSAGES);
if ($result === $string) {
$result = _($string);
return 'schwarzes-brett';
}
if (func_num_args() > 1) {
$arguments = array_slice(func_get_args(), 1);
$result = vsprintf($result, $arguments);
}
return $result;
}
/**
* Plugin localization for plural strings.
* This method supports sprintf()-like execution if you pass additional
* parameters.
*
* @param String $string0 String to translate (singular)
* @param String $string1 String to translate (plural)
* @param mixed $n Quantity factor (may be an array or array-like)
* @return translated string
*/
public function _n($string0, $string1, $n)
public function getTranslationPath()
{
if (is_array($n)) {
$n = count($n);
}
$result = static::GETTEXT_DOMAIN === null
? $string0
: dngettext(static::GETTEXT_DOMAIN, $string0, $string1, $n);
if ($result === $string0 || $result === $string1) {
$result = ngettext($string0, $string1, $n);
}
if (func_num_args() > 3) {
$arguments = array_slice(func_get_args(), 3);
$result = vsprintf($result, $arguments);
}
return $result;
return $this->getPluginPath() . '/locale';
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment