Refactor translation functions arguments handeling

This commit is contained in:
Marlin Cremers 2014-06-08 13:41:48 +02:00
parent 461aa34739
commit 4f91e14cab
2 changed files with 34 additions and 56 deletions

View file

@ -643,6 +643,26 @@ class I18n {
return $definitions; return $definitions;
} }
/**
* Puts the parameters in raw translated strings
*
* @param string $translated The raw translated string
* @param array $args The arguments to put in the translation
* @return string Translated string with arguments
*/
public static function insertArgs($translated, array $args) {
if (empty($args)) {
return $translated;
}
if (is_array($args[0])) {
$args = $args[0];
}
$translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
}
/** /**
* Auxiliary function to parse a symbol from a locale definition file * Auxiliary function to parse a symbol from a locale definition file
* *

View file

@ -552,14 +552,8 @@ if (!function_exists('__')) {
App::uses('I18n', 'I18n'); App::uses('I18n', 'I18n');
$translated = I18n::translate($singular); $translated = I18n::translate($singular);
if ($args === null) { $arguments = func_get_args();
return $translated; return I18n::insertArgs($translated, array_slice($arguments, 1));
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 1);
}
$translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
} }
} }
@ -584,14 +578,8 @@ if (!function_exists('__n')) {
App::uses('I18n', 'I18n'); App::uses('I18n', 'I18n');
$translated = I18n::translate($singular, $plural, null, I18n::LC_MESSAGES, $count); $translated = I18n::translate($singular, $plural, null, I18n::LC_MESSAGES, $count);
if ($args === null) { $arguments = func_get_args();
return $translated; return I18n::insertArgs($translated, array_slice($arguments, 3));
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 3);
}
$translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
} }
} }
@ -613,14 +601,8 @@ if (!function_exists('__d')) {
} }
App::uses('I18n', 'I18n'); App::uses('I18n', 'I18n');
$translated = I18n::translate($msg, null, $domain); $translated = I18n::translate($msg, null, $domain);
if ($args === null) { $arguments = func_get_args();
return $translated; return I18n::insertArgs($translated, array_slice($arguments, 2));
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 2);
}
$translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
} }
} }
@ -646,14 +628,8 @@ if (!function_exists('__dn')) {
} }
App::uses('I18n', 'I18n'); App::uses('I18n', 'I18n');
$translated = I18n::translate($singular, $plural, $domain, I18n::LC_MESSAGES, $count); $translated = I18n::translate($singular, $plural, $domain, I18n::LC_MESSAGES, $count);
if ($args === null) { $arguments = func_get_args();
return $translated; return I18n::insertArgs($translated, array_slice($arguments, 4));
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 4);
}
$translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
} }
} }
@ -690,14 +666,8 @@ if (!function_exists('__dc')) {
} }
App::uses('I18n', 'I18n'); App::uses('I18n', 'I18n');
$translated = I18n::translate($msg, null, $domain, $category); $translated = I18n::translate($msg, null, $domain, $category);
if ($args === null) { $arguments = func_get_args();
return $translated; return I18n::insertArgs($translated, array_slice($arguments, 3));
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 3);
}
$translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
} }
} }
@ -738,14 +708,8 @@ if (!function_exists('__dcn')) {
} }
App::uses('I18n', 'I18n'); App::uses('I18n', 'I18n');
$translated = I18n::translate($singular, $plural, $domain, $category, $count); $translated = I18n::translate($singular, $plural, $domain, $category, $count);
if ($args === null) { $arguments = func_get_args();
return $translated; return I18n::insertArgs($translated, array_slice($arguments, 5));
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 5);
}
$translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
} }
} }
@ -778,14 +742,8 @@ if (!function_exists('__c')) {
} }
App::uses('I18n', 'I18n'); App::uses('I18n', 'I18n');
$translated = I18n::translate($msg, null, null, $category); $translated = I18n::translate($msg, null, null, $category);
if ($args === null) { $arguments = func_get_args();
return $translated; return I18n::insertArgs($translated, array_slice($arguments, 2));
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 2);
}
$translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
} }
} }