aboutsummaryrefslogtreecommitdiffstats
path: root/langs/lib.php
blob: 054a4743e6cad77fe39bbd0ea66042fc5b17cac0 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
/**
*/

if (isset($_SERVER['APP_MODE']) && $_SERVER['APP_MODE'] !== 'prod') {
    ini_set('error_reporting', E_ALL);
    ini_set('show_errors', true);
    ini_set('display_errors', true);
} else {
    ini_set('error_reporting', FALSE);
    ini_set('show_errors', FALSE);
    ini_set('display_errors', FALSE);
    ini_set('log_errors', FALSE);
}

include '../langs.inc.php';

/**
 * Diff two .lang files, to get:
 * - strings count of each
 * - missing strings (in $a, not in $b)
 * - extra strings (in $b, not in $a)
 * - untranslated strings (same in $a and $b, or empty in $b)
 * - duplicate strings in $b
 *
 * @param string $a file name
 * @param string $b file name
 *
 * @return array
*/
function _lang_diff($a, $b)
{
    $fa = i18n::_lang_return($a);
    $fb = i18n::_lang_return($b, true); // option to return duplicates
    $duplicates = (isset($fb['duplicates']) ? array_pop($fb) : null);

/*    $ret = array(
        'aCount' => count($fa),
        'bCount' => count($fb),
        'diff'   => count($fa) - count($fb),
    ); unused var */
    $missing = array();
    $notrans = array();

    $ka = array_keys($fa);
    $kb = array_keys($fb);

    $missing = array_diff($ka, $kb);
    $extra   = array_diff($kb, $ka);

    // search for untranslated strings
    foreach ($fa as $k => $v) {
        if (array_key_exists($k, $fb)) {
            if ($v == $fb[$k] || '' == $fb[$k]) {
                $notrans[] = $k;
            }
        }
    }

    return array(
        'a_name'  => $a,
        'b_name'  => $b,
        'a'       => count($fa),
        'b'       => count($fb),
        'missing' => $missing,
        'notrans' => $notrans,
        'extra'   => $extra,
        'dup_str' => $duplicates,
    );
}

/**
 * Diff pot and po files, to get:
 * - source (pot) strings count
 * - missing strings in target
 * - untranslated strings in target
 * - empty array for extra and duplicate strings for backward compatibility
 *
 * @param string $locale locale name ('sl')
 * @param string $resource file name ('about/license')
 *
 * @return array
*/
function _po_diff($locale, $resource)
{
    $source_l = read_translation_file('en', $resource);
    $target_l = read_translation_file($locale, $resource);

    $pot_strings = array();
    $untrans = array();
    $fuzzy_or_missing = array();

    foreach ($source_l as $escaped_string => $subarray) {
        if (!empty($subarray["msgid"])) { // filter out header
            $pot_strings[$escaped_string] = $subarray["msgid"];
        }
    }

    foreach ($target_l as $escaped_string => $subarray) {
        if (!empty($subarray["msgid"])) { // filter out header
            $po_strings[$escaped_string] = $subarray["msgstr"][0];
        }
    }

    foreach ($pot_strings as $escaped_string => $translated_string) {
        if (isset($po_strings[$escaped_string])) {
            if (empty($po_strings[$escaped_string])) {
                $untrans[] = $escaped_string;
            }
        } else {
            $fuzzy_or_missing[] = $escaped_string;
        }
    }

    return array(
        'a'       => count($pot_strings), // # of original strings
//        'b'       => count($po_strings), // # of target strings
        'fuzzy_or_missing' => $fuzzy_or_missing,
        'notrans' => $untrans,
        'extra'   => array(),
        'dup_str' => array(),
    );
}

/*function _lang_diff_stats($a, $b)
{
    $diff = _lang_diff($a, $b);

    $diff['missing'] = count($diff['missing']);
    $diff['notrans'] = count($diff['notrans']);
    $diff['extra']   = count($diff['extra']);
    $diff['ok']      = (($diff['b'] - $diff['a']) == 0) ? true : false;
    $diff['correct'] = $diff['b'] - $diff['notrans'] - $diff['missing'];

    return $diff;
} /**/

