<lang_xx> and </lang_xx>, where xx is an ISO language code such as "en" or "de". Note: Both languages will show up in automagically generated RSS excerpts unless you manually specify an excerpt in one language. Based on Language Picker by Jason. Version: 0.3 Author: Martin Chlupac Author URI: http://fredfred.net/skriker/ Update: http://fredfred.net/skriker/plugin-update.php?p=198 */ // Default language for text not enclosed in $default_lang = 'es'; // Name of cookie - default:'wp_lang_pref'.$cookiehash $lang_pref_cookie = 'wp_lang_pref'.$cookiehash; // Use cruft-free URLs to view other languages? MUST ADD REWRITE RULES: // RewriteRule ^(.*)/xx/?$ /$1?lp_lang_view=xx [QSA] // above should be modified to match your other rewrite rules and added // for each language 'xx' you will be using. //this is not tested now - as I am not allowed to use mod_rewrite rules by my webhosting company $lang_rewrite = false; // Should Polyglot attempt to dynamically change the WP locale // setting based on user's prefered language? Note, for this to work // .mo files must be renamed to match the languages in use on your // blog. For example, de_DE.mo might become de.mo. $lang_change_locale = TRUE; //You can define your own translations of language shortcuts $trans['cz'] = 'Äesky';//warning I am not using ISO code for czech - look bellow for awful hack! But you should use ISO code for your languages! $trans['en'] = 'english'; $trans['it'] = 'italiano'; $trans['de'] = 'deutch'; $trans['sv'] = 'svenska'; $trans['es'] = 'español'; $trans['eu'] = 'euskera'; //Which languages are we supporting: there must be ISO codes! $knownlangs = array('es','eu','en');// //-----------------STOP EDITING HERE!------- // Initialization stuff $lp_lang_pref = (isset($HTTP_COOKIE_VARS[$lang_pref_cookie])) ? trim($HTTP_COOKIE_VARS[$lang_pref_cookie]) : FALSE; if ($_GET["lp_lang_pref"]) { setcookie($lang_pref_cookie, $_GET["lp_lang_pref"], time() + 30000000, COOKIEPATH); $lp_lang_pref=$_GET["lp_lang_pref"]; } $lp_lang_pref = ($_GET["lp_lang_view"]) ? $_GET["lp_lang_view"] : $lp_lang_pref; if ($lp_lang_pref_not_set = (!$lp_lang_pref)) $lp_lang_pref = get_users_pref_lang();//fred //awful hack for czech language as I am using 'cz' code instead of 'cs' //This must be solved in some clear way in the future, because people usually prefer some other codes than ISO...:o( if($lp_lang_pref == 'cs') $lp_lang_pref = 'cz'; add_filter('the_content', 'lang_picker_respect_more',1); add_filter('the_title', 'lang_picker_respect_more',1); add_filter('single_post_title', 'lang_picker_respect_more',1); add_filter('the_content_rss', 'lang_picker_respect_more',1); if ($lang_change_locale && class_exists('streamreader')){ $locale = $lp_lang_pref; $mofile = ABSPATH . WPINC . "/languages/$locale.mo"; if ( is_readable($mofile) && ($locale != 'en_US') ) { $input = new FileReader($mofile); } else { $input = false; } if(get_settings('version')<'1.5'){ $l10n['default'] = new gettext_reader($input);//fred for WP1.5 or later } else{ $l10n['default'] = new gettext_reader($input);//fred for WP1.5 or later } require(ABSPATH . WPINC . '/locale.php'); } if (!defined('WPLANG')){ define('WPLANG', $lp_lang_pref); } function get_trans($lang){ global $trans; if(IsSet($trans[$lang])) return $trans[$lang]; else return $lang; } function lang_picker($content) { global $lp_lang_pref; global $default_lang; if (preg_match_all ( '//', $content , $match, PREG_PATTERN_ORDER)) { if ($lp_lang_pref && lang_exists($lp_lang_pref,$content)){ $content=str_replace("","",$content); $content=str_replace("","",$content); $find = "/(?s)(.*?)<\/lang_".$lp_lang_pref.">/"; preg_match_all ( $find, $content , $match, PREG_PATTERN_ORDER); $content = implode('',$match[1]); } elseif(lang_exists($default_lang,$content)){ $content=str_replace("","",$content); $content=str_replace("","",$content); $find = "/(?s)(.*?)<\/lang_".$default_lang.">/"; preg_match_all ( $find, $content , $match, PREG_PATTERN_ORDER); $content = implode('',$match[1]); } else { //if not, use default $content=str_replace("","",$content); $content=str_replace("","",$content); $find = "/(?s)(.*?)<\/lang_(.*?)>/"; $replace = ""; $content = preg_replace($find,$replace,$content); } } return $content; } function lang_exists($lang, $content){ return !(strpos($content, "")===FALSE); } //fred---------------------- function lang_picker_respect_more($text) {//older version doesn't work with in the text GLOBAL $lp_lang_pref; // $text = preg_replace ('/(
.*?<\/div>)/i',"\\1",$text,1); $text = preg_replace ('/()/i',"\\1",$text); $text = preg_replace ('/(<\/lang_..>)/i',"\\1",$text); $text = ''.$text.''; return str_replace("","",str_replace("","",lang_picker($text)) ); } function safe_link(){ global $post; $idpost = $post; $permalink = get_settings('home') . '/' . get_settings('blogfilename') . '?p=' . $idpost->ID; if ($idpost->post_status == 'static') $permalink .= '&static=1'; return $permalink; } //-------------------------- function lp_other_langs($none='none', $before='
    ', $after='
