-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfix_strings.php
More file actions
56 lines (51 loc) · 1.62 KB
/
Copy pathfix_strings.php
File metadata and controls
56 lines (51 loc) · 1.62 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
#!/usr/bin/env php
<?php
/* SPDX-License-Identifier: GPL-3.0-or-later */
// Example usage: php ./scripts/fix_strings.php ./app/src/main/res/
if ($argc != 2) {
echo "USAGE: php ./scripts/fix_strings.php <res_dir>\n";
exit(1);
}
$res_dir = $argv[1];
$string_files = array();
if (file_exists($res_dir)) {
if (!is_dir($res_dir)) {
echo "$res_dir is not a directory.\n";
exit(1);
} else {
$_files = array_diff(scandir($res_dir), array('..', '.'));
foreach ($_files as $file) {
if (str_contains($file, 'values-')) {
$file = $res_dir . '/' . $file . '/strings.xml';
if (file_exists($file)) {
$string_files[] = $file;
}
}
}
}
} else {
echo "$res_dir doesn't exist.\n";
exit(1);
}
// We've got all the strings.xml files at this point
// Apply fixes
$patterns = [
/** @lang RegExp */
'/\<\;xliff\:g xmlns\:xliff\=\\\"urn\:oasis\:names\:tc\:xliff\:document\:1\.2\\\" id\=\\\"([^\"]+)\\\" example\=\\\"([^\"]+)\\\"\>\;([^\&]+)\<\;\/xliff\:g\>\;/',
/** @lang RegExp */
'/\<\;a href\=\\\"([^\"]+)\\\"\>\;([^\&]+)\<\;\/a\>\;/',
/** @lang RegExp */
'/\<\;(\w+)\>\;([^\&]+)\<\;\/(\w+)\>\;/',
];
$replacements = [
/** @lang text */
'<xliff:g id="$1" example="$2">$3</xliff:g>',
/** @lang text */
'<a href="$1">$2</a>',
/** @lang text */
'<$1>$2</$3>'
];
foreach ($string_files as $string_file) {
$contents = file_get_contents($string_file);
file_put_contents($string_file, preg_replace($patterns, $replacements, $contents));
}