if ( ! function_exists('glob_recursive'))
{
    // Does not support flag GLOB_BRACE

    function glob_recursive($pattern, $flags = 0)
    {
        $files = glob($pattern, $flags);

//      removing dirs from $files as they are not files ;)
        $files_wo_dirs = array();
        foreach ($files as $single_file) {
            $single_file_as_string = str_split($single_file);
            $last_sign = array_pop($single_file_as_string);
            if($last_sign != '/') {
                $files_wo_dirs[] = $single_file;
            };
        }
        $files = $files_wo_dirs;

        foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir)
        {
			$files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
        }

        return $files;
    }
}

/**
 * Create 'sl/about/license.sl.lang'
 * from 'en/about/license.en.lang'
 * or ../_nav/langs/sl.lang
 * from ../_nav/langs/en.lang
 *
 * @param string $s file name with path
 * @param string $l locale name
 *
 * @return string
*/
function _lang_file_switch($s, $l)
{
    $s = str_replace('en.lang', $l . '.lang', $s);
    return str_replace('en/', $l . '/', $s);
}

/**
 * Create 'sl/about/license.po'
 * from 'en/about/license.pot'
 * or ../_nav/langs/sl.po
 * from ../_nav/langs/en.pot
 *
 * @param string $s file name with path
 * @param string $l locale name
 *
 * @return string
*/
function _po_file_switch($s, $l)
{
    if($l != 'en') {
        $s = str_replace('.pot', '.po', $s);
    }
    $s = str_replace('/en.', '/' . $l . '.', $s);
    return str_replace('en/', $l . '/', $s);
}

/**
 * Create 'about/license'
 * from 'en/about/license.pot' or 'en/about/license.en.lang'
 *
 * @param string $source_file file name with path
 * @param string $extension file extension to remove
 *
 * @return string
*/
function _extract_resource($source_file, $extension = '.pot')
{
    $resource = str_replace($extension, '', $source_file);
    return str_replace('en/', '', $resource);
}

function get_lang_references($pattern = '*')
{
    return glob_recursive('en/' . $pattern, GLOB_MARK);
}

function get_other_langs()
{
    $ls = glob('*');
    $re = array();
    foreach ($ls as $l) {
        if (!is_dir($l)) continue;
        if ($l == 'en') continue;
        $re[] = $l;
    }
    array_unshift($re, 'en');
    return $re;
}

function aproximate_number_of_untranslated_constitution_lines($app_root, $lang, $unique_lines_in_eng_constitution = array())
{
    $constitution_readable = FALSE;
    $dest_constitution = sprintf('%s/%s/%s/%s_%s.md', $app_root, $lang, 'about/constitution', 'mageia.org_statutes', $lang);
    $number_of_unique_lines_in_eng_constitution = count($unique_lines_in_eng_constitution);
    $aproximate_number_of_untranslated_lines = 0;
    if(is_readable($dest_constitution)) {
        $unique_lines_in_constitution = array_unique(file($dest_constitution));
        $number_of_unique_lines_in_constitution = count($unique_lines_in_constitution);
        $constitution_readable = TRUE;
        if ($lang == 'en') {
            $aproximate_number_of_untranslated_lines = $number_of_unique_lines_in_constitution;
            $untranslated_lines_in_constitution      = array();
        } else {
            $untranslated_lines_in_constitution = array_intersect($unique_lines_in_eng_constitution, $unique_lines_in_constitution);
            $number_of_nonunique_lines_lang_constitution = count($untranslated_lines_in_constitution);
            $ratio = $number_of_nonunique_lines_lang_constitution / $number_of_unique_lines_in_eng_constitution;
            $limit_ratio = 0.15; // limit ratio of "allowed" untranslated lines
            if ($ratio > $limit_ratio) {
                // add aproximate number of untranslated constitution lines
                $aproximate_number_of_untranslated_lines = $number_of_nonunique_lines_lang_constitution - round($limit_ratio * $number_of_unique_lines_in_eng_constitution);
            }
        }
    } else {
        $unique_lines_in_constitution             = $unique_lines_in_eng_constitution;
        $aproximate_number_of_untranslated_lines  = $number_of_unique_lines_in_eng_constitution;
        $untranslated_lines_in_constitution       = $unique_lines_in_eng_constitution;
    }

    return array(
        'unique_lines_in_constitution'            => $unique_lines_in_constitution,
        'constitution_readable'                   => $constitution_readable,
        'untranslated_lines_in_constitution'      => $untranslated_lines_in_constitution,
        'aproximate_number_of_untranslated_lines' => $aproximate_number_of_untranslated_lines,
    );
}