summaryrefslogtreecommitdiff
path: root/library/WT/Debug.php
blob: d80c29d85b6538ec4b856bd614149316053fb129 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
// Debug functions.
//
// Copyright (C) 2014 Greg Roach
//
// 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 2 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, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

if (!defined('WT_WEBTREES')) {
	header('HTTP/1.0 403 Forbidden');
	exit;
}

class WT_Debug {
	private static $CHAR_DECORATION=array(
		// Add diacritics to letters to test UTF8 encoding issues
		'a'=>'ă', 'b'=>'ḅ', 'c'=>'ç', 'd'=>'đ', 'e'=>'ě', 'f'=>'ḟ', 'g'=>'ğ',
		'h'=>'ħ', 'i'=>'ĩ', 'j'=>'ĵ', 'k'=>'ḱ', 'l'=>'ł', 'm'=>'ḿ', 'n'=>'ñ',
		'o'=>'ø', 'p'=>'ṗ', 'q'=>'ǫ', 'r'=>'ṙ', 's'=>'š', 't'=>'ť', 'u'=>'û',
		'v'=>'ṽ', 'w'=>'ẃ', 'x'=>'ẍ', 'y'=>'ÿ', 'z'=>'ẕ',

		'A'=>'Ă', 'B'=>'Ḅ', 'C'=>'Ç', 'D'=>'Đ', 'E'=>'Ě', 'F'=>'Ḟ', 'G'=>'Ğ',
		'H'=>'Ħ', 'I'=>'Ĩ', 'J'=>'ĵ', 'K'=>'Ḱ', 'L'=>'Ł', 'M'=>'Ḿ', 'N'=>'Ñ',
		'O'=>'Ø', 'P'=>'ṗ', 'Q'=>'ǫ', 'R'=>'ṙ', 'S'=>'š', 'T'=>'ť', 'U'=>'Û',
		'V'=>'Ṽ', 'W'=>'Ẃ', 'X'=>'Ẍ', 'Y'=>'Ý', 'Z'=>'Ẕ',
		// Modify puncutation, so we can easily identify untranslated punctuation
		'['=>'⟨', ']'=>'⟩', '('=>'⟨', ')'=>'⟩', '{'=>'⟨', '}'=>'⟩',
		'.'=>'·', ':'=>'˸'
	);

	// Pseudo-tranlsate a string.  Give it the attributes of a foreign language,
	// while leaving it understandable by an English speaking developer.
	public static function pseudoTranslate($text) {
		global $TEXT_DIRECTION;

		// There are certain texts that we should never translate!
		switch ($text) {
		case 'utf8_unicode_ci':
			return $text;
		default:
			break;
		}

		// Process the each UTF8 character separately.
		// Take care not to transform HTML tags
		$tmp='';
		$in_html=false;
		$in_format=false;
		while ($text) {
			// Read the next multi-byte character
			$next_char='';
			while (ord(substr($text, 0, 1)) & 0xF0 == 0xC0) {
				$next_char.=substr($text, 0, 1);
			}
			$next_char.=substr($text, 0, 1);
			$text=substr($text, 1);
			switch ($next_char) {
			case '<':
				$in_html=true;
				$tmp.=$next_char;
				break;
			case '>':
				$in_html=false;
				$tmp.=$next_char;
				break;
			case '%':
				$in_format=true;
				$tmp.=$next_char;
				break;
			default:
				if ($in_html || $in_format || !array_key_exists($next_char, self::$CHAR_DECORATION)) {
					$tmp.=$next_char;
					if ($in_format && $next_char >='a' && $next_char<='z') {
						$in_format=false;
					}
				} else {
					if ($TEXT_DIRECTION=='ltr') {
						// Many languages have longer words than English - add some padding
						$tmp.='&thinsp;'.self::$CHAR_DECORATION[$next_char].'&thinsp;';
					} else {
						// RTL words tend to be short, so no need to add padding
						$tmp.=WT_UTF8_RLO.$next_char.WT_UTF8_PDF;
					}
				}
				break;
			}
		}
		// Highlight the boundaries between translated strings
		return '|'.$tmp.'|';
	}
}