I18n::translate PHP Method

translate() public static method

Used by the translation functions in basics.php Returns a translated string based on current language and translation files stored in locale folder
public static translate ( string $singular, string $plural = null, string $domain = null, string $category = self::LC_MESSAGES, integer $count = null, string $language = null, string $context = null ) : string
$singular string String to translate
$plural string Plural string (if any)
$domain string Domain The domain of the translation. Domains are often used by plugin translations. If null, the default domain will be used.
$category string Category The integer value of the category to use.
$count integer Count Count is used with $plural to choose the correct plural form.
$language string Language to translate string to. If null it checks for language in session followed by Config.language configuration variable.
$context string Context The context of the translation, e.g a verb or a noun.
return string translated string.
    public static function translate($singular, $plural = null, $domain = null, $category = self::LC_MESSAGES, $count = null, $language = null, $context = null)
    {
        $_this = I18n::getInstance();
        if (strpos($singular, "\r\n") !== false) {
            $singular = str_replace("\r\n", "\n", $singular);
        }
        if ($plural !== null && strpos($plural, "\r\n") !== false) {
            $plural = str_replace("\r\n", "\n", $plural);
        }
        if (is_numeric($category)) {
            $_this->category = $_this->_categories[$category];
        }
        if (empty($language)) {
            if (CakeSession::started()) {
                $language = CakeSession::read('Config.language');
            }
            if (empty($language)) {
                $language = Configure::read('Config.language');
            }
        }
        if ($_this->_lang && $_this->_lang !== $language || !$_this->_lang) {
            $lang = $_this->l10n->get($language);
            $_this->_lang = $lang;
        }
        if ($domain === null) {
            $domain = static::$defaultDomain;
        }
        if ($domain === '') {
            throw new CakeException(__d('cake_dev', 'You cannot use "" as a domain.'));
        }
        $_this->domain = $domain . '_' . $_this->l10n->lang;
        if (!isset($_this->_domains[$domain][$_this->_lang])) {
            $_this->_domains[$domain][$_this->_lang] = Cache::read($_this->domain, '_cake_core_');
        }
        if (!isset($_this->_domains[$domain][$_this->_lang][$_this->category])) {
            $_this->_bindTextDomain($domain);
            Cache::write($_this->domain, $_this->_domains[$domain][$_this->_lang], '_cake_core_');
        }
        if ($_this->category === 'LC_TIME') {
            return $_this->_translateTime($singular, $domain);
        }
        if (!isset($count)) {
            $plurals = 0;
        } elseif (!empty($_this->_domains[$domain][$_this->_lang][$_this->category]["%plural-c"]) && $_this->_noLocale === false) {
            $header = $_this->_domains[$domain][$_this->_lang][$_this->category]["%plural-c"];
            $plurals = $_this->_pluralGuess($header, $count);
        } else {
            if ($count != 1) {
                $plurals = 1;
            } else {
                $plurals = 0;
            }
        }
        if (isset($_this->_domains[$domain][$_this->_lang][$_this->category][$singular][$context])) {
            if (($trans = $_this->_domains[$domain][$_this->_lang][$_this->category][$singular][$context]) || $plurals && ($trans = $_this->_domains[$domain][$_this->_lang][$_this->category][$plural][$context])) {
                if (is_array($trans)) {
                    if (isset($trans[$plurals])) {
                        $trans = $trans[$plurals];
                    } else {
                        trigger_error(__d('cake_dev', 'Missing plural form translation for "%s" in "%s" domain, "%s" locale. ' . ' Check your po file for correct plurals and valid Plural-Forms header.', $singular, $domain, $_this->_lang), E_USER_WARNING);
                        $trans = $trans[0];
                    }
                }
                if (strlen($trans)) {
                    return $trans;
                }
            }
        }
        if (!empty($plurals)) {
            return $plural;
        }
        return $singular;
    }

Usage Example

 function testTranslationCaching()
 {
     Configure::write('Config.language', 'cache_test_po');
     $i18n =& i18n::getInstance();
     // reset internally stored entries
     I18n::clear();
     Cache::clear(false, '_cake_core_');
     $lang = Configure::read('Config.language');
     #$i18n->l10n->locale;
     Cache::config('_cake_core_', Cache::config('default'));
     // make some calls to translate using different domains
     $this->assertEqual(I18n::translate('dom1.foo', false, 'dom1'), 'Dom 1 Foo');
     $this->assertEqual(I18n::translate('dom1.bar', false, 'dom1'), 'Dom 1 Bar');
     $this->assertEqual($i18n->__domains['dom1']['cache_test_po']['LC_MESSAGES']['dom1.foo'], 'Dom 1 Foo');
     // reset internally stored entries
     I18n::clear();
     // now only dom1 should be in cache
     $cachedDom1 = Cache::read('dom1_' . $lang, '_cake_core_');
     $this->assertEqual($cachedDom1['LC_MESSAGES']['dom1.foo'], 'Dom 1 Foo');
     $this->assertEqual($cachedDom1['LC_MESSAGES']['dom1.bar'], 'Dom 1 Bar');
     // dom2 not in cache
     $this->assertFalse(Cache::read('dom2_' . $lang, '_cake_core_'));
     // translate a item of dom2 (adds dom2 to cache)
     $this->assertEqual(I18n::translate('dom2.foo', false, 'dom2'), 'Dom 2 Foo');
     // verify dom2 was cached through manual read from cache
     $cachedDom2 = Cache::read('dom2_' . $lang, '_cake_core_');
     $this->assertEqual($cachedDom2['LC_MESSAGES']['dom2.foo'], 'Dom 2 Foo');
     $this->assertEqual($cachedDom2['LC_MESSAGES']['dom2.bar'], 'Dom 2 Bar');
     // modify cache entry manually to verify that dom1 entries now will be read from cache
     $cachedDom1['LC_MESSAGES']['dom1.foo'] = 'FOO';
     Cache::write('dom1_' . $lang, $cachedDom1, '_cake_core_');
     $this->assertEqual(I18n::translate('dom1.foo', false, 'dom1'), 'FOO');
 }
All Usage Examples Of I18n::translate