Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php // encoding: utf-8
- /*
- Plugin Name: Theme Live Translator
- */
- // enable strftime usage
- $conf['use_strftime'] = true;
- // Date Configuration (uses strftime)
- $conf['date_format']['en'] = '%A %B %e%q, %Y';
- $conf['date_format']['it'] = '%e %B %Y';
- $conf['time_format']['en'] = '%I:%M %p';
- $conf['time_format']['it'] = '%H:%M';
- function my_parseURL($url) {
- $r = '!(?:(\w+)://)?(?:(\w+)\:(\w+)@)?([^/:]+)?';
- $r .= '(?:\:(\d*))?([^#?]+)?(?:\?([^#]+))?(?:#(.+$))?!i';
- preg_match ( $r, $url, $out );
- $result = array(
- "scheme" => $out[1],
- "host" => $out[4].(($out[5]=='')?'':':'.$out[5]),
- "user" => $out[2],
- "pass" => $out[3],
- "path" => $out[6],
- "query" => $out[7],
- "fragment" => $out[8]
- );
- return $result;
- }
- function is_italian() {
- session_start();
- $request_uri = $_SERVER['REQUEST_URI'];
- $frags = my_parseURL($request_uri);
- $forceitalian = strpos($frags['query'], 'lang=it') !== FALSE || strpos($frags['path'], '/it') === 0;
- $forceenglish = strpos($frags['query'], 'lang=en') !== FALSE || (!$forceitalian && strpos($frags['path'], '/log') === FALSE && $frags['path'] != "/");
- if ($forceitalian) {
- $_SESSION['locale'] = 'it';
- return true;
- }
- if ($forceenglish) {
- $_SESSION['locale'] = 'en';
- return false;
- }
- $lang=split('[,;]',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
- $lang=strtoupper($lang[0]);
- $lang=split('[-]',$lang);
- if ($lang[0]=="IT") {
- return true;
- }
- if (isset($_SESSION['locale'])) {
- return $_SESSION['locale'] === 'it';
- }
- return false;
- }
- function is_english() {
- return !is_italian();
- }
- function my_localeForCurrentLanguage($locale){
- if (is_italian()) {
- $locale = array();
- $locale[] = "it_IT.utf8";
- $locale[] = "it_IT@euro";
- $locale[] = "it_IT";
- $locale[] = "it-IT.utf8";
- $locale[] = "it-IT@euro";
- $locale[] = "it-IT";
- $locale[] = "it";
- setlocale(LC_ALL, $locale);
- return "it_IT";
- } else {
- $locale = array();
- $locale[] = "en_US.utf8";
- $locale[] = "en_US@euro";
- $locale[] = "en_US";
- $locale[] = "en";
- setlocale(LC_ALL, $locale);
- return "en_US";
- }
- }
- function my_strftime($format, $date) {
- // add date suffix ability (%q) to strftime
- $day = intval(trim(strftime("%e",$date)));
- $replace = 'th';
- if($day==1||$day==21||$day==31) $replace = 'st';
- if($day==2||$day==22) $replace = 'nd';
- if($day==3||$day==23) $replace = 'rd';
- $format = preg_replace("/([^%])%q/","$1".$replace,$format);
- return strftime($format, $date);
- }
- function localized_commentdate($old_date, $format ='') {
- global $comment;
- if (is_english()) {
- return ucwords(my_strftime("%A %B %e%q, %Y", mysql2date('U',$comment->comment_date)));
- } else {
- return ucwords(my_strftime('%A %e %B %Y', mysql2date('U',$comment->comment_date)));
- }
- }
- function localized_date($old_date, $format ='', $before = '', $after = '') {
- global $post;
- if (is_english()) {
- return ucwords(my_strftime("%A %B %e%q, %Y", mysql2date('U',$post->post_date)));
- } else {
- return ucwords(my_strftime('%A %e %B %Y', mysql2date('U',$post->post_date)));
- }
- }
- // Hooks (execution time critical filters)
- add_filter('locale', 'my_localeForCurrentLanguage',99);
- add_filter('get_comment_date', 'localized_commentdate',0,2);
- add_filter('the_date', 'localized_date',0,4);
- ?>
Add Comment
Please, Sign In to add comment