/**
 * locale for current language and set it on PHP.
 */
function qtranxf_localeForCurrentLanguage($locale)
{
    static $locale_lang;
    //cache
    if (!empty($locale_lang)) {
        //qtranxf_dbg_log('qtranxf_localeForCurrentLanguage: cached $locale_lang: ', $locale_lang);
        return $locale_lang;
    }
    global $q_config;
    $lang = $q_config['language'];
    $locale_lang = $q_config['locale'][$lang];
    // submit a few possible locales
    $lc = array();
    $lc[] = $locale_lang . '.utf8';
    $lc[] = $locale_lang . '@euro';
    $lc[] = $locale_lang;
    if (qtranxf_windows_os()) {
        $windows_locale = qtranxf_default_windows_locale();
        if (isset($windows_locale[$lang])) {
            $wl = $windows_locale[$lang];
            $lc[] = $wl;
        }
    }
    $lc[] = $lang;
    // return the correct locale and most importantly set it (wordpress doesn't, which is bad)
    // only set LC_TIME as everything else doesn't seem to work with windows
    //qtranxf_dbg_log('qtranxf_localeForCurrentLanguage: $lc: ', $lc);
    $loc = setlocale(LC_TIME, $lc);
    if (!$loc) {
        $lc2 = array();
        if (strlen($locale_lang) == 2) {
            $lc2[] = $locale_lang . '_' . strtoupper($locale_lang);
            $loc = $locale_lang . '_' . strtoupper($lang);
            if (!in_array($loc, $lc2)) {
                $lc2[] = $loc;
            }
        }
        $loc = $lang . '_' . strtoupper($lang);
        if (!in_array($loc, $lc2)) {
            $lc2[] = $loc;
        }
        //qtranxf_dbg_log('qtranxf_localeForCurrentLanguage: $lc2: ', $lc2);
        $loc = setlocale(LC_TIME, $lc2);
        //if(!$loc) qtranxf_error_log(sprintf('Could not set locale with setlocale(LC_TIME, %s).', json_encode(array_merge($lc, $lc2))));
    }
    //qtranxf_dbg_log('qtranxf_localeForCurrentLanguage: $locale='.$locale.'; return: '.$locale_lang.'; Set to ', $loc);
    return $locale_lang;
}
Exemple #2
0
function qtranxf_localeForCurrentLanguage($locale)
{
    global $q_config;
    //if( !isset($q_config['language']) ) return $locale;
    // try to figure out the correct locale
    $windows_locale = qtranxf_default_windows_locale();
    //$q_config['windows_locale'];
    $lang = $q_config['language'];
    $locale_lang = $q_config['locale'][$lang];
    $locale = array();
    $locale[] = $locale_lang . ".utf8";
    $locale[] = $locale_lang . "@euro";
    $locale[] = $locale_lang;
    $locale[] = $windows_locale[$lang];
    $locale[] = $lang;
    // return the correct locale and most importantly set it (wordpress doesn't, which is bad)
    // only set LC_TIME as everything else doesn't seem to work with windows
    setlocale(LC_TIME, $locale);
    return $locale_lang;
}
function qtranxf_use_block($lang, $blocks, $show_available = false, $show_empty = false)
{
    global $q_config;
    $available_langs = array();
    //$content = qtranxf_split_blocks($blocks);
    $content = qtranxf_split_blocks($blocks, $available_langs);
    //qtranxf_dbg_log('qtranxf_use_block: $available_langs: ',$available_langs);
    // if content is available show the content in the requested language
    //if(!empty($content[$lang])) return $content[$lang];
    if (!empty($available_langs[$lang])) {
        return $content[$lang];
    } elseif ($show_empty) {
        return '';
    }
    // content is not available in requested language (bad!!) what now?
    // find available and alternative languages
    //if(empty($content[$q_config['default_language']])){
    if (empty($available_langs[$q_config['default_language']])) {
        $alt_lang = null;
        $alt_content = null;
        $alt_lang_is_default = false;
        foreach ($available_langs as $language => $b) {
            if (!qtranxf_isEnabled($language)) {
                continue;
            }
            $alt_lang = $language;
            $alt_content = $content[$language];
            break;
        }
    } else {
        $alt_lang = $q_config['default_language'];
        $alt_content = $content[$alt_lang];
        $alt_lang_is_default = true;
    }
    /*
    	$available_languages = array();
    	foreach($content as $language => $lang_text) {
    		if(empty($lang_text)) continue;
    		if(!qtranxf_isEnabled($language)) continue;
    		$available_languages[] = $language;
    		if(empty($alt_lang)){
    			$alt_lang = $language;
    			$alt_content = $lang_text;
    		}
    	}
    */
    if (!$alt_lang) {
        return '';
    }
    if (!$show_available) {
        if ($q_config['show_displayed_language_prefix']) {
            return '(' . $q_config['language_name'][$alt_lang] . ') ' . $alt_content;
        } else {
            return $alt_content;
        }
    }
    //qtranxf_dbg_log('$alt_content=',$alt_content);
    // display selection for available languages
    $language_list = '';
    if (preg_match('/%LANG:([^:]*):([^%]*)%/', $q_config['not_available'][$lang], $match)) {
        $normal_separator = $match[1];
        $end_separator = $match[2];
        // build available languages string backward
        $translations = get_translations_for_domain('qtranslate');
        $english_names = qtranxf_default_windows_locale();
        //todo: load enabled only
        $i = 0;
        //foreach($available_languages as $language) {
        foreach ($available_langs as $language => $b) {
            if ($i == 1) {
                $language_list = $end_separator . $language_list;
            } elseif ($i > 1) {
                $language_list = $normal_separator . $language_list;
            }
            if (!empty($english_names[$language]) && isset($translations->entries[$english_names[$language]])) {
                $language_name = $translations->entries[$english_names[$language]]->translations[0];
            } else {
                $language_name = $q_config['language_name'][$language];
            }
            $language_list = '<a href="' . qtranxf_convertURL('', $language, false, true) . '" class="qtranxs-available-language-link qtranxs-available-language-link-' . $language . '">' . $language_name . '</a>' . $language_list;
            ++$i;
        }
    }
    //qtranxf_dbg_log('$language_list=',$language_list);
    $msg = '';
    if (!empty($q_config['show_alternative_content']) && $q_config['show_alternative_content']) {
        // show content in  alternative language
        //if(sizeof($available_languages) > 1){
        if (sizeof($available_langs) > 1) {
            if ($alt_lang_is_default) {
                //$fmt = __('For the sake of viewer convenience, the content is shown below in this site default language %s.', 'qtranslate');
                $msg = __('For the sake of viewer convenience, the content is shown below in this site default language.', 'qtranslate');
            } else {
                //$fmt = __('For the sake of viewer convenience, the content is shown below in an available alternative language %s.', 'qtranslate');
                $msg = __('For the sake of viewer convenience, the content is shown below in one of the available alternative languages.', 'qtranslate');
            }
            //$msg = sprintf($fmt, '<a href="'.qtranxf_convertURL('', $language, false, true).'">'.$q_config['language_name'][$alt_lang].'</a>');
            $msg .= ' ' . __('You may click one of the links to switch the site language to another available language.', 'qtranslate');
        } else {
            $msg = __('For the sake of viewer convenience, the content is shown below in the alternative language.', 'qtranslate');
            $msg .= ' ' . __('You may click the link to switch the active language.', 'qtranslate');
        }
        $altlanguagecontent = ' ' . $msg . '</p>' . $alt_content;
    } else {
        //by default, do not show alternative content
        $altlanguagecontent = '</p>';
    }
    $output = '<p class="qtranxs-available-languages-message qtranxs-available-languages-message-' . $lang . '">' . preg_replace('/%LANG:([^:]*):([^%]*)%/', $language_list, $q_config['not_available'][$lang]) . $altlanguagecontent;
    /* todo documentation
     */
    return apply_filters('i18n_content_translation_not_available', $output, $lang, $language_list, $alt_lang, $alt_content, $msg, $q_config);
}