PKPLocale::translate PHP Method

translate() static public method

Substitution works by replacing tokens like "{$foo}" with the value of the parameter named "foo" (if supplied).
static public translate ( $key, $params = [], $locale = null ) : string
$key string
$params array named substitution parameters
$locale string the locale to use
return string
    static function translate($key, $params = array(), $locale = null)
    {
        if (!isset($locale)) {
            $locale = AppLocale::getLocale();
        }
        if (($key = trim($key)) == '') {
            return '';
        }
        $localeFiles =& AppLocale::getLocaleFiles($locale);
        $value = '';
        for ($i = count($localeFiles) - 1; $i >= 0; $i--) {
            $value = $localeFiles[$i]->translate($key, $params);
            if ($value !== null) {
                return $value;
            }
        }
        // Add a missing key to the debug notes.
        $notes =& Registry::get('system.debug.notes');
        $notes[] = array('debug.notes.missingLocaleKey', array('key' => $key));
        if (!HookRegistry::call('PKPLocale::translate', array(&$key, &$params, &$locale, &$localeFiles, &$value))) {
            // Add some octothorpes to missing keys to make them more obvious
            return '##' . htmlentities($key) . '##';
        } else {
            return $value;
        }
    }

Usage Example

/**
 * Wrapper around PKPLocale::translate().
 *
 * Enables us to work with translated strings everywhere without
 * introducing a lot of duplicate code and without getting
 * blisters on our fingers.
 *
 * This is similar to WordPress' solution for translation, see
 * <http://codex.wordpress.org/Translating_WordPress>.
 *
 * @see PKPLocale::translate()
 *
 * @param $key string
 * @param $params array named substitution parameters
 * @param $locale string the locale to use
 * @return string
 */
function __($key, $params = array(), $locale = null)
{
    return PKPLocale::translate($key, $params, $locale);
}