-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxl.copy.source
More file actions
106 lines (104 loc) · 3.95 KB
/
xl.copy.source
File metadata and controls
106 lines (104 loc) · 3.95 KB
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
#!/usr/bin/php -q
<?PHP
/*
xl.copy.source - copy new source entries to foreign language files
Copyright @2024, Bergware International.
*/
$select = isset($argv[1]) ? 'lang-'.$argv[1] : '*';
$rootdir = "/boot/unraid/languages";
$native = glob("$rootdir/lang-en_US/*.txt",GLOB_NOSORT);
$foreign = array_filter(glob("$rootdir/$select",GLOB_ONLYDIR|GLOB_NOSORT),'foreign');
function foreign($lang) {
global $argv;
return empty($argv[1]) ? strpos($lang,'en_US')===false : strpos($lang,$argv[1])!==false;
}
function escapeQuotes($text) {
return str_replace(["\"\n",'"'],["\" \n",'\"'],$text);
}
function tag($key) {
return preg_replace('/^(null|yes|no|true|false|on|off|none)$/i','$1.',$key);
}
function remap($key) {
return preg_replace(['/\&|[\?\{\}\|\&\~\!\[\]\(\)\/\\:\*^\.\"\']|<.+?\/?>/','/^(null|yes|no|true|false|on|off|none)$/i','/ +/'],['','$1.',' '],$key);
}
function parse_lang_file($file) {
return parse_ini_string(preg_replace(['/^(null|yes|no|true|false|on|off|none)=/mi','/^([^>].*?)=(.*)$/m','/^:(.+_(help|plug)):$/m','/^:end$/m'],['$1.=','$1="$2"','_$1="','"'],escapeQuotes(file_get_contents($file))));
}
// loop thru plugin language files
foreach ($native as $file) {
$name = basename($file);
if ($name=='helptext.txt') continue;
$source = file($file,FILE_IGNORE_NEW_LINES);
// compare with foreign language files
foreach ($foreign as $language) {
$output = "$language/$name";
// skip non-existing translations
if (!is_file($output)) continue;
// find current missing translations
$note = exec("grep -m1 '^; Note:' ".escapeshellarg($output));
$old = $note ? explode(' ',explode(' - ',$note)[1])[0] : 0;
echo "Parsing: $output",str_repeat(' ',120-strlen($output)),"\r";
// skip when parse error
if (!($target = parse_lang_file($output))) continue;
$section = 0;
$copy = $missing = $shadow = [];
foreach ($source as $row) {
if (empty($row) || $row[0]==';') {
// empty row or comment
if ($section==0) $copy[] = $shadow[] = $row;
} elseif ($row[0]==':' && substr($row,-5)=='plug:') {
// start of plug section
$copy[] = $shadow[] = $row;
$section = 1;
$key = '_'.substr($row,1,-1);
if (!empty($target[$key])) {
// use translated plug section
$copy[] = $shadow[] = preg_replace('/^\n+|\n+$/','',$target[$key]);
$section = 2;
}
} elseif ($row==':end') {
// end of plug section
$copy[] = $shadow[] = $row;
$section = 0;
} elseif ($section==1) {
// inside plug section
$copy[] = $shadow[] = $row;
} elseif ($section==0) {
// translation line
if (strpos($row,'=')===false) echo "\nError: '$row' is missing an equal sign\n";
[$key,$text] = array_pad(explode('=',$row,2),2,'');
if (preg_match('/\&|[\?\{\}\|\&\~\!\[\]\(\)\\*^\"]/',$key)) echo "\nError: '$key' is invalid\n";
// get translation text
$line = trim($target[tag($key)]??'');
$shadow[] = trim($key)."=".trim($line ?: $text);
if (empty($line) || ($text && tag($key)==remap($line) && strlen($target[tag($key)])!=strlen(remap($line)) && strlen($key)>14)) {
// new or not translated key
$missing[] = trim($key)."=".trim($text);
} else {
// translated key
$copy[] = trim($key)."=".trim($line ?: $text);
}
}
}
$tail = "\n";
$new = count($missing);
if ($new) {
if ($new != $old) {
echo "\nUpdating: $output ($new)\n";
$date = date('F j, Y');
$note = "; Note: $date - $new missing translations";
}
if ($new < 60) {
// show missing keys at the end
$tail .= "\n$note\n".implode("\n",$missing)."\n";
} else {
// show missing keys at their original location
$tail .= "\n$note\n";
$copy = $shadow;
}
}
file_put_contents($output,implode("\n",$copy).$tail);
}
}
echo str_repeat(' ',120)."\r";
?>