@@ -12,10 +12,8 @@ import java.io.File
1212class ShellTranslation (val context : Context ) {
1313 // 示例:
1414 // @string:home_shell_01
15- private val regex1 = Regex (" @(string|dimen):[_a-z][_0-9a-z]+" , RegexOption .IGNORE_CASE )
16- // 示例
17- // @string/home_shell_01
18- private val regex2 = Regex (" @(string|dimen)/[_a-z][_0-9a-z]+" , RegexOption .IGNORE_CASE )
15+ private val resRegex =
16+ Regex (" @(string|dimen)[:/][_a-z][_0-9a-z]*" , RegexOption .IGNORE_CASE )
1917
2018 private val appResources: Resources by lazy {
2119 runCatching {
@@ -36,38 +34,28 @@ class ShellTranslation(val context: Context) {
3634 }
3735
3836 fun resolveRow (originRow : String ): String {
39- val separator = if (regex1.matches(originRow)) {
40- ' :'
41- } else if (regex2.matches(originRow)) {
42- ' /'
43- } else {
44- null
45- }
46- if (separator != null ) {
47- val row = originRow.trim()
48- // val resources = context.resources
49- val resources = appResources
50- val type = row.substring(1 , row.indexOf(separator)).lowercase(Locale .ENGLISH )
37+ var result = originRow
38+ val res = appResources
39+
40+ resRegex.findAll(originRow).forEach { match ->
41+ val row = match.value // @string:notification_ui
42+ val separator = if (row.contains(" :" )) ' :' else ' /'
43+ val type = row.substring(1 , row.indexOf(separator))
5144 val name = row.substring(row.indexOf(separator) + 1 )
52-
53- try {
54- val id = resources.getIdentifier(name, type, context.packageName)
55- when (type) {
56- " string" -> {
57- return resources.getString(id)
58- }
59- " dimen" -> {
60- return resources.getDimension(id).toString()
61- }
45+
46+ val id = res.getIdentifier(name, type, context.packageName)
47+ if (id != 0 ) {
48+ val value = when (type) {
49+ " string" -> res.getString(id)
50+ " dimen" -> res.getDimensionPixelSize(id).toString()
51+ else -> null
6252 }
63- } catch (_: Exception ) {
64- if (row.contains(" [(" ) && row.contains(" )]" )) {
65- return row.substring(row.indexOf(" [(" ) + 2 , row.indexOf(" )]" ))
53+ if (value != null ) {
54+ result = result.replace(row, value)
6655 }
6756 }
6857 }
69-
70- return originRow
58+ return result
7159 }
7260
7361 fun resolveRows (rows : List <String >): String {
0 commit comments