aboutsummaryrefslogtreecommitdiffstats
path: root/langs/lib.php
blob: 81e231673c136a2e58f3a319401504534916c7db (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
<?php
/**
*/

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)
 *
 * @param string $a file name
 * @param string $b file name
 *
 * @return array
 *
 * @todo some strings may be left untranslated on purpose
*/
function _lang_diff($a, $b)
{
    $fa = _lang_return($a);
    $fb = _lang_return($b);

    $ret = array(
        'aCount' => count($fa),
        'bCount' => count($fb),
        'diff'   => count($fa) - count($fb),
    );
    $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' => count($fa),
        'b' => count($fb),
        'missing' => $missing,
        'notrans' => $notrans,
        'extra'   => $extra
    );
}


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

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

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

        return $files;
    }
}

function _lang_file_switch($s, $l)
{
    return $l . substr(str_replace('.en.lang', '.' . $l . '.lang', $s), 2);
}

function get_lang_references()
{
    return glob_recursive('en/*', 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;
}