language_switched = false; $this->download_lang = null; $this->languages = new WP_languages(); add_filter('gettext_with_context', array($this, 'icl_sw_filters_gettext_with_context'), 9, 4); $this->settings = get_option('wp_languages_options'); $this->current_scope = 'back-end'; } function init() { $this->plugin_localization(); if(is_admin()){ add_action('admin_menu', array($this, 'admin_menu')); add_action('admin_footer', array($this, 'render_lang_switch_popup')); add_action('admin_head', array($this, 'render_lang_switch_js')); add_action('wp_ajax_wp_language_get_info', array($this, 'ajax_get_lang_info')); add_action('wp_ajax_wp_install_language', array($this, 'ajax_install_language')); add_action('wp_ajax_wp_language_check_for_updates', array($this, 'ajax_check_for_updates')); add_action('wp_ajax_wp_show_hide_language_selector', array($this, 'ajax_show_hide_language_selector')); add_action('wp_ajax_wp_languages_distinct_languages', array($this, 'ajax_wp_languages_distinct_languages')); add_thickbox(); if(isset($_GET['page']) && $_GET['page'] == 'wordpress_language'){ wp_enqueue_script('wp-trans', WP_LANG_URL . '/res/js/wp-trans.js', 'jquery', WP_LANGUAGE_VERSION); if(isset($_GET['scope']) && $_GET['scope']=='front-end'){ $this->current_scope = 'front-end'; } } } add_filter('gettext', array($this, 'icl_sw_filters_gettext'), 9, 3); } function load_css_and_js(){ wp_register_style('wordpress-language', WP_LANG_URL . '/res/css/style.css', array(), WP_LANGUAGE_VERSION); wp_enqueue_style('wordpress-language'); } function save_selected_locale() { if(is_admin() && function_exists('wp_verify_nonce')){ global $pagenow, $wpdb; static $save_started = false; if (!$save_started) { $save_started = true; if ($pagenow == 'options-general.php' && isset($_GET['page']) && $_GET['page'] == 'wordpress_language' && isset($_GET['switch_to']) && isset($_GET['_wpnonce'])) { if (wp_verify_nonce($_GET['_wpnonce'], 'wp_lang_get_lang_info')) { if(isset($_GET['scope']) && $_GET['scope'] == 'front-end'){ update_option('wp_language_locale_front', $_GET['switch_to']); }else{ update_option('wp_language_locale', $_GET['switch_to']); } $this->language_switched = true; } } } } } function locale($locale) { $this->save_selected_locale(); if($this->settings['different_languages'] && !is_admin()){ $stored_locale = get_option('wp_language_locale_front'); }else{ $stored_locale = get_option('wp_language_locale'); } if ($this->download_lang) { $stored_locale = $this->get_locale($this->download_lang); } if ($stored_locale != '') { return $stored_locale; } else { return $locale; } } // Localization function plugin_localization(){ $locale = get_locale(); load_textdomain( 'wordpress-language', WP_LANG_PATH . '/locale/wordpress-language-' . $locale . '.mo'); } function admin_menu(){ add_submenu_page('options-general.php', __('Language', 'wordpress-language'), __('Language', 'wordpress-language'), 'manage_options', 'wordpress_language', array($this, 'language_menu')); } function language_menu() { $icon_url = WP_LANG_URL . '/res/img/wplang_2_32_c.jpg'; global $lang_locales; $different_languages = isset($this->settings['different_languages']) ? $this->settings['different_languages'] : 0; $current_locale_front = get_option('wp_language_locale_front'); if(empty($current_locale_front)){ $current_locale_front = get_locale(); } if($different_languages){ if($this->current_scope == 'front-end'){ $current_locale = $current_locale_front; }else{ $current_locale = get_locale(); } }else{ $current_locale = get_locale(); } $current_lang_code_front = $this->get_lang_code($current_locale_front); $current_lang_code_back = $this->get_lang_code(get_locale()); $current_lang_code = $this->get_lang_code($current_locale); $current_lang = $this->get_own_lang_name($current_lang_code); ?>




wpml_promotion(); ?>
' . __('Updated translation is available.', 'wordpress-language') . '

'; $nonce = wp_create_nonce('wp_lang_get_lang_info'); $url = admin_url('options-general.php?page=wordpress_language&update_lang=1&scope='.$this->current_scope.'&_wpnonce=' . $nonce); echo ' ' . __('Update now', 'wordpress-language') . '

