SimpleSAML\Locale\Translate::t PHP Method

t() public method

This function is used to look up a translation tag in dictionaries, and return the translation into the current language. If no translation into the current language can be found, english will be tried, and if that fails, placeholder text will be returned. An array can be passed as the tag. In that case, the array will be assumed to be on the form (language => text), and will be used as the source of translations. This function can also do replacements into the translated tag. It will search the translated tag for the keys provided in $replacements, and replace any found occurrences with the value of the key.
Deprecation: Not used in twig, gettext
public t ( string | array $tag, array $replacements = [], boolean $fallbackdefault = true, $oldreplacements = [], $striptags = false ) : string
$tag string | array A tag name for the translation which should be looked up, or an array with (language => text) mappings. The array version will go away in 2.0
$replacements array An associative array of keys that should be replaced with values in the translated string.
$fallbackdefault boolean Default translation to use as a fallback if no valid translation was found.
return string The translated tag, or a placeholder value if the tag wasn't found.
    public function t($tag, $replacements = array(), $fallbackdefault = true, $oldreplacements = array(), $striptags = false)
    {
        $backtrace = debug_backtrace();
        $where = $backtrace[0]['file'] . ':' . $backtrace[0]['line'];
        if (!$fallbackdefault) {
            \SimpleSAML\Logger::warning('Deprecated use of new SimpleSAML\\Locale\\Translate::t(...) at ' . $where . '. This parameter will go away, the fallback will become' . ' identical to the $tag in 2.0.');
        }
        if (!is_array($replacements)) {
            // TODO: remove this entire if for 2.0
            // old style call to t(...). Print warning to log
            \SimpleSAML\Logger::warning('Deprecated use of SimpleSAML\\Locale\\Translate::t(...) at ' . $where . '. Please update the code to use the new style of parameters.');
            // for backwards compatibility
            if (!$replacements && $this->getTag($tag) === null) {
                \SimpleSAML\Logger::warning('Code which uses $fallbackdefault === FALSE should be updated to use the getTag() method instead.');
                return null;
            }
            $replacements = $oldreplacements;
        }
        if (is_array($tag)) {
            $tagData = $tag;
            \SimpleSAML\Logger::warning('Deprecated use of new SimpleSAML\\Locale\\Translate::t(...) at ' . $where . '. The $tag-parameter can only be a string in 2.0.');
        } else {
            $tagData = $this->getTag($tag);
            if ($tagData === null) {
                // tag not found
                \SimpleSAML\Logger::info('Template: Looking up [' . $tag . ']: not translated at all.');
                return $this->getStringNotTranslated($tag, $fallbackdefault);
            }
        }
        $translated = $this->getPreferredTranslation($tagData);
        foreach ($replacements as $k => $v) {
            // try to translate if no replacement is given
            if ($v == null) {
                $v = $this->t($k);
            }
            $translated = str_replace($k, $v, $translated);
        }
        return $translated;
    }

Usage Example

 /**
  * Test SimpleSAML\Locale\Translate::t().
  */
 public function testTFallback()
 {
     $c = \SimpleSAML_Configuration::loadFromArray(array());
     $t = new Translate($c);
     $testString = 'Blablabla';
     // $fallbackdefault = true
     $result = 'not translated (' . $testString . ')';
     $this->assertEquals($result, $t->t($testString));
     // $fallbackdefault = false, should be a noop
     $this->assertEquals($testString, $t->t($testString, array(), false));
 }
All Usage Examples Of SimpleSAML\Locale\Translate::t