Guest User

Untitled

a guest
May 26th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. <?php // encoding: utf-8
  2. /*
  3. Plugin Name: Theme Live Translator
  4. */
  5.  
  6. // enable strftime usage
  7. $conf['use_strftime'] = true;
  8.  
  9. // Date Configuration (uses strftime)
  10. $conf['date_format']['en'] = '%A %B %e%q, %Y';
  11. $conf['date_format']['it'] = '%e %B %Y';
  12.  
  13. $conf['time_format']['en'] = '%I:%M %p';
  14. $conf['time_format']['it'] = '%H:%M';
  15.  
  16. function my_parseURL($url) {
  17. $r = '!(?:(\w+)://)?(?:(\w+)\:(\w+)@)?([^/:]+)?';
  18. $r .= '(?:\:(\d*))?([^#?]+)?(?:\?([^#]+))?(?:#(.+$))?!i';
  19.  
  20. preg_match ( $r, $url, $out );
  21. $result = array(
  22. "scheme" => $out[1],
  23. "host" => $out[4].(($out[5]=='')?'':':'.$out[5]),
  24. "user" => $out[2],
  25. "pass" => $out[3],
  26. "path" => $out[6],
  27. "query" => $out[7],
  28. "fragment" => $out[8]
  29. );
  30. return $result;
  31. }
  32.  
  33. function is_italian() {
  34. session_start();
  35.  
  36. $request_uri = $_SERVER['REQUEST_URI'];
  37. $frags = my_parseURL($request_uri);
  38.  
  39. $forceitalian = strpos($frags['query'], 'lang=it') !== FALSE || strpos($frags['path'], '/it') === 0;
  40. $forceenglish = strpos($frags['query'], 'lang=en') !== FALSE || (!$forceitalian && strpos($frags['path'], '/log') === FALSE && $frags['path'] != "/");
  41.  
  42. if ($forceitalian) {
  43. $_SESSION['locale'] = 'it';
  44. return true;
  45. }
  46. if ($forceenglish) {
  47. $_SESSION['locale'] = 'en';
  48. return false;
  49. }
  50.  
  51. $lang=split('[,;]',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
  52. $lang=strtoupper($lang[0]);
  53. $lang=split('[-]',$lang);
  54. if ($lang[0]=="IT") {
  55. return true;
  56. }
  57.  
  58. if (isset($_SESSION['locale'])) {
  59. return $_SESSION['locale'] === 'it';
  60. }
  61.  
  62. return false;
  63. }
  64.  
  65. function is_english() {
  66. return !is_italian();
  67. }
  68.  
  69. function my_localeForCurrentLanguage($locale){
  70.  
  71. if (is_italian()) {
  72. $locale = array();
  73. $locale[] = "it_IT.utf8";
  74. $locale[] = "it_IT@euro";
  75. $locale[] = "it_IT";
  76. $locale[] = "it-IT.utf8";
  77. $locale[] = "it-IT@euro";
  78. $locale[] = "it-IT";
  79. $locale[] = "it";
  80. setlocale(LC_ALL, $locale);
  81. return "it_IT";
  82. } else {
  83. $locale = array();
  84. $locale[] = "en_US.utf8";
  85. $locale[] = "en_US@euro";
  86. $locale[] = "en_US";
  87. $locale[] = "en";
  88. setlocale(LC_ALL, $locale);
  89. return "en_US";
  90. }
  91. }
  92.  
  93. function my_strftime($format, $date) {
  94. // add date suffix ability (%q) to strftime
  95. $day = intval(trim(strftime("%e",$date)));
  96. $replace = 'th';
  97. if($day==1||$day==21||$day==31) $replace = 'st';
  98. if($day==2||$day==22) $replace = 'nd';
  99. if($day==3||$day==23) $replace = 'rd';
  100. $format = preg_replace("/([^%])%q/","$1".$replace,$format);
  101. return strftime($format, $date);
  102. }
  103.  
  104. function localized_commentdate($old_date, $format ='') {
  105. global $comment;
  106. if (is_english()) {
  107. return ucwords(my_strftime("%A %B %e%q, %Y", mysql2date('U',$comment->comment_date)));
  108. } else {
  109. return ucwords(my_strftime('%A %e %B %Y', mysql2date('U',$comment->comment_date)));
  110. }
  111. }
  112.  
  113. function localized_date($old_date, $format ='', $before = '', $after = '') {
  114. global $post;
  115. if (is_english()) {
  116. return ucwords(my_strftime("%A %B %e%q, %Y", mysql2date('U',$post->post_date)));
  117. } else {
  118. return ucwords(my_strftime('%A %e %B %Y', mysql2date('U',$post->post_date)));
  119. }
  120. }
  121.  
  122. // Hooks (execution time critical filters)
  123. add_filter('locale', 'my_localeForCurrentLanguage',99);
  124. add_filter('get_comment_date', 'localized_commentdate',0,2);
  125. add_filter('the_date', 'localized_date',0,4);
  126.  
  127. ?>
Add Comment
Please, Sign In to add comment