Exemplo n.º 1
0
    } else {
        if (isset($_course['language'])) {
            $language_interface = $_course['language'];
        }
    }
}
// Sometimes the variable $language_interface is changed
// temporarily for achieving translation in different language.
// We need to save the genuine value of this variable and
// to use it within the function get_lang(...).
$language_interface_initial_value = $language_interface;
/**
 * Include the trad4all language file
 */
// if the sub-language feature is on
$parent_path = SubLanguageManager::get_parent_language_path($language_interface);
if (!empty($parent_path)) {
    // include English
    include $langpath . 'english/trad4all.inc.php';
    // prepare string for current language and its parent
    $lang_file = $langpath . $language_interface . '/trad4all.inc.php';
    $parent_lang_file = $langpath . $parent_path . '/trad4all.inc.php';
    // load the parent language file first
    if (file_exists($parent_lang_file)) {
        include $parent_lang_file;
    }
    // overwrite the parent language translations if there is a child
    if (file_exists($lang_file)) {
        include $lang_file;
    }
} else {
/**
 * Returns a translated (localized) string, called by its identificator.
 * @param string $variable				This is the identificator (name) of the translated string to be retrieved.
 * @param string $reserved				This parameter has been reserved for future use.
 * @param string $language (optional)	Language indentificator. If it is omited, the current interface language is assumed.
 * @return string						Returns the requested string in the correspondent language.
 *
 * @author Roan Embrechts
 * @author Patrick Cool
 * @author Ivan Tcholakov, 2009-2010 (caching functionality, additional parameter $language, other adaptations).
 *
 * Notes:
 * 1. If the name of a given language variable has the prefix "lang" it may be omited, i.e. get_lang('Yes') == get_lang('Yes').
 * 2. Untranslated variables might be indicated by special opening and closing tags  -  [=  =]
 * The special tags do not show up in these two cases:
 * - when the system has been switched to "production server mode";
 * - when a special platform setting 'hide_dltt_markup' is set to "true" (the name of this setting comes from history);
 * 3. Translations are created many contributors through using a special tool: Chamilo Translation Application.
 * @link http://translate.chamilo.org/
 */
function get_lang($variable, $reserved = null, $language = null)
{
    global $language_interface, $language_interface_initial_value, $_api_is_translated, $_api_is_translated_call;
    global $used_lang_vars, $_configuration;
    // add language_measure_frequency to your main/inc/conf/configuration.php in order to generate language
    // variables frequency measurements (you can then see them trhough main/cron/lang/langstats.php)
    // The $langstats object is instanciated at the end of main/inc/global.inc.php
    if (isset($_configuration['language_measure_frequency']) && $_configuration['language_measure_frequency'] == 1) {
        require_once api_get_path(SYS_CODE_PATH) . '/cron/lang/langstats.class.php';
        global $langstats;
        $langstats->add_use($variable, '');
    }
    if (!isset($used_lang_vars)) {
        $used_lang_vars = array();
    }
    // Caching results from some API functions, for speed.
    static $initialized, $encoding, $is_utf8_encoding, $langpath, $test_server_mode, $show_special_markup;
    if (!isset($initialized)) {
        $encoding = api_get_system_encoding();
        $is_utf8_encoding = api_is_utf8($encoding);
        $langpath = api_get_path(SYS_LANG_PATH);
        $test_server_mode = api_get_setting('server_type') == 'test';
        $show_special_markup = api_get_setting('hide_dltt_markup') != 'true' || $test_server_mode;
        $initialized = true;
    }
    // Combining both ways for requesting specific language.
    if (empty($language)) {
        $language = $language_interface;
    }
    $lang_postfix = isset($is_interface_language) && $is_interface_language ? '' : '(' . $language . ')';
    $is_interface_language = $language == $language_interface_initial_value;
    // This is a cache for already translated language variables. By using it, we avoid repetitive translations, gaining speed.
    static $cache;
    // Looking up into the cache for existing translation.
    if (isset($cache[$language][$variable]) && !$_api_is_translated_call) {
        // There is a previously saved translation, returning it.
        //return $cache[$language][$variable];
        $ret = $cache[$language][$variable];
        $used_lang_vars[$variable . $lang_postfix] = $ret;
        return $ret;
    }
    $_api_is_translated = false;
    // There is no cached translation, we have to retrieve it:
    // - from a global variable (the faster way) - on production server mode;
    // - from a local variable after reloading the language files - on test server mode or when requested language is different than the genuine interface language.
    $read_global_variables = $is_interface_language && !$test_server_mode && !$_api_is_translated_call;
    // Reloading the language files when it is necessary.
    if (!$read_global_variables) {
        global $language_files;
        if (isset($language_files)) {
            $parent_language = null;
            if (api_get_setting('allow_use_sub_language') == 'true') {
                require_once api_get_path(SYS_CODE_PATH) . 'admin/sub_language.class.php';
                $parent_language = SubLanguageManager::get_parent_language_path($language);
            }
            if (!is_array($language_files)) {
                if (isset($parent_language)) {
                    @(include "{$langpath}{$parent_language}/{$language_files}.inc.php");
                }
                @(include "{$langpath}{$language}/{$language_files}.inc.php");
            } else {
                foreach ($language_files as &$language_file) {
                    if (isset($parent_language)) {
                        @(include "{$langpath}{$parent_language}/{$language_file}.inc.php");
                    }
                    @(include "{$langpath}{$language}/{$language_file}.inc.php");
                }
            }
        }
    }
    // Translation mode for production servers.
    if (!$test_server_mode) {
        if ($read_global_variables) {
            if (isset($GLOBALS[$variable])) {
                $langvar = $GLOBALS[$variable];
                $_api_is_translated = true;
            } elseif (isset($GLOBALS["lang{$variable}"])) {
                $langvar = $GLOBALS["lang{$variable}"];
                $_api_is_translated = true;
            } else {
                $langvar = $show_special_markup ? SPECIAL_OPENING_TAG . $variable . SPECIAL_CLOSING_TAG : $variable;
            }
        } else {
            if (isset(${$variable})) {
                $langvar = ${$variable};
                $_api_is_translated = true;
            } elseif (isset(${"lang{$variable}"})) {
                $langvar = ${"lang{$variable}"};
                $_api_is_translated = true;
            } else {
                $langvar = $show_special_markup ? SPECIAL_OPENING_TAG . $variable . SPECIAL_CLOSING_TAG : $variable;
            }
        }
        if (empty($langvar) || !is_string($langvar)) {
            $_api_is_translated = false;
            $langvar = $show_special_markup ? SPECIAL_OPENING_TAG . $variable . SPECIAL_CLOSING_TAG : $variable;
        }
        //return $cache[$language][$variable] = $is_utf8_encoding ? $langvar : api_utf8_decode($langvar, $encoding);
        $ret = $cache[$language][$variable] = $is_utf8_encoding ? $langvar : api_utf8_decode($langvar, $encoding);
        $used_lang_vars[$variable . $lang_postfix] = $ret;
        return $ret;
    }
    // Translation mode for test/development servers.
    if (!is_string($variable)) {
        //return $cache[$language][$variable] = SPECIAL_OPENING_TAG.'get_lang(?)'.SPECIAL_CLOSING_TAG;
        $ret = $cache[$language][$variable] = SPECIAL_OPENING_TAG . 'get_lang(?)' . SPECIAL_CLOSING_TAG;
        $used_lang_vars[$variable . $lang_postfix] = $ret;
        return $ret;
    }
    if (isset(${$variable})) {
        $langvar = ${$variable};
        $_api_is_translated = true;
    } elseif (isset(${"lang{$variable}"})) {
        $langvar = ${"lang{$variable}"};
        $_api_is_translated = true;
    } else {
        $langvar = $show_special_markup ? SPECIAL_OPENING_TAG . $variable . SPECIAL_CLOSING_TAG : $variable;
    }
    if (empty($langvar) || !is_string($langvar)) {
        $_api_is_translated = false;
        $langvar = $show_special_markup ? SPECIAL_OPENING_TAG . $variable . SPECIAL_CLOSING_TAG : $variable;
    }
    //return $cache[$language][$variable] = $is_utf8_encoding ? $langvar : api_utf8_decode($langvar, $encoding);
    $ret = $cache[$language][$variable] = $is_utf8_encoding ? $langvar : api_utf8_decode($langvar, $encoding);
    $used_lang_vars[$variable . $lang_postfix] = $ret;
    return $ret;
}