summaryrefslogtreecommitdiff
path: root/scripts/langdiff.php
blob: f004b5431704042957146748e69a8df138aa514f (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php

if(!defined('IN_FS')) {
	die('Do not access this file directly.');
}

# let also project managers allow translation of flyspray
if(!$user->perms('manage_project')) {
	Flyspray::show_error(28);
}

ob_start();

?>
<style type="text/css">
pre { margin : 0; }
table{border-collapse:collapse;}
.progress_bar_container{height:20px;}
.progress_bar_container span:first-child{display:inline-block;margin-top:2px;z-index:101;color:#000;}
.overview{margin-left:auto;margin-right:auto;}
.overview td, .overview th{border:none;padding:0;}
a.button{padding:2px 10px 2px 10px;margin:2px;}
table th{text-align:center;}
table th, table td {
	vertical-align:middle;
	border: 1px solid #ccc;
	padding: 2px;
}
tr:hover td, tr:hover th { background : #e0e0e0; }
</style>
<?php
require_once dirname(dirname(__FILE__)) . '/includes/fix.inc.php';
/*
* Usage: Open this file like ?do=langdiff?lang=de in your browser.
*    "de" represents your language code.
*/
$lang = isset($_GET['lang']) ? $_GET['lang'] : 'en';
if( preg_match('/[^a-zA-Z_]/', $lang)) {
	die('Invalid language name.');
}

# reload en.php if flyspray did it before!
require('lang/en.php');
// while the en.php and $lang.php both defines $language, the english one should be keept
$orig_language = $language;

$translationfile = 'lang/'."$lang.php";
if ($lang != 'en' && file_exists($translationfile)) {
	# reload that file if flyspray did it before!
        include($translationfile);
	if( isset($_GET['sort']) && $_GET['sort']=='key'){
		ksort($orig_language);
	}elseif( isset($_GET['sort']) && $_GET['sort']=='en'){
		asort($orig_language);
	}elseif( isset($_GET['sort']) && $_GET['sort']==$_GET['lang']){
		# todo
	}else{
		# show as it is in file en.php
	}

        echo '<h1>Diff report for language ',$lang,'</h1>',"\n";
        echo '<h2>The following translation keys are missing in the translation:</h2>';
	echo '<table>';
	$i = 0;
        foreach ($orig_language as $key => $val) {
            if (!isset($translation[$key])) {
                echo '<tr><th>',$key,'</th><td>'.htmlspecialchars($val).'</td></tr>',"\n";
		$i++;
            }

        }
	echo '</table>';
	if ( $i > 0 ){
		echo '<p>',$i,' out of ',sizeof($language),' keys to translate.</p>';
	}
	echo '<h2>The following translation keys should be deleted from the translation:</h2>';
	echo '<table cellspacing="0">';
	$i = 0;
	foreach ($translation as $key => $val) {
		if ( !isset($orig_language[$key])) {
			  echo '<tr class="line',($i%2),'"><th>',$key,'</th><td><pre>\'',$val,'\'</pre></td></tr>',"\n";
			  $i++;
		}
	}
	echo '</table>';
	if ( $i > 0 ){
		echo '<p>'.$i.' entries can be removed from this translation.</p>';
	} else{
		echo '<p><i class="fa fa-check fa-2x"></i> None</p>';
	}
	echo '<h2><a name="compare"></a>Direct comparision between english and '.htmlspecialchars($lang).'</h2>';
	echo '<table>
		<colgroup></colgroup>
		<thead><tr>
		<th><a href="?do=langdiff&lang='.htmlspecialchars($lang).'&amp;sort=key#compare" title="sort by translation key">translation key</th>
		<th><a href="?do=langdiff&lang='.htmlspecialchars($lang).'&amp;sort=en#compare" title="sort by english">en</a></th>
		<th>'.htmlspecialchars($lang).'</th>
		</tr>
		</thead>
		<tbody>';
	$i = 0;
        foreach ($orig_language as $key => $val) {
            if (!isset($translation[$key])) {
              echo '<tr><th>',$key,'</th><td>'.htmlspecialchars($val).'</td><td></td></tr>'."\n";
            }else{
              echo '
	<tr>
	<th>',$key,'</th><td>'.htmlspecialchars($val).'</td>
	<td>'.htmlspecialchars($translation[$key]).'</td>
	</tr>'."\n";
            }
            $i++;
        }
	echo '</tbody></table>';
} else {
	# TODO show all existing translations overview and selection
	# readdir
	$english=$language;
	$max=count($english);
	$langfiles=array();
	$workfiles=array();
	if ($handle = opendir('lang')) {
		$languages=array();
		while (false !== ($file = readdir($handle))) {
			if ($file != "." 
			 && $file != ".." 
			 && $file!='.langdiff.php' 
			 && $file!='.langedit.php' 
			 && !(substr($file,-4)=='.bak') 
			 && !(substr($file,-5)=='.safe') ) {
				# if a .$lang.php.work file but no $lang.php exists yet
				if( substr($file,-5)=='.work'){ 
					if(!is_file('lang/'.substr($file,1,-5)) ){
						$workfiles[]=$file;
					}
				} else{ 
					$langfiles[]=$file;
				}
			}
		}
		asort($langfiles);
		asort($workfiles);
		echo '<table class="overview">
		<thead><tr><th>'.L('file').'</th><th>'.L('progress').'</th><th> </th></tr></thead>
		<tbody>';
		foreach($langfiles as $lang){
			unset($translation);
			require('lang/'.$lang); # file $language variable
			$i=0; $empty=0;
			foreach ($orig_language as $key => $val) {
				if (!isset($translation[$key])) {
					$i++;
				}else{
					if($val==''){
						$empty++;
					}
				}
			}
			$progress=floor(($max-$i)*100/$max*10)/10;
			if($lang!='en.php'){
				echo '
<tr>
<td><a href="?do=langdiff&lang='.substr($lang,0,-4).'">'.$lang.'</a></td>
<td><a href="?do=langdiff&lang='.substr($lang,0,-4).'" class="progress_bar_container">
<span class="progress">'.$progress.' %</span>
<span style="width:'.$progress.'%" class="progress_bar"></span></a>
</td>
<td><a class="button" href="?do=langedit&lang='.substr($lang,0,-4).'">'.L('translate').' '.substr($lang,0,-4).'</a></td>
</tr>';
			}else{
				echo '<tr><td>en.php</td><td>is reference and fallback</td><td><a class="button" href="?do=langedit&lang='.substr($lang,0,-4).'">Translate '.substr($lang,0,-4).'</a></td></tr>';
			}
		}
		foreach($workfiles as $workfile){
			echo '<tr>
			<td><a href="?do=langdiff&lang='.substr($workfile,1,-9).'">'.$workfile.'</a></td>
			<td></td>
			<td><a class="button" href="?do=langedit&lang='.substr($workfile,1,-9).'">'.L('translate').' '.substr($workfile,1,-9).'</a></td>
			</tr>';
		}
		closedir($handle);
		echo '</tbody></table>';
	}
}

$content = ob_get_contents();
ob_end_clean();

$page->uses('content');
$page->pushTpl('admin.translation.tpl');

?>