We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29988
    • 89 Posts
    *** EXPERIMENTAL VERSION ***

    This plugin will become handy for some non-english users.

    It will automatically translate everything in <autoutf8>...</autoutf8> to UTF-8. This version has Czech translation tables for ISO-8859-2 and Windows-1250, but you can easily find your own languages’. It also changes the META/XML charset to UTF-8 in the whole document. So if there is a meta header od <?xml ... ?> definition with non-UTF identifier, it will replace it to UTF.

    BTW, the holy grail of charset translation is in The EXACT translation tables - don’t take the first one you find on the internet... We in czech republic use 3 different charsets for one language and its BIG BIG HELL. These tables seems to be good.

    This is useful for example in case when you run a site on ISO-8859-2, but want some pages to be in UTF. The initial reason for this was a fact that I’ve seen some XML to be treaten invalid when there is anything else than UTF charset in the header declaration.

    I use this plugin to produce RSS in UTF. The calling goes like this for me:

    <autoutf8>
    [!Ditto? &tpl=`DittoTemplate` &format=`rss` &language=`czech` &displayArchive=`0` &startID=`6,2,9,5,7,8` &summarize=`3` &multiLevel=`1` &hideFolders=1!]</autoutf8>

    Use OnWebPagePrerender event with this plugin.

     /**
     *	AutoUTF8Czech
     *
     */
     
    
    
    /** 
     * AutoCzech by dgx 
     * 
     * czech & slovak charset detection and conversion 
     * ----------------------------------------------- 
     * 
     * This source file is subject to the GNU GPL license. 
     * 
     * @author     David Grudl aka -dgx- <[email protected]> 
     * @link       http://www.dgx.cz/ 
     * @copyright  Copyright (c) 2006 David Grudl 
     * @license    GNU GENERAL PUBLIC LICENSE 
     * @category   Text 
     * @version    1.1 
     */ 
    
    
    
    // charset detection 
    function detect($s) 
    { 
        if (preg_match('#[\x80-\x{1FF}\x{2000}-\x{3FFF}]#u', $s)) 
            return 'UTF-8'; 
    
        if (preg_match('#[\x7F-\x9F]#', $s)) 
            return 'WINDOWS-1250'; 
    
        return 'ISO-8859-2'; 
    } 
    
    // convert to UTF-8 (for PHP4 without iconv) 
    function autoUTF_PHP4($s) 
    { 
        // detect UTF-8 
        if (preg_match('#[\x80-\x{1FF}\x{2000}-\x{3FFF}]#u', $s)) 
            return $s; 
    
        // detect WINDOWS-1250 
        if (preg_match('#[\x7F-\x9F]#', $s)) 
            return strtr($s, array("\x80"=>"\xe2\x82\xac","\x81"=>"","\x82"=>"\xe2\x80\x9a","\x83"=>"","\x84"=>"\xe2\x80\x9e","\x85"=>"\xe2\x80\xa6","\x86"=>"\xe2\x80\xa0","\x87"=>"\xe2\x80\xa1","\x88"=>"","\x89"=>"\xe2\x80\xb0","\x8a"=>"\xc5\xa0","\x8b"=>"\xe2\x80\xb9","\x8c"=>"\xc5\x9a","\x8d"=>"\xc5\xa4","\x8e"=>"\xc5\xbd","\x8f"=>"\xc5\xb9","\x90"=>"","\x91"=>"\xe2\x80\x98","\x92"=>"\xe2\x80\x99","\x93"=>"\xe2\x80\x9c","\x94"=>"\xe2\x80\x9d","\x95"=>"\xe2\x80\xa2","\x96"=>"\xe2\x80\x93","\x97"=>"\xe2\x80\x94","\x98"=>"","\x99"=>"\xe2\x84\xa2","\x9a"=>"\xc5\xa1","\x9b"=>"\xe2\x80\xba","\x9c"=>"\xc5\x9b","\x9d"=>"\xc5\xa5","\x9e"=>"\xc5\xbe","\x9f"=>"\xc5\xba","\xa0"=>"\xc2\xa0","\xa1"=>"\xcb\x87","\xa2"=>"\xcb\x98","\xa3"=>"\xc5\x81","\xa4"=>"\xc2\xa4","\xa5"=>"\xc4\x84","\xa6"=>"\xc2\xa6","\xa7"=>"\xc2\xa7","\xa8"=>"\xc2\xa8","\xa9"=>"\xc2\xa9","\xaa"=>"\xc5\x9e","\xab"=>"\xc2\xab","\xac"=>"\xc2\xac","\xad"=>"\xc2\xad","\xae"=>"\xc2\xae","\xaf"=>"\xc5\xbb","\xb0"=>"\xc2\xb0","\xb1"=>"\xc2\xb1","\xb2"=>"\xcb\x9b","\xb3"=>"\xc5\x82","\xb4"=>"\xc2\xb4","\xb5"=>"\xc2\xb5","\xb6"=>"\xc2\xb6","\xb7"=>"\xc2\xb7","\xb8"=>"\xc2\xb8","\xb9"=>"\xc4\x85","\xba"=>"\xc5\x9f","\xbb"=>"\xc2\xbb","\xbc"=>"\xc4\xbd","\xbd"=>"\xcb\x9d","\xbe"=>"\xc4\xbe","\xbf"=>"\xc5\xbc","\xc0"=>"\xc5\x94","\xc1"=>"\xc3\x81","\xc2"=>"\xc3\x82","\xc3"=>"\xc4\x82","\xc4"=>"\xc3\x84","\xc5"=>"\xc4\xb9","\xc6"=>"\xc4\x86","\xc7"=>"\xc3\x87","\xc8"=>"\xc4\x8c","\xc9"=>"\xc3\x89","\xca"=>"\xc4\x98","\xcb"=>"\xc3\x8b","\xcc"=>"\xc4\x9a","\xcd"=>"\xc3\x8d","\xce"=>"\xc3\x8e","\xcf"=>"\xc4\x8e","\xd0"=>"\xc4\x90","\xd1"=>"\xc5\x83","\xd2"=>"\xc5\x87","\xd3"=>"\xc3\x93","\xd4"=>"\xc3\x94","\xd5"=>"\xc5\x90","\xd6"=>"\xc3\x96","\xd7"=>"\xc3\x97","\xd8"=>"\xc5\x98","\xd9"=>"\xc5\xae","\xda"=>"\xc3\x9a","\xdb"=>"\xc5\xb0","\xdc"=>"\xc3\x9c","\xdd"=>"\xc3\x9d","\xde"=>"\xc5\xa2","\xdf"=>"\xc3\x9f","\xe0"=>"\xc5\x95","\xe1"=>"\xc3\xa1","\xe2"=>"\xc3\xa2","\xe3"=>"\xc4\x83","\xe4"=>"\xc3\xa4","\xe5"=>"\xc4\xba","\xe6"=>"\xc4\x87","\xe7"=>"\xc3\xa7","\xe8"=>"\xc4\x8d","\xe9"=>"\xc3\xa9","\xea"=>"\xc4\x99","\xeb"=>"\xc3\xab","\xec"=>"\xc4\x9b","\xed"=>"\xc3\xad","\xee"=>"\xc3\xae","\xef"=>"\xc4\x8f","\xf0"=>"\xc4\x91","\xf1"=>"\xc5\x84","\xf2"=>"\xc5\x88","\xf3"=>"\xc3\xb3","\xf4"=>"\xc3\xb4","\xf5"=>"\xc5\x91","\xf6"=>"\xc3\xb6","\xf7"=>"\xc3\xb7","\xf8"=>"\xc5\x99","\xf9"=>"\xc5\xaf","\xfa"=>"\xc3\xba","\xfb"=>"\xc5\xb1","\xfc"=>"\xc3\xbc","\xfd"=>"\xc3\xbd","\xfe"=>"\xc5\xa3","\xff"=>"\xcb\x99")); 
    
        // assume ISO-8859-2 
        return strtr($s, array("\x80"=>"\xc2\x80","\x81"=>"\xc2\x81","\x82"=>"\xc2\x82","\x83"=>"\xc2\x83","\x84"=>"\xc2\x84","\x85"=>"\xc2\x85","\x86"=>"\xc2\x86","\x87"=>"\xc2\x87","\x88"=>"\xc2\x88","\x89"=>"\xc2\x89","\x8a"=>"\xc2\x8a","\x8b"=>"\xc2\x8b","\x8c"=>"\xc2\x8c","\x8d"=>"\xc2\x8d","\x8e"=>"\xc2\x8e","\x8f"=>"\xc2\x8f","\x90"=>"\xc2\x90","\x91"=>"\xc2\x91","\x92"=>"\xc2\x92","\x93"=>"\xc2\x93","\x94"=>"\xc2\x94","\x95"=>"\xc2\x95","\x96"=>"\xc2\x96","\x97"=>"\xc2\x97","\x98"=>"\xc2\x98","\x99"=>"\xc2\x99","\x9a"=>"\xc2\x9a","\x9b"=>"\xc2\x9b","\x9c"=>"\xc2\x9c","\x9d"=>"\xc2\x9d","\x9e"=>"\xc2\x9e","\x9f"=>"\xc2\x9f","\xa0"=>"\xc2\xa0","\xa1"=>"\xc4\x84","\xa2"=>"\xcb\x98","\xa3"=>"\xc5\x81","\xa4"=>"\xc2\xa4","\xa5"=>"\xc4\xbd","\xa6"=>"\xc5\x9a","\xa7"=>"\xc2\xa7","\xa8"=>"\xc2\xa8","\xa9"=>"\xc5\xa0","\xaa"=>"\xc5\x9e","\xab"=>"\xc5\xa4","\xac"=>"\xc5\xb9","\xad"=>"\xc2\xad","\xae"=>"\xc5\xbd","\xaf"=>"\xc5\xbb","\xb0"=>"\xc2\xb0","\xb1"=>"\xc4\x85","\xb2"=>"\xcb\x9b","\xb3"=>"\xc5\x82","\xb4"=>"\xc2\xb4","\xb5"=>"\xc4\xbe","\xb6"=>"\xc5\x9b","\xb7"=>"\xcb\x87","\xb8"=>"\xc2\xb8","\xb9"=>"\xc5\xa1","\xba"=>"\xc5\x9f","\xbb"=>"\xc5\xa5","\xbc"=>"\xc5\xba","\xbd"=>"\xcb\x9d","\xbe"=>"\xc5\xbe","\xbf"=>"\xc5\xbc","\xc0"=>"\xc5\x94","\xc1"=>"\xc3\x81","\xc2"=>"\xc3\x82","\xc3"=>"\xc4\x82","\xc4"=>"\xc3\x84","\xc5"=>"\xc4\xb9","\xc6"=>"\xc4\x86","\xc7"=>"\xc3\x87","\xc8"=>"\xc4\x8c","\xc9"=>"\xc3\x89","\xca"=>"\xc4\x98","\xcb"=>"\xc3\x8b","\xcc"=>"\xc4\x9a","\xcd"=>"\xc3\x8d","\xce"=>"\xc3\x8e","\xcf"=>"\xc4\x8e","\xd0"=>"\xc4\x90","\xd1"=>"\xc5\x83","\xd2"=>"\xc5\x87","\xd3"=>"\xc3\x93","\xd4"=>"\xc3\x94","\xd5"=>"\xc5\x90","\xd6"=>"\xc3\x96","\xd7"=>"\xc3\x97","\xd8"=>"\xc5\x98","\xd9"=>"\xc5\xae","\xda"=>"\xc3\x9a","\xdb"=>"\xc5\xb0","\xdc"=>"\xc3\x9c","\xdd"=>"\xc3\x9d","\xde"=>"\xc5\xa2","\xdf"=>"\xc3\x9f","\xe0"=>"\xc5\x95","\xe1"=>"\xc3\xa1","\xe2"=>"\xc3\xa2","\xe3"=>"\xc4\x83","\xe4"=>"\xc3\xa4","\xe5"=>"\xc4\xba","\xe6"=>"\xc4\x87","\xe7"=>"\xc3\xa7","\xe8"=>"\xc4\x8d","\xe9"=>"\xc3\xa9","\xea"=>"\xc4\x99","\xeb"=>"\xc3\xab","\xec"=>"\xc4\x9b","\xed"=>"\xc3\xad","\xee"=>"\xc3\xae","\xef"=>"\xc4\x8f","\xf0"=>"\xc4\x91","\xf1"=>"\xc5\x84","\xf2"=>"\xc5\x88","\xf3"=>"\xc3\xb3","\xf4"=>"\xc3\xb4","\xf5"=>"\xc5\x91","\xf6"=>"\xc3\xb6","\xf7"=>"\xc3\xb7","\xf8"=>"\xc5\x99","\xf9"=>"\xc5\xaf","\xfa"=>"\xc3\xba","\xfb"=>"\xc5\xb1","\xfc"=>"\xc3\xbc","\xfd"=>"\xc3\xbd","\xfe"=>"\xc5\xa3","\xff"=>"\xcb\x99")); 
    } 
    
    
    
    // convert to UTF-8 
    function autoUTF($s) 
    { 
        $phpversion=phpversion();
        if (($phpversion[0]=="4") || ($phpversion[0]=="3")) return autoUTF_PHP4($s); else {
    
          // detect UTF-8 
          if (preg_match('#[\x80-\x{1FF}\x{2000}-\x{3FFF}]#u', $s)) 
              return $s; 
      
          // detect WINDOWS-1250 
          if (preg_match('#[\x7F-\x9F]#', $s)) 
              return iconv('WINDOWS-1250', 'UTF-8', $s); 
      
          // assume ISO-8859-2 
          return iconv('ISO-8859-2', 'UTF-8', $s);
        } 
    } 
    
    
    
    $e = &$modx->Event;
    
    switch ($e->name) {
    	case "OnWebPagePrerender":	
    $doc = $modx->documentOutput;
    		preg_match_all("|<autoutf8>(.*)</autoutf8>|Uis",$doc,$matches);
    		for ($i=0;$i<count($matches[0]);$i++) {
    			$tag = $matches[0][$i];
    			$text = autoUTF($matches[1][$i]);
    			$doc = str_replace($tag,$text,$doc);
    		}
    $doc=preg_replace("/encoding=\"[^\"']*\"/i","encoding=\"utf-8\"",$doc);
    $doc=preg_replace("/<meta([^>]*)charset=[^\"']*([\"'])([^>]*)>/i","<meta\\1charset=utf-8\\2\\3>",$doc);
    
    		$modx->documentOutput = $doc;
    		break;
    		
    	default:	// stop here
    		return; 
    		break;	
    }
    return 1;