~fkooman/vpn-user-portal

ref: 57df62b9a04e9b2391ca809e5ff9d76e524eceb1 vpn-user-portal/dev/update_translations.php -rw-r--r-- 2.3 KiB
57df62b9François Kooman update CHANGES 1 year, 5 months ago
                                                                                
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
<?php

/*
 * eduVPN - End-user friendly VPN.
 *
 * Copyright: 2016-2019, The Commons Conservancy eduVPN Programme
 * SPDX-License-Identifier: AGPL-3.0+
 */

$localeFileList = glob(dirname(__DIR__).'/locale/*.php');
$tplFileList = glob(dirname(__DIR__).'/views/*.php');

if ($argc > 1) {
    $localeFileList = [dirname(__DIR__).'/locale/'.$argv[1].'.php'];
}

// extract all translatable strings from the PHP templates and put them in an
// array
$sourceStr = [];
foreach ($tplFileList as $tplFile) {
    $phpFile = file_get_contents($tplFile);
    // find all translatable strings in the template...
    preg_match_all("/this->t\('(.*?)'\)/", $phpFile, $matches);
    foreach ($matches[1] as $m) {
        if (!in_array($m, $sourceStr, true)) {
            $sourceStr[] = stripslashes($m);
        }
    }
}

foreach ($localeFileList as $localeFile) {
    $localeData = include $localeFile;

    // check which keys are missing from translation file
    foreach ($sourceStr as $k) {
        if (!array_key_exists($k, $localeData)) {
            // adding them to translation file
            $localeData[$k] = '';
        }
    }

    $deletedList = [];
    // check which translations are there, but are no longer needed...
    foreach ($localeData as $k => $v) {
        if (!in_array($k, $sourceStr, true)) {
            // remove them from the translation file, add them to the
            // "deleted" array
            unset($localeData[$k]);
            $deletedList[$k] = $v;
        }
    }

    // sort the translations
    ksort($localeData);
    ksort($deletedList);

    // create the locale file
    $output = '<?php'.PHP_EOL.PHP_EOL.'return ['.PHP_EOL;
    foreach ($localeData as $k => $v) {
        $k = quoteStr($k);
        $v = quoteStr($v);
        if (empty($v)) {
            $output .= sprintf("    //'%s' => '%s',", $k, $v).PHP_EOL;
        } else {
            $output .= sprintf("    '%s' => '%s',", $k, $v).PHP_EOL;
        }
    }
    // add the deleted entries as comments
    foreach ($deletedList as $k => $v) {
        $k = quoteStr($k);
        $v = quoteStr($v);
        $output .= sprintf("    // [DELETED] '%s' => '%s',", $k, $v).PHP_EOL;
    }
    $output .= '];';

    // write locale file
    file_put_contents($localeFile, $output);
}

function quoteStr($str)
{
    return str_replace("'", "\'", $str);
}