', $pre='
  • (', $post=')
  • '){ global $page, $pages, $lp_lang_pref, $default_lang, $lang_rewrite; $content = $pages[$page-1].get_the_title();//fred $link = safe_link(); $before = lang_picker($before); $after = lang_picker($after); $pre = lang_picker($pre); $post = lang_picker($post); $other_langs = lang_picker($none); if (preg_match_all ( '//', $content , $match, PREG_PATTERN_ORDER)) { $match = array_unique($match[1]); $other_langs = $before; if ($lp_lang_pref != $default_lang && !in_array($default_lang, $match)) { if ('' != get_settings('permalink_structure') && $lang_rewrite) { $other_langs .= "$pre".get_trans($default_lang)."$post";//fred } else { $other_langs .= "$pre".get_trans($default_lang)."$post"; } } foreach ($match as $lang){ if ($lp_lang_pref != $lang) { if ('' != get_settings('permalink_structure') && $lang_rewrite) { $other_langs .= "$pre".get_trans($lang)."$post";//fred } else { $other_langs .= "$pre".get_trans($lang)."$post";//fred ?-> & } } } if($other_langs != $before)//fred to avoid empty
      $other_langs .= $after; else $other_langs = $none; } echo $other_langs; } function lp($todo='',$return=0){ global $lp_running; if ($todo=='start' OR (!$todo && !$lp_running)){ ob_start("lang_picker"); $lp_running=TRUE; } else if ($todo=='stop' OR (!$todo && $lp_running)) { $lp_running=FALSE; ob_end_flush(); } else if ($todo && $return) { return lang_picker($todo); } else if ($todo) { echo lang_picker($todo); } } //------------fred------------------- function get_users_pref_lang(){ GLOBAL $default_lang,$knownlangs; $deflang = $default_lang; # An advanced PHP Language Detection Script # by Fibergeek (fibergeek @ codegurus.be) # # This script is a modification to a script I found via google # # 2003-12-18 - Version 1 (I added...) # - you can now also specify the country code in the $knowlangs array # - extraction of the country code ($country) # - I fully documented the code # # 2004-01-07 - Version 2 (I added...) # - you can now make $knowlangs NULL (the script will set to array to all known languages (ISO-639)) # # NOTE : for those using Brion's script, I renamed $lastquality to $quality # # Found at URL : http://mail.wikipedia.org/pipermail/wikitech-l/2002-October/001068.html # Assumed creator : Brion VIBBER (wikitech-l@wikipedia.org) # HTTP reference : RFC 2616 - ftp://ftp.isi.edu/in-notes/rfc2616.txt # ================================================================================================== # TODO # You need to add the 2 lines to your source code, and change the content of them of course ;-) # ================================================================================================== #$knownlangs = NULL; // default to all known languages (ISO-639) #$deflang = NULL; // there is no default language # #$knownlangs = array('en-us', 'en-uk', 'en', 'nl'); // We know : English (US & UK) and Dutch #$deflang = 'en-uk'; // In case of an error, we default to English UK # ================================================================================================== # INFORMATION # The following variables are returned by this program # $lclist : the full list with the languages accepted by the client's browser # $lctag : the tag we are supporting (includes the country code if any) # $quality : the quality # $lang : the language code # $country : the country code (if any) # # The following variables are used by this program (and may be overwritten if you use them also!) # $langtag # $qvalue # $eachbit # $tmppos # $tmplclist # $tmplctag # $tmptagarray # ================================================================================================== # ================================================================================================== # THE SCRIPT # ================================================================================================== # Check that $knowlangs is an array! # NOTE : the list is taken from this URL : http://www.w3.org/WAI/ER/IG/ert/iso639.htm if(!is_array($knownlangs)) { if(is_string($knownlangs) || is_object($knownlangs) || $knownlangs != NULL) die("Fibergeek's PHP Language Detection Script : You need define \$knownlangs as an array or as NULL!
      \n"); $knownlangs = array('aa', 'ab', 'af', 'am', 'ar', 'as', 'ay', 'az', 'ba', 'be', 'bg', 'bh', 'bi', 'bn', 'bo', 'br', 'ca', 'co', 'cs', 'cy', 'da', 'de', 'dz', 'el', 'en', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fj', 'fo', 'fr', 'fy', 'ga', 'gd', 'gl', 'gn', 'gu', 'ha', 'hi', 'hr', 'hu', 'hy', 'ia', 'ie', 'ik', 'in', 'is', 'it', 'iw', 'ja', 'ji', 'jw', 'ka', 'kk', 'kl', 'km', 'kn', 'ko', 'ks', 'ku', 'ky', 'la', 'ln', 'lo', 'lt', 'lv', 'mg', 'mi', 'mk', 'ml', 'mn', 'mo', 'mr', 'ms', 'mt', 'my', 'na', 'ne', 'nl', 'no', 'oc', 'om', 'or', 'pa', 'pl', 'ps', 'pt', 'qu', 'rm', 'rn', 'ro', 'ru', 'rw', 'sa', 'sd', 'sg', 'sh', 'si', 'sk', 'sl', 'sm', 'sn', 'so', 'sq', 'sr', 'ss', 'st', 'su', 'sv', 'sw', 'ta', 'te', 'tg', 'th', 'ti', 'tk', 'tl', 'tn', 'to', 'tr', 'ts', 'tt', 'tw', 'uk', 'ur', 'uz', 'vi', 'vo', 'wo', 'xh', 'yo', 'zh', 'zu'); } # Initalisation of the default variables if(isset($HTTP_SERVER_VARS)) $lclist = trim($HTTP_SERVER_VARS['HTTP_ACCEPT_LANGUAGE']); # PHP running from a server else $lclist = trim($_SERVER['HTTP_ACCEPT_LANGUAGE']); # PHP 5.0 when the old server variables disabled $lctag = $deflang; $tmppos = strpos($lctag, '-'); $quality = 0.0; if($tmppos == FALSE or $tmppos == NULL) { $lang = $deflang; if($deflang == NULL) $country = NULL; else $country = ''; } else { $lang = substr($lctag, 0, $tmppos); $country = substr($lctag, $tmppos + 1); echo "lang sub = $lang"; } # $langtag = '((?:[a-zA-Z]{1,8})(?:-[a-zA-Z]{1,8})*)'; $qvalue ='(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3}))'; $eachbit = '^' . $langtag . '(?:;q=' . $qvalue . ')?(?:,\s*)?(.*)$'; # $tmplclist = $lclist; # The loop while(strlen($tmplclist)) { if(preg_match("/$eachbit/", $tmplclist, $tmptagarray)) # requires 3.0.9 { # Extract data from the tag list $tmplctag = $tmptagarray[1]; $tmpquality = $tmptagarray[2]; if(strlen($tmpquality) == 0) $tmpquality = 1; $tmplclist = $tmptagarray[3]; # Find the tag in our $knownlangs array (this search includes the country code if set) if(in_array($tmplctag, $knownlangs) and $tmpquality > $quality) { # Extract the language & quality $lctag = $tmplctag; $lang = $tmplctag; $country = ''; $quality = $tmpquality; } else { # Not found, does the tag include a country code? $tmppos = strpos($tmplctag, '-'); if($tmppos <> FALSE) { # OK, the tag includes a country code but it's not in the $knownlangs array, let's extract the language and look it up if(in_array(substr($tmplctag, 0, $tmppos), $knownlangs) and $tmpquality > $quality) { # Extract the language, country & quality $lctag = $tmplctag; $lang = substr($tmplctag, 0, $tmppos); $country = substr($tmplctag, $tmppos + 1); $quality = $tmpquality; } } } } else { # There was an error, abort the loop break; } } return $lang; }//get_users_pref_lang() ?>