Ejemplo n.º 1
0
function init_i18n()
{
    global $CFG, $USER;
    // Clean up the (possible) flaw created by the first svn commit
    // TODO can be removed with next package release
    //delete_records('user_flags','flag','language','user_id',0);
    // End fix
    // Load default language path
    parse_installed_languages($CFG->dirroot);
    // Load plugin language paths
    $plugindir = opendir($CFG->dirroot . 'mod/');
    while (false !== ($dir = readdir($plugindir))) {
        $firstchar = substr($dir, 0, 1);
        if ($firstchar == '.' or $dir == 'CVS' or $dir == '_vti_cnf' or $dir == 'README.txt') {
            continue;
        } else {
            parse_installed_languages($CFG->dirroot . 'mod/' . $dir . '/');
        }
    }
    // Store the browser setting
    $USER->languages_browser = parse_http_accept_language();
    // Check if $CFG->defaultlocale has been setup correctly
    if (!$CFG->languages_available[$CFG->defaultlocale]) {
        $bad = $CFG->defaultlocale;
        // Empty it, and send a message to the screen
        $CFG->defaultlocale = '';
        global $messages;
        $messages[] = "Unknown language code: " . $bad . ". Please ask the system administrator to properly configure \$CFG->defaultlocale in the main configuration file.";
    }
    // Setup variables to hold the user language
    $USER->locale = $CFG->defaultlocale;
    // set initial session variable
    if (!isset($_SESSION['locale'])) {
        $_SESSION['locale'] = '';
    }
    // Now, grab user preference
    // logged_on is not yet defined yet in this stage,
    // use USER->ident for this...
    // TODO Implement better check!
    if ($USER->ident != 0) {
        // User is logged in and has a session
        if ($result = get_record('user_flags', 'flag', 'language', 'user_id', $USER->ident)) {
            // TODO fix possible rev. 664 and 663 commit errors
            // Remove for before next package release
            if ($result->value == 'default') {
                $result->value = $CFG->defaultlocale;
            }
            // End fix
            $_SESSION['locale'] = $result->value;
            $USER->locale = $result->value;
        } else {
            // No flag set yet, try extract from the browser
            $setting = '';
            if (!empty($USER->languages_browser)) {
                $keys = array_keys($USER->languages_browser);
                if ($browser = $USER->languages_browser[$keys[0]]) {
                    $setting = $browser['fullcode'];
                }
            } else {
                // No browser preference, get defaultlocale
                $setting = $CFG->defaultlocale;
            }
            // Store the value
            $flag = new StdClass();
            $flag->flag = 'language';
            $flag->user_id = $USER->ident;
            $flag->value = $setting;
            insert_record('user_flags', $flag);
            $_SESSION['locale'] = $setting;
            $USER->locale = $setting;
        }
    } else {
        // User is logged out
        // TODO special actions? For now language will be set
        // to $CFG-defaultlocale for non logged in users or via
        // a session, see below.
        // TODO This is here to set the pref via a session for
        // non logged in so they could also use the language
        // selection widget. Should perhaps get stored in a cookie.
        if (isset($_SESSION['locale']) && $_SESSION['locale'] != '') {
            $USER->locale = $_SESSION['locale'];
        } else {
            $USER->locale = $CFG->defaultlocale;
        }
    }
    // Handle explicit language setting via widget. Here for now because
    // else it will get called too late to set the user language.
    $params = explode('?', $_SERVER['REQUEST_URI']);
    if (isset($params[1])) {
        $lang = explode('=', $params[1]);
        if ($lang[0] == 'lang') {
            if ($lang[1] != 'default' && array_key_exists($lang[1], $CFG->languages_available)) {
                $USER->locale = $lang[1];
            } else {
                $USER->locale = $CFG->defaultlocale;
            }
        }
        // Store it in the user preferences
        // TODO better logged in check, logged_on not available
        if ($USER->ident != 0) {
            // Have to replicate flag setting because of including order
            // unset first
            delete_records('user_flags', 'flag', 'language', 'user_id', $USER->ident);
            // save the flag
            $flag = new StdClass();
            $flag->flag = 'language';
            $flag->user_id = $USER->ident;
            $flag->value = $USER->locale;
            insert_record('user_flags', $flag);
        }
        // Set session variable
        $_SESSION['locale'] = $USER->locale;
    }
    // Set system locale, for future native gettext support
    setlocale(LC_ALL, $USER->locale . '.utf8', $USER->locale);
    // If only language set, add a region for strftime to work properly.
    // Note, this implies system locales installed and is independent
    // from Elgg locales
    $time_locale = $USER->locale;
    if (!substr($USER->locale, 2, 1) == "_") {
        $time_locale = $USER->locale . "_" . strtoupper($USER->locale);
    }
    setlocale(LC_TIME, $time_locale . '.utf8', $time_locale);
}
Ejemplo n.º 2
0
function init_i18n($domain, $native = false)
{
    global $CFG;
    // To prevent config file error
    if (!defined($CFG->defaultlocale) || empty($CFG->defaultlocale)) {
        $CFG->locale = "en_GB";
    }
    $languages = parse_http_accept_language();
    // Get the language array key
    $lang_key = array_shift(array_keys($languages));
    if (!empty($languages[$lang_key]['fullcode'])) {
        //Set initial user locale
        $CFG->userlocale = $languages[$lang_key]['fullcode'];
    } else {
        $CFG->userlocale = $CFG->defaultlocale;
    }
    if ($native == false) {
        $textdomainfile = textdomain_file($CFG->defaultlocale);
        load_textdomain($domain, $textdomainfile);
    }
    return;
}