Browse files

API-CHANGE: new translatable syntax _t function

  • Loading branch information...
1 parent c314d0b commit 65e3651ff4ee46959ac8a7ba7806235ed7b5b85d @candidasa candidasa committed Apr 16, 2012
Showing with 1,657 additions and 1,564 deletions.
  1. +31 −10 i18n/i18n.php
  2. +1,625 −1,553 view/SSTemplateParser.php
  3. +1 −1 view/SSTemplateParser.php.inc
View
41 i18n/i18n.php
@@ -1456,25 +1456,39 @@ public static function get_time_format() {
* the class name where this string is used and Entity identifies the string inside the namespace.
* @param string $string The original string itself. In a usual call this is a mandatory parameter, but if you are reusing a string which
* has already been "declared" (using another call to this function, with the same class and entity), you can omit it.
- * @param string $context If the string can be difficult to translate by any reason, you can help translators with some more info using this param
+ * @param string $context (optional) If the string can be difficult to translate by any reason, you can help translators with some more info using this param
+ * @param string injectionArray (optional) array of key value pairs that are used to replace corresponding expressions in {curly brackets} in the $string
* @return string The translated string, according to the currently set locale {@link i18n::set_locale()}
*/
- static function _t($entity, $string = "", $context = "") {
+ static function _t($entity, $string = "", $context = "", $injection = "") {
if(is_numeric($context) && in_array($context, array(PR_LOW, PR_MEDIUM, PR_HIGH))) {
- $context = func_get_arg(4);
Deprecation::notice(
- '3.0',
+ '3.0',
'The $priority argument to _t() is deprecated, please use module inclusion priorities instead'
);
}
+
+ //fetch the injection array out of the parameters (if it is present)
+ $argList = func_get_args();
+ $argNum = func_num_args();
+ //_t($entity, $string = "", $context (optional), $injectionArray (optional))
+ $injectionArray = null;
+ for($i = 0; $i < $argNum; $i++) {
+ if (is_array($argList[$i])) { //we have reached the injectionArray
+ $injectionArray = $argList[$i]; //any array in the args will be the injection array
+ }
+ }
+
// get current locale (either default or user preference)
$locale = i18n::get_locale();
$lang = i18n::get_lang_from_locale($locale);
-
+
// Only call getter if static isn't already defined (for performance reasons)
$translatorsByPrio = self::$translators;
if(!$translatorsByPrio) $translatorsByPrio = self::get_translators();
-
+
+ $returnValue = $string; // Fall back to default string argument
+
foreach($translatorsByPrio as $priority => $translators) {
foreach($translators as $name => $translator) {
$adapter = $translator->getAdapter();
@@ -1489,14 +1503,21 @@ static function _t($entity, $string = "", $context = "") {
$translation = $adapter->translate($entity, $locale);
// Return translation only if we found a match thats not the entity itself (Zend fallback)
- if($translation && $translation != $entity) return $translation;
+ if($translation && $translation != $entity) $returnValue = $translation;
}
}
-
- // Fall back to default string argument
- return $string;
+
+ // inject the variables from injectionArray (if present)
+ if ($injectionArray && count($injectionArray) > 0) {
+ foreach($injectionArray as $variable => $injection) {
+ $returnValue = str_replace('{'.$variable.'}', $injection, $returnValue);
+ }
+ }
+
+ return $returnValue;
}
+
/**
* @return array Array of priority keys to instances of Zend_Translate, mapped by name.
*/
View
3,178 view/SSTemplateParser.php
1,625 additions, 1,553 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
2 view/SSTemplateParser.php.inc
@@ -103,7 +103,7 @@ class SSTemplateParser extends Parser {
# and is used by all enclosing blocks, as well as a base for the top level.
# Any new template elements need to be included in this list, if they are to work.
- Template: (Comment | Translate | If | Require | CacheBlock | UncachedBlock | OldI18NTag | ClosedBlock | OpenBlock | MalformedBlock | Injection | Text)+
+ Template: (Comment | Translate | If | Require | CacheBlock | UncachedBlock | OldI18NTag | Include | ClosedBlock | OpenBlock | MalformedBlock | Injection | Text)+
*/
function Template_STR(&$res, $sub) {
$res['php'] .= $sub['php'] . PHP_EOL ;

0 comments on commit 65e3651

Please sign in to comment.