/
getStrings.php
76 lines (62 loc) · 2.15 KB
/
getStrings.php
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
<?php
function get_comments($xmlstr){
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->loadXML($xmlstr);
$xpath = new DOMXPath($doc);
$comments = [];
foreach ($xpath->query('//comment()') as $comment)
{
$comments[trim($comment->nextSibling->attributes->item(0)->value)] = $comment->textContent;
}
return $comments;
}
function getNeatFileName($filename){
$filename = str_replace("strings_", "", $filename);
$filename = str_replace("_", " ", $filename);
$filename = str_replace(".xml", "", $filename);
return ucfirst($filename);
}
function find_node_by_name($resources, $name){
foreach($resources as $resNode) {
if ($resNode['name'] == $name)
return $resNode;
}
}
function get_file_as_id($file){
return str_replace(".xml", "", $file);
}
$resources_dir = 'xml/';
$default_lang = $resources_dir.'values/';
$default_lang_string_files = scandir($default_lang);
$default_lang_string_files = array_splice($default_lang_string_files, 2); // remove . and ..
$translations_directorys = scandir($resources_dir);
$translations_directorys = array_splice($translations_directorys, 3); // remove . and .. and values/
$comments = [];
foreach($default_lang_string_files as $file) {
$resources[$file]['default'] =
new SimpleXMLElement(
file_get_contents($default_lang.$file)
);
$comments = array_merge($comments, get_comments(file_get_contents($default_lang.$file)));
// Delete untranslatable strings
error_reporting(E_ERROR | E_PARSE);
foreach ($resources[$file]['default']->children() as $string) {
if ($string['translatable'] == "false")
unset($string[0][0]);
}
error_reporting(E_ALL);
}
foreach($translations_directorys as $directory) {
foreach($resources as $file => $resource){
$file_address = $resources_dir.$directory."/".$file;
if (file_exists($file_address)){
$lang = str_replace("values-", "", $directory);
$resources[$file][$lang] =
simplexml_load_string(
file_get_contents($file_address)
);
}
}
}
?>