'; } else { if ($ajax) { ?>

current_scope == 'front-end'){ $current_locale = get_option('wp_language_locale_front'); }else{ $current_locale = get_locale(); } if($current_locale == 'en_US') return; $current_lang_code = $this->get_lang_code($current_locale); $current_lang = $this->get_own_lang_name($current_lang_code); ?>
>


languages->get_lang_code($find_locale); } function get_flag($lang_code) { return '' . $lang_code . ' '; } function get_locale($lang_code) { return $this->languages->get_locale($lang_code); } function get_lang_name($lang_code) { return $this->languages->get_lang_name($lang_code); } function get_own_lang_name($lang_code) { return $this->languages->get_own_lang_name($lang_code); } function get_lang_name_in_current_locale($lang_code) { return $this->languages->get_lang_name_in_current_locale($lang_code); } function get_languages($sort = false) { $langs = $this->languages->get_languages(); if ($sort) { uasort($langs, array($this, 'lang_sort')); } return $langs; } function lang_sort($a, $b) { return ($this->get_lang_name_in_current_locale($a['code']) < $this->get_lang_name_in_current_locale($b['code'])) ? -1 : 1; } function get_major_langs() { return $this->languages->get_major_langs(); } function icl_sw_filters_gettext($translation, $text, $domain, $name = false){ $has_translation = 0; $context = ($domain != 'default') ? 'theme ' . $domain : 'WordPress'; if(empty($name)){ $name = md5($text); } $ret_translation = wp_trans_icl_t($context, $name, $text, $has_translation); if(false === $has_translation){ $ret_translation = $translation; } return $ret_translation; } function icl_sw_filters_gettext_with_context($translation, $text, $_gettext_context, $domain){ // special check for language direction if ($text == 'ltr' && $_gettext_context == 'text direction' && $domain == 'default') { $current_language = $this->get_lang_code(get_locale()); if (in_array($current_language, array('ar','he','fa'))) { return 'rtl'; } } return $this->icl_sw_filters_gettext($translation, $text, $domain, $_gettext_context . ': ' . $text); } function render_lang_switch_popup() { echo '
'; echo '' . __('Fetching language information ...', 'wordpress-language') . ' '; echo '
'; wp_nonce_field('wp_lang_get_lang_info', 'wp_lang_get_lang_info'); echo '
'; } function render_lang_switch_js() { ?>

', ''); ?>


 

load_xml(); $lang_code = $this->get_lang_code($_POST['lang']); $locales = $ST_MO_Downloader->get_locales($lang_code); // Hardcoded default texts language if($lang_code == 'en'){ $locales['en_US'] = true; } if (sizeof($locales) > 1 ) { echo sprintf(__('We found several alternatives for %s translation. Choose which one you want to use:', 'wordpress-language'), $this->get_lang_name_in_current_locale($lang_code)); $default_locale = $this->languages->get_locale($lang_code); ?>
get_lang_name_in_current_locale($lang_code)); echo '

