-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcheck_lang.php
executable file
·142 lines (132 loc) · 3.1 KB
/
check_lang.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
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
<?php
/*
This script does the following:
- Checks for missing strings
- Checks for duplicate strings
- Orders strings according to the English template.
- Fills in missing strings with English strings.
*/
# -- GLOBAL VARIABLES --
$lang_files = array();
$english_strings = array();
# - ---
# read in all language files
function grab_lang_files() {
global $lang_files;
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if (strpos($file,'.txt')>0) {
$lang_files[] = $file;
}
}
closedir($handle);
}
}
# - ---
function found( $p_haystack, $p_needle ) {
if ( strpos( $p_haystack, $p_needle ) > 0 ) {
return true;
} else {
return false;
}
}
# - ---
# parse the string and grab the variable
function get_key( $p_string ) {
$p_string = trim( $p_string );
if ( '$' != $p_string[0] ) {
return '';
}
$p_string = str_replace( ' ', '', $p_string );
$p_array = explode('=', $p_string);
return trim($p_array[0]);
}
# - ---
function check_missing( $p_file ) {
$strings = file( $p_file );
$lang_strings = array();
foreach( $strings as $string ) {
$string = trim( $string );
$key = get_key( $string );
if ( strlen( $key ) > 0 ) {
$lang_strings[$key] = $string;
}
}
$fp = fopen( $p_file, 'wb' );
# print out header
foreach( $strings as $string ) {
$p_string = trim( $string );
if ( '?>' === $p_string ) {
fwrite( $fp, rtrim($string)."\n" );
break;
}
fwrite( $fp, rtrim($string)."\n" );
}
# grab english strings
$english_strings = file( 'strings_english.txt' );
$skip = 0;
foreach ( $english_strings as $english_string ) {
$english_string = trim( $english_string );
$p_english_string = trim( $english_string );
# ignore header section
if ( 0 == $skip ) {
if ( '?>' == $p_english_string ) {
$skip = 1;
}
} else {
# grab the key
# if found then use the lang string
$key = get_key( $english_string );
if ( array_key_exists( $key, $lang_strings ) ) {
fwrite( $fp, $lang_strings[$key]."\n" );
# else use the english string
} else {
fwrite( $fp, $english_string."\n" );
}
}
}
fclose( $fp );
}
# - ---
function check_duplicates( $p_file ) {
$strings = file( $p_file );
$lang_strings = array();
$counter = 0;
foreach( $strings as $string ) {
$string = trim( $string );
$key = get_key( $string );
$counter++;
if ( strlen( $key ) > 0 ) {
if ( array_key_exists( $key, $lang_strings ) ) {
echo "DUPLICATE: $key (line: $counter)\n";
} else {
$lang_strings[$key] = $string;
}
}
}
}
# - ---
function print_usage() {
echo "\nUsage:\n php -q check_lang.php <path/folder>\n";
}
# - ---
# -- MAIN --
$argv = $_SERVER['argv'];
$argc = $_SERVER['argc'];
# too few arguments?
if ( $argc < 2 ) {
print_usage();
exit;
} else if ( !is_dir( $argv[1] ) ) {
print_usage();
exit;
}
grab_lang_files();
foreach( $lang_files as $file ) {
if ( 'strings_english.txt' != $file ) {
echo "Processing: $file\n";
check_missing( $file );
check_duplicates( $file );
}
}
?>