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 3 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, see .
Plugin Name: WP Translit
Plugin URI: http://blog.urosevic.net/wordpress/wp-translit/
Description: Transliterate text from Serbian Cyrillic to Latin script in posts and pages.
Author: Aleksandar Urošević
Version: 0.3.7.1
Author URI: http://urosevic.net
Thanks to:
http://www.emanueleferonato.com/2008/02/15/how-to-create-a-wordpress-widget/
http://lonewolf-online.net/computers/wordpress/create-widgets-control-panels/
http://kimmo.suominen.com/sw/srlatin/
http://us3.php.net/ob_start
*/
add_action('init', 'wpt_init');
add_action('plugins_loaded', 'wpt_register_widget');
add_action('translit', 'wpt_translit');
function wpt_init()
{
// setup textdomain for localisation
load_plugin_textdomain( 'wpt', PLUGINDIR . '/' . dirname(plugin_basename(__FILE__)) . '/lang' );
// get default language from HTTP headers
hdr_lang();
// determine output script
wpt_set_lang();
}
function wpt_register_widget()
{
register_sidebar_widget('WP Translit', 'wpt_widget');
register_widget_control('WP Translit', 'wpt_widget_control');
}
function wpt_widget_control()
{
// Configuration panel for WP Translit Widget
$options = get_option("widget_wpt");
if (!is_array( $options ))
{
$options = array(
'title' => 'Избор писма',
'style' => 'list'
);
}
if ($_POST['wpt-Submit'])
{
$options['title'] = htmlspecialchars($_POST['wpt-wTitle']);
$options['style'] = htmlspecialchars($_POST['wpt-wStyle']);
update_option("widget_wpt", $options);
}
// Form generator
?>
EOF;
} // wpt_widget_links()
class wp_translit
{
function wp_translit()
{
add_action('wp_head', array(&$this,'buffer_start'));
add_action('wp_footer', array(&$this,'buffer_end'));
// add transliteration to feed
add_action('feed_head', array(&$this,'buffer_start'), 1);
add_action('feed_footer', array(&$this,'buffer_end'), 1);
add_action('rss_head', array(&$this,'buffer_start'), 1);
add_action('rss_footer', array(&$this,'buffer_end'), 1);
add_action('rss2_head', array(&$this,'buffer_start'), 1);
add_action('rss2_footer', array(&$this,'buffer_end'), 1);
}
function buffer_start()
{
ob_start( array(&$this,"do_wptranslit") );
}
function buffer_end()
{
ob_end_flush();
}
// Function to do replace of text
function do_wptranslit($text)
{
// set default script to 'cir' (put in widget setup?)
$wpt_lang = "cir";
// get language from REQUEST (if 'lng' exists)
if ( isset($_REQUEST['lng']) )
{
$wpt_lang = $_REQUEST['lng'];
}
// if no language in REQUEST, get language from cookies (if cookie 'wpt_lang' exists)
elseif ( isset($_COOKIE['wpt_lang']) )
{
$wpt_lang = $_COOKIE['wpt_lang'];
}
// if no wpt_lang in cookies, get Accept Language from headers
elseif ( $GLOBALS['hdr_lang'] )
{
$wpt_lang = $GLOBALS['hdr_lang'];
}
// Do we even need to do transliteration (wpt_lang is 'lat')?
if ( $wpt_lang == "lat" )
{
$wpt_izlaz = "";
// set source script - Cyrillic
$str_from = array ("Џа", "Џе", "Џи", "Џо", "Џу", "Ња", "Ње", "Њи", "Њо", "Њу", "Ља", "Ље", "Љи", "Љо", "Љу", "а","б","в","г","д","ђ","е","ж","з","и","ј","к","л","љ","м","н","њ","о","п","р","с","т","ћ","у","ф","х","ц","ч","џ","ш","А","Б","В","Г","Д","Ђ","Е","Ж","З","И","Ј","К","Л","Љ","М","Н","Њ","О","П","Р","С","Т","Ћ","У","Ф","Х","Ц","Ч","Џ","Ш","č","Č","ć","Ć","ž","Ž","đ","Đ","š","Š");
// set destination script to UTF-8 Serbian Latin
$str_to = array ("Dža", "Dže", "Dži", "Džo", "Džu", "Nja", "Nje", "Nji", "Njo", "Nju", "Lja", "Lje", "Lji", "Ljo", "Lju", "a","b","v","g","d","đ","e","ž","z","i","j","k","l","lj","m","n","nj","o","p","r","s","t","ć","u","f","h","c","č","dž","š","A","B","V","G","D","Đ","E","Ž","Z","I","J","K","L","Lj","M","N","Nj","O","P","R","S","T","Ć","U","F","H","C","Č","Dž","Š","č","Č","ć","Ć","ž","Ž","đ","Đ","š","Š");
// do simple string replace
return str_replace($str_from, $str_to, $text);
} else {
// if there no need for transliteration, print out unchanged text
return $text;
} // wpt_lang == lat
} // function do_wptranslit()
} // class wp_translit
// Function to get language from Accept Language headers
function hdr_lang() {
// get language from HTTP headers and split it to array
$languages = split(',', $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
$hdr_lang = "";
// check first defined Accept Language
$hlang = $languages[0];
if ( ereg("^(sr|mk|bg|ru)", $hlang) && $hbr == 0 )
{
// for Serbian, Macedonian, Bulgarian and Russian set 'cir'
$hdr_lang = "cir";
} else {
// for all other set to 'lat'
$hdr_lang = "lat";
}
// set global variable
$GLOBALS['hdr_lang'] = $hdr_lang;
}
// Function to set language from request headers to cookies
function wpt_set_lang()
{
$lng = "";
$wpt_lang = "";
if ( isset($_REQUEST['lng']) )
{
$wpt_lang = $_REQUEST['lng'];
if ( $wpt_lang == "cir" || $wpt_lang == "lat" )
{
setcookie("wpt_lang", $wpt_lang, strtotime("+3 months"), "/");
}
}
}
$_wp_wp_translit =& new wp_translit;
?>