'; $link = admin_url('options-general.php?page=wordpress_language&switch_to=' . $this->get_locale($lang_code) . '&_wpnonce=' . $nonce . '&scope=' . $_POST['scope']); $click = ''; } ?> style="color:#FFFFFF"> get_option('translations'); $ST_MO_Downloader->load_xml(); $ST_MO_Downloader->get_translation_files(); if(isset($_POST['scope']) && $_POST['scope']=='front-end'){ $current_locale = get_option('wp_language_locale_front'); }else{ $current_locale = get_locale(); } if($current_locale == 'en_US') {echo '1'; exit;} $current_lang_code = $this->get_lang_code($current_locale); $translations = $ST_MO_Downloader->get_translations($current_locale, array('types'=>array('admin', 'core'))); if ($translations !== false) { if (isset($translations['new'])) { $ST_MO_Downloader->save_translations($translations['new'], $current_lang_code, $wptranslations[$current_locale]['available']); } if (isset($translations['updated'])) { $ST_MO_Downloader->save_translations($translations['updated'], $current_lang_code, $wptranslations[$current_locale]['available']); } echo '1'; } else { echo '0'; } } die(); } function ajax_check_for_updates() { $nonce = $_POST['_wpnonce']; if (wp_verify_nonce( $nonce, 'wp_lang_get_lang_info') ) { $ST_MO_Downloader = new ST_MO_Downloader(); $ST_MO_Downloader->updates_check(); $wptranslations = $ST_MO_Downloader->get_option('translations'); if($_POST['scope']=='front-end'){ $current_locale = get_option('wp_language_locale_front'); }else{ $current_locale = get_locale(); } $current_lang_code = $this->get_lang_code($current_locale); $this->current_translation_state($current_lang_code, $current_locale, $wptranslations, true); $contents = ob_get_contents(); } die(); } function ajax_show_hide_language_selector() { $nonce = $_POST['_wpnonce']; if (wp_verify_nonce( $nonce, 'wp_lang_get_lang_info') ) { if ($_POST['state'] == 'on') { update_option('wp_language_show_switcher', 'on'); } else { update_option('wp_language_show_switcher', 'off'); } } die(); } function switch_language($lang_code) { $ST_MO_Downloader = new ST_MO_Downloader(); $ST_MO_Downloader->load_xml(); $default_locale = $this->languages->get_locale($lang_code); update_option('wp_language_locale', $default_locale); } function switch_locale($locale) { update_option('wp_language_locale', $locale); } function ajax_wp_languages_distinct_languages(){ if(isset($_POST['value'])){ $this->settings['different_languages'] = intval($_POST['value']); } update_option('wp_languages_options', $this->settings); if(!get_option('wp_language_locale_front')){ update_option('wp_language_locale_front', get_locale()); } $resp = array(); echo json_encode($resp); exit; } } function wp_trans_icl_t($context, $name, $original_value=false, &$has_translation=null, $dont_auto_register = false){ global $wpdb, $WordPress_language; if (!isset($WordPress_language)) { if(isset($has_translation)) $has_translation = false; return $original_value; } $current_language = $WordPress_language->get_lang_code(get_locale()); $default_language = 'en'; if($current_language == $default_language && $original_value){ $ret_val = $original_value; if(isset($has_translation)) $has_translation = false; }else{ $result = wp_trans_cache_lookup($context, $name); $is_string_change = $result !== false && ( $result['translated'] && $result['original'] != $original_value || !$result['translated'] && $result['value'] != $original_value ); if($result === false || is_array($result) && !$result['translated'] && $original_value){ $ret_val = $original_value; if(isset($has_translation)) $has_translation = false; }else{ $ret_val = $result['value']; if(isset($has_translation)) $has_translation = true; } } return $ret_val; } function wp_trans_cache_lookup($context, $name){ global $wpdb, $WordPress_language; static $icl_st_cache; $ret_value = false; if(!isset($icl_st_cache[$context])){ //CACHE MISS (context) $icl_st_cache[$context] = array(); $current_language = $WordPress_language->get_lang_code(get_locale()); // workaround for multi-site setups - part i global $switched, $switched_stack; if(isset($switched) && $switched){ $prev_blog_id = $wpdb->blogid; $wpdb->set_blog_id($switched_stack[0]); } // THE QUERY $res = $wpdb->get_results($wpdb->prepare(" SELECT s.name, s.value, t.value AS translation_value, t.status FROM {$wpdb->prefix}icl_strings s LEFT JOIN {$wpdb->prefix}icl_string_translations t ON s.id = t.string_id WHERE s.context = %s AND (t.language = %s OR t.language IS NULL) ", $context, $current_language), ARRAY_A); // workaround for multi-site setups - part ii if(isset($switched) && $switched){ $wpdb->set_blog_id($prev_blog_id); } // SAVE QUERY RESULTS if($res){ foreach($res as $row){ if($row['status'] != ICL_STRING_TRANSLATION_COMPLETE || empty($row['translation_value'])){ $icl_st_cache[$context][$row['name']]['translated'] = false; $icl_st_cache[$context][$row['name']]['value'] = $row['value']; }else{ $icl_st_cache[$context][$row['name']]['translated'] = true; $icl_st_cache[$context][$row['name']]['value'] = $row['translation_value']; $icl_st_cache[$context][$row['name']]['original'] = $row['value']; } } } } if(isset($icl_st_cache[$context][$name])){ $ret_value = $icl_st_cache[$context][$name]; } return $ret_value; }