<?php
/*
Plugin Name: Language Switcher
Version:     1.1.3
Plugin URI:  http://yskin.net/projects/language-switcher/
Description: Switch language of WordPress easily in your WordPress admin panel.
Author:      Yskin
Author URI:  http://yskin.net/

License
=============================================================================
Copyright (C) 2006-2007 yskin (email: yskins@gmail.com)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

register_activation_hook(__FILE__'language_switcher_activate');
function 
language_switcher_activate() {
    if (
get_option("WPLANG") === false)
        
add_option("WPLANG"WPLANG);
}

//If the variable $locale has already been set, WordPress will use $locale instead of constant WPLANG.
$locale get_option("WPLANG");
if ( empty(
$locale) ) //Empty means default language: en_US.
    
$locale 'en_US';

load_plugin_textdomain('langsw');

add_action('admin_menu''language_switcher_add_option_page');
function 
language_switcher_add_option_page() {
    if ( 
function_exists('add_options_page') ) {
         
add_options_page('Language Switcher Setting'__('Language''langsw'), 8__FILE__'language_switcher_option_page');
    }
}

function 
language_switcher_option_page() {
    if ( isset(
$_POST['Submit']) ) {
        
update_option('WPLANG', (string)$_POST['WPLANG']);
        
?>
        <div id="message" class="updated fade"><p><?php _e('Language has been changed, page will refresh after 5 seconds...''langsw'?></p></div>
        <script language="javascript" type="text/javascript">
            function beginrefresh() {
                window.location.href = window.location.href;
            }
            setTimeout("beginrefresh()", 5000);
        </script>
        <?php
    
}

    
//Current language in WordPress
    
$wplang get_option("WPLANG");

    
//Main language files
    
$main_lang_files glob(ABSPATH LANGDIR '/*.mo');

    
//Template language files
    
language_switcher_files_scan(TEMPLATEPATH '/''mo'$template_lang_files);

    
//Plugin language files
    
language_switcher_files_scan(ABSPATH 'wp-content/plugins/''mo'$plugin_lang_files);

    
//Active plugin language files
    
global $l10n;

    if ( isset(
$l10n) ) {
        
$active_plugin_lang_files array_keys($l10n);
        if ( (
$key array_search('default'$active_plugin_lang_files)) !== false )
            unset(
$active_plugin_lang_files[$key]);

        
//Add compatibility with Google (XML) Sitemaps plugin
        
if ( class_exists('GoogleSitemapGenerator') )
            
$active_plugin_lang_files[] = 'sitemap';

        foreach (
$active_plugin_lang_files as $key => $v)
            
$active_plugin_lang_files[$key] = $v '-' $wplang;
    } else {
        
$active_plugin_lang_files = array();
    }

    
$langs = array(); //Languages which can be used

    
foreach ( (array)$main_lang_files as $main_lang_file )
        
$langs[] = basename($main_lang_file'.mo');

    foreach ( (array)
$template_lang_files as $template_lang_file )
        
$langs[] = basename($template_lang_file'.mo');

    foreach ( (array)
$plugin_lang_files as $plugin_lang_file ) {
        
$v basename($plugin_lang_file'.mo');
        if ( (
$pos strrpos($v'-')) !==  false)
            
$langs[] = substr($v$pos 1);
    }

    
$langs array_unique($langs);
    
sort($langs);
    if ( (
$key array_search(''$langs)) !== false )
        unset(
$langs[$key]);
    
?>

    <div class='wrap'>
        <h2 id="edit-settings"><?php _e('Language Switcher''langsw'?></h2>
        <form style="margin-left: 40px" method="post" action="<?php echo attribute_escape($_SERVER["REQUEST_URI"]) ?>">
        <?php _e('Current Language: ''langsw'?>
        <select name="WPLANG" id="WPLANG">
            <option value=""><?php _e('Default''langsw'?></option>
            <?php
            
foreach ( $langs as $lang ) {
                echo 
"<option value='$lang'";
                
selected($wplang$lang);
                echo 
">$lang</option>\n";
            }
            
?>
        </select>
        <p class="submit"><input type="submit" name="Submit" value="<?php _e('Update Options &raquo;'?>" /></p>
        </form>
    </div>

    <div class='wrap'>
        <h2><?php _e('Main language files''langsw'?></h2>
        <?php
        
if ( empty($main_lang_files) )
            
_e('No available main language file.''langsw');
        else
            
language_switcher_mo_info($main_lang_files, array($wplang));
        
?>
    </div>

    <div class='wrap'>
        <h2><?php _e('Template language files''langsw'?></h2>
        <?php
        
if ( empty($template_lang_files) )
            
_e('No available template language file.''langsw');
        else
            
language_switcher_mo_info($template_lang_files, array($wplang));
        
?>
    </div>

    <div class='wrap'>
        <h2><?php _e('Plugin language files''langsw'?></h2>
        <?php
        
if ( empty($plugin_lang_files) )
            
_e('No available plugin language file.''langsw');
        else
            
language_switcher_mo_info($plugin_lang_files$active_plugin_lang_files);
        
?>
    </div>
    <?php
}

function 
language_switcher_files_scan($path$ext false, &$files) {
    if ( (
$dir = @dir($path)) !== false ) {
        while ( (
$file $dir->read()) !== false) {
            
$file_path $path $file;
            if ( 
is_dir($file_path) && !($file == '.' or $file == '..') ) {
                
language_switcher_files_scan($file_path '/'$ext$files);
            } elseif (
is_file($file_path) && (!$ext || preg_match('/\.' $ext '$/i'$file)) ) {
                
$files[] = $file_path;
            }
        }
        
$dir->close();
    }
}

function 
language_switcher_mo_info_li($lang_file$active_lang_files) {
    if ( 
is_readable($lang_file) )
        
$input = new CachedFileReader($lang_file);
    else
        return;

    
$ls_l10n = new gettext_reader($input);

    
$total $ls_l10n->total 1//Subtract the empty string.
    
$info $ls_l10n->translate(''); //Get information from the translation of the empty string.

    
echo '<li><p>';
    echo 
'<strong title="' str_replace("\n"'&#10;'trim($info)) . '">' str_replace(ABSPATH''$lang_file) . '</strong>';
    if ( 
in_array(basename($lang_file'.mo'), $active_lang_files) )
        echo 
' <small style="color: red">' __('(active)''langsw') . '</small>';
    echo 
"</p>\n";

    if ( 
$ls_l10n->error ) {
        
$error_str __('Not a valid mo file.''langsw');
        
$img_url get_option('home') . '/wp-includes/images/smilies/icon_exclaim.gif';
        echo 
"<p><img alt='$error_st' src='$img_url' style='vertical-align: text-bottom;' /><span style='color:red'>$error_str</span></p>\n";
        return;
    }

    
$name = array(
        
__('Project ID',         'langsw'),
        
__('Revision Date',      'langsw'),
        
__('Translator',         'langsw'),
        
__('Language Team',      'langsw'),
        
__('Country',            'langsw'),
        
__('Language',           'langsw'),
        
__('Charset',            'langsw'),
        
__('Total string count''langsw')
    );

    
$value = array();
    if ( 
preg_match("|^Project-Id-Version:[\s*](.+)$|im"$info$match) != )
        
$value[0] = $match[1];
    if ( 
preg_match("|^PO-Revision-Date:[\s*](.+)$|im"$info$match) != )
        
$value[1] = $match[1];
    if ( 
preg_match("|^Last-Translator:[\s*](.+)$|im"$info$match) != )
        
$value[2] = $match[1];
    if ( 
preg_match("|^Language-Team:[\s*](.+)$|im"$info$match) != )
        
$value[3] = $match[1];
    if ( 
preg_match("|^X-Poedit-Country:[\s*](.+)$|im"$info$match) != )
        
$value[4] = $match[1];
    if ( 
preg_match("|^X-Poedit-Language:[\s*](.+)$|im"$info$match) != )
        
$value[5] = $match[1];
    if ( 
preg_match("|charset=(.+)$|im"$info$match) != )
        
$value[6] = strtoupper($match[1]);
    
$value[7] = $total;

    
$output = array();
    foreach ( 
$value as $key => $v )
        
$output[] = '<strong>' $name[$key] . __(': ''langsw') . '</strong>' htmlspecialchars($v);

    echo 
'<p>' implode($output"<br />\n") . "</p>\n";
    echo 
"</li>\n";
}

function 
language_switcher_mo_info($lang_files$active_lang_files) {
    echo 
"<ul>\n";
    foreach (
$lang_files as $lang_file)
        
language_switcher_mo_info_li($lang_file$active_lang_files);
    echo 
"</ul>\n";
}

?>