1 |
<?php |
2 |
/** |
3 |
*/ |
4 |
|
5 |
if (isset($_SERVER['APP_MODE']) && $_SERVER['APP_MODE'] !== 'prod') { |
6 |
ini_set('error_reporting', E_ALL); |
7 |
ini_set('show_errors', true); |
8 |
ini_set('display_errors', true); |
9 |
} else { |
10 |
ini_set('error_reporting', FALSE); |
11 |
ini_set('show_errors', FALSE); |
12 |
ini_set('display_errors', FALSE); |
13 |
ini_set('log_errors', FALSE); |
14 |
} |
15 |
|
16 |
include '../langs.inc.php'; |
17 |
|
18 |
/** |
19 |
* Diff two .lang files, to get: |
20 |
* - strings count of each |
21 |
* - missing strings (in $a, not in $b) |
22 |
* - extra strings (in $b, not in $a) |
23 |
* - untranslated strings (same in $a and $b, or empty in $b) |
24 |
* |
25 |
* @param string $a file name |
26 |
* @param string $b file name |
27 |
* |
28 |
* @return array |
29 |
* |
30 |
* @todo some strings may be left untranslated on purpose |
31 |
*/ |
32 |
function _lang_diff($a, $b) |
33 |
{ |
34 |
$fa = i18n::_lang_return($a); |
35 |
$fb = i18n::_lang_return($b, true); // option to return duplicates |
36 |
$duplicates = (isset($fb['duplicates']) ? array_pop($fb) : null); |
37 |
|
38 |
/* $ret = array( |
39 |
'aCount' => count($fa), |
40 |
'bCount' => count($fb), |
41 |
'diff' => count($fa) - count($fb), |
42 |
); unused var */ |
43 |
$missing = array(); |
44 |
$notrans = array(); |
45 |
|
46 |
$ka = array_keys($fa); |
47 |
$kb = array_keys($fb); |
48 |
|
49 |
$missing = array_diff($ka, $kb); |
50 |
$extra = array_diff($kb, $ka); |
51 |
|
52 |
// search for untranslated strings |
53 |
foreach ($fa as $k => $v) { |
54 |
if (array_key_exists($k, $fb)) { |
55 |
if ($v == $fb[$k] || '' == $fb[$k]) { |
56 |
$notrans[] = $k; |
57 |
} |
58 |
} |
59 |
} |
60 |
|
61 |
return array( |
62 |
'a_name' => $a, |
63 |
'b_name' => $b, |
64 |
'a' => count($fa), |
65 |
'b' => count($fb), |
66 |
'missing' => $missing, |
67 |
'notrans' => $notrans, |
68 |
'extra' => $extra, |
69 |
'dup_str' => $duplicates, |
70 |
); |
71 |
} |
72 |
|
73 |
function _lang_diff_stats($a, $b) |
74 |
{ |
75 |
$diff = _lang_diff($a, $b); |
76 |
|
77 |
$diff['missing'] = count($diff['missing']); |
78 |
$diff['notrans'] = count($diff['notrans']); |
79 |
$diff['extra'] = count($diff['extra']); |
80 |
$diff['ok'] = (($diff['b'] - $diff['a']) == 0) ? true : false; |
81 |
$diff['correct'] = $diff['b'] - $diff['notrans'] - $diff['missing']; |
82 |
|
83 |
return $diff; |
84 |
} |
85 |
|
86 |
if ( ! function_exists('glob_recursive')) |
87 |
{ |
88 |
// Does not support flag GLOB_BRACE |
89 |
|
90 |
function glob_recursive($pattern, $flags = 0) |
91 |
{ |
92 |
$files = glob($pattern, $flags); |
93 |
|
94 |
// removing dirs from $files as they are not files ;) |
95 |
$files_wo_dirs = array(); |
96 |
foreach ($files as $single_file) { |
97 |
$single_file_as_string = str_split($single_file); |
98 |
$last_sign = array_pop($single_file_as_string); |
99 |
if($last_sign != '/') { |
100 |
$files_wo_dirs[] = $single_file; |
101 |
}; |
102 |
} |
103 |
$files = $files_wo_dirs; |
104 |
|
105 |
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) |
106 |
{ |
107 |
$files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags)); |
108 |
} |
109 |
|
110 |
return $files; |
111 |
} |
112 |
} |
113 |
|
114 |
function _lang_file_switch($s, $l) |
115 |
{ |
116 |
$s = str_replace('en.lang', $l . '.lang', $s); |
117 |
return str_replace('en/', $l . '/', $s); |
118 |
} |
119 |
|
120 |
function get_lang_references() |
121 |
{ |
122 |
return glob_recursive('en/*', GLOB_MARK); |
123 |
} |
124 |
|
125 |
function get_other_langs() |
126 |
{ |
127 |
$ls = glob('*'); |
128 |
$re = array(); |
129 |
foreach ($ls as $l) { |
130 |
if (!is_dir($l)) continue; |
131 |
if ($l == 'en') continue; |
132 |
$re[] = $l; |
133 |
} |
134 |
array_unshift($re, 'en'); |
135 |
return $re; |
136 |
} |
137 |
|
138 |
function aproximate_number_of_untranslated_constitution_lines($app_root, $lang, $unique_lines_in_eng_constitution = array()) |
139 |
{ |
140 |
$constitution_readable = FALSE; |
141 |
$dest_constitution = sprintf('%s/%s/%s/%s_%s.md', $app_root, $lang, 'about/constitution', 'mageia.org_statutes', $lang); |
142 |
$number_of_unique_lines_in_eng_constitution = count($unique_lines_in_eng_constitution); |
143 |
$aproximate_number_of_untranslated_lines = 0; |
144 |
if(is_readable($dest_constitution)) { |
145 |
$unique_lines_in_constitution = array_unique(file($dest_constitution)); |
146 |
$number_of_unique_lines_in_constitution = count($unique_lines_in_constitution); |
147 |
$constitution_readable = TRUE; |
148 |
if ($lang == 'en') { |
149 |
$aproximate_number_of_untranslated_lines = $number_of_unique_lines_in_constitution; |
150 |
$untranslated_lines_in_constitution = array(); |
151 |
} else { |
152 |
$untranslated_lines_in_constitution = array_intersect($unique_lines_in_eng_constitution, $unique_lines_in_constitution); |
153 |
$number_of_nonunique_lines_lang_constitution = count($untranslated_lines_in_constitution); |
154 |
$ratio = $number_of_nonunique_lines_lang_constitution / $number_of_unique_lines_in_eng_constitution; |
155 |
$limit_ratio = 0.15; // limit ratio of "allowed" untranslated lines |
156 |
if ($ratio > $limit_ratio) { |
157 |
// add aproximate number of untranslated constitution lines |
158 |
$aproximate_number_of_untranslated_lines = $number_of_nonunique_lines_lang_constitution - round($limit_ratio * $number_of_unique_lines_in_eng_constitution); |
159 |
} |
160 |
} |
161 |
} else { |
162 |
$unique_lines_in_constitution = $unique_lines_in_eng_constitution; |
163 |
$aproximate_number_of_untranslated_lines = $number_of_unique_lines_in_eng_constitution; |
164 |
$untranslated_lines_in_constitution = $unique_lines_in_eng_constitution; |
165 |
} |
166 |
|
167 |
return array( |
168 |
'unique_lines_in_constitution' => $unique_lines_in_constitution, |
169 |
'constitution_readable' => $constitution_readable, |
170 |
'untranslated_lines_in_constitution' => $untranslated_lines_in_constitution, |
171 |
'aproximate_number_of_untranslated_lines' => $aproximate_number_of_untranslated_lines, |
172 |
); |
173 |
} |