Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feature 2978: l10n() and {translate} with additional arguments
TODO: apply in core

git-svn-id: http://piwigo.org/svn/trunk@24988 68402e56-0260-453c-a942-63ccdbb3a9ee
  • Loading branch information
mistic100 committed Oct 19, 2013
1 parent 2209edc commit 6313ae8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
19 changes: 13 additions & 6 deletions include/functions.inc.php
Expand Up @@ -900,10 +900,11 @@ function get_name_from_file($filename)
}

/**
* returns the corresponding value from $lang if existing. Else, the key is
* returned
*
* @param string key
* translation function
* returns the corresponding value from $lang if existing, else the key is returned
* if more than one parameter is provided sprintf is applied
* @param string $key
* @param mixed $args,... optional arguments
* @return string
*/
function l10n($key)
Expand All @@ -914,15 +915,21 @@ function l10n($key)
{
if ($conf['debug_l10n'] and !isset($lang[$key]) and !empty($key))
{
trigger_error('[l10n] language key "'.$key.'" is not defined', E_USER_WARNING);
trigger_error('[l10n] language key "'. $key .'" not defined', E_USER_WARNING);
}
$val = $key;
}

if (func_num_args() > 1)
{
$val = vsprintf($val, array_slice(func_get_args(), 1));
}

return $val;
}

/**
* returns the prinft value for strings including %d
* returns the printf value for strings including %d
* return is concorded with decimal value (singular, plural)
*
* @param singular string key
Expand Down
30 changes: 23 additions & 7 deletions include/template.class.php
Expand Up @@ -95,7 +95,7 @@ function Template($root = ".", $theme= "", $path = "template")
$this->smarty->registerPlugin('modifiercompiler', 'translate', array('Template', 'modcompiler_translate') );
$this->smarty->registerPlugin('modifiercompiler', 'translate_dec', array('Template', 'modcompiler_translate_dec') );
$this->smarty->registerPlugin('modifier', 'explode', array('Template', 'mod_explode') );
$this->smarty->registerPlugin( 'modifier', 'get_extent', array($this, 'get_extent') );
$this->smarty->registerPlugin('modifier', 'get_extent', array($this, 'get_extent') );
$this->smarty->registerPlugin('block', 'html_head', array($this, 'block_html_head') );
$this->smarty->registerPlugin('block', 'html_style', array($this, 'block_html_style') );
$this->smarty->registerPlugin('function', 'combine_script', array($this, 'func_combine_script') );
Expand Down Expand Up @@ -487,7 +487,7 @@ function p()
}
}

static function get_php_str_val($str)
private static function get_php_str_val($str)
{
if (is_string($str) && strlen($str)>1)
{
Expand All @@ -507,19 +507,35 @@ static function get_php_str_val($str)
static function modcompiler_translate($params)
{
global $conf, $lang;
if ( $conf['compiled_template_cache_language']
&& ($key=self::get_php_str_val($params[0])) !== null)

switch (count($params))
{
if (isset($lang[$key]))
case 1:
if ($conf['compiled_template_cache_language']
&& ($key=self::get_php_str_val($params[0])) !== null
&& isset($lang[$key])
) {
return var_export($lang[$key], true);
}
return 'l10n('.$params[0].')';

default:
if ($conf['compiled_template_cache_language'])
{
$ret = 'sprintf(';
$ret .= self::modcompiler_translate( array($params[0]) );
$ret .= ','. implode(',', array_slice($params, 1));
$ret .= ')';
return $ret;
}
return 'l10n('.$params[0].','.implode(',', array_slice($params, 1)).')';
}
return 'l10n('.$params[0].')';
}

static function modcompiler_translate_dec($params)
{
global $conf, $lang, $lang_info;
if ( $conf['compiled_template_cache_language'])
if ($conf['compiled_template_cache_language'])
{
$ret = 'sprintf(';
if ($lang_info['zero_plural'])
Expand Down

0 comments on commit 6313ae8

Please sign in to comment.