Skip to content

Commit 1763aa8

Browse files
committed
Update
1 parent dd4a008 commit 1763aa8

4 files changed

Lines changed: 60 additions & 48 deletions

File tree

app/src/main/java/com/omarea/common/shell/ShellTranslation.kt

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,26 @@
11
package com.omarea.common.shell
22

33
import android.content.Context
4-
import android.content.res.Configuration
5-
import android.content.res.Resources
6-
import java.io.File
7-
import java.util.Locale
4+
import com.tool.tree.LanguageManager
85

96
// 从Resource解析字符串,实现输出内容多语言
10-
class ShellTranslation(val context: Context) {
7+
class ShellTranslation(private val context: Context) {
8+
119
// 示例:
1210
// @string:home_shell_01
1311
private val resRegex =
14-
Regex("@(string|dimen)[:/][_a-z][_0-9a-z]*", RegexOption.IGNORE_CASE)
15-
16-
private val appResources: Resources by lazy {
17-
runCatching {
18-
val langFile = File(context.filesDir, "home/usr/log/language")
19-
val lang = langFile
20-
.takeIf { it.exists() }
21-
?.readText()
22-
?.trim()
23-
?.takeIf { it.isNotEmpty() }
24-
?: return@runCatching context.resources
25-
val locale = Locale.forLanguageTag(lang.replace("_", "-"))
26-
val config = Configuration(context.resources.configuration)
27-
config.setLocale(locale)
28-
context.createConfigurationContext(config).resources
29-
}.getOrElse {
30-
context.resources
31-
}
32-
}
12+
Regex("@(string|dimen)[:/][_a-z][_0-9a-z]*", RegexOption.IGNORE_CASE)
3313

3414
fun resolveRow(originRow: String): String? {
3515
var result = originRow
36-
val res = appResources
16+
val res = LanguageManager.resources(context)
17+
3718
resRegex.findAll(originRow).forEach { match ->
38-
val row = match.value // @string:notification_ui
19+
val row = match.value
3920
val separator = if (row.contains(":")) ':' else '/'
4021
val type = row.substring(1, row.indexOf(separator))
4122
val name = row.substring(row.indexOf(separator) + 1)
42-
23+
4324
val id = res.getIdentifier(name, type, context.packageName)
4425
if (id != 0) {
4526
val value = when (type) {
@@ -58,10 +39,10 @@ class ShellTranslation(val context: Context) {
5839
fun resolveRows(rows: List<String>): String {
5940
val builder = StringBuilder()
6041
var first = true
61-
42+
6243
for (row in rows) {
6344
val resolved = resolveRow(row) ?: continue
64-
45+
6546
if (!first) {
6647
builder.append('\n')
6748
}
@@ -72,7 +53,7 @@ class ShellTranslation(val context: Context) {
7253
}
7354

7455
fun getTranslatedResult(shellCommand: String, executor: KeepShell?): String {
75-
val shell = executor?: KeepShellPublic.getDefaultInstance()
56+
val shell = executor ?: KeepShellPublic.getDefaultInstance()
7657
val rows = shell.doCmdSync(shellCommand).split("\n")
7758
return if (rows.isNotEmpty()) {
7859
resolveRows(rows)

app/src/main/java/com/omarea/krscript/config/PageConfigReader.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import java.io.ByteArrayInputStream
1717
import java.io.InputStream
1818
import java.util.Locale.getDefault
1919
import androidx.core.graphics.toColorInt
20+
import com.tool.tree.LanguageManager
2021

2122
/**
2223
* Created by Hello on 2018/04/01.
@@ -45,19 +46,24 @@ class PageConfigReader {
4546
if (!text.contains("@string")) {
4647
return text
4748
}
49+
50+
val res = LanguageManager.resources(context)
51+
4852
var result = text
4953
STRING_REF_REGEX.findAll(text).forEach { match ->
5054
val token = match.value
5155
val separator = if (token.contains(":")) ':' else '/'
5256
val name = token.substring(token.indexOf(separator) + 1)
57+
5358
try {
54-
val id = context.resources.getIdentifier(name, "string", context.packageName)
59+
val id = res.getIdentifier(name, "string", context.packageName)
5560
if (id != 0) {
56-
result = result.replace(token, context.getString(id))
61+
result = result.replace(token, res.getString(id))
5762
}
5863
} catch (_: Exception) {
5964
}
6065
}
66+
6167
return result
6268
}
6369

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// LanguageManager.kt
2+
package com.tool.tree
3+
4+
import android.content.Context
5+
import android.content.res.Configuration
6+
import android.content.res.Resources
7+
import java.io.File
8+
import java.util.Locale
9+
10+
object LanguageManager {
11+
@Volatile private var appResources: Resources? = null
12+
13+
@Synchronized
14+
fun init(context: Context) {
15+
runCatching {
16+
val lang = File(context.filesDir, "home/usr/log/language")
17+
.takeIf { it.exists() }?.readText()?.trim()?.takeIf { it.isNotEmpty() }
18+
if (lang != null) {
19+
val locale = Locale.forLanguageTag(lang.replace("_","-"))
20+
if (Locale.getDefault() != locale) {
21+
Locale.setDefault(locale)
22+
val config = Configuration(context.resources.configuration)
23+
config.setLocale(locale)
24+
context.resources.updateConfiguration(config, context.resources.displayMetrics)
25+
}
26+
}
27+
appResources = context.resources
28+
}.getOrElse { appResources = context.resources }
29+
}
30+
31+
fun resources(context: Context): Resources {
32+
if (appResources == null) init(context)
33+
return appResources ?: context.resources
34+
}
35+
36+
fun reload(context: Context) {
37+
appResources = null
38+
init(context)
39+
}
40+
}

app/src/main/java/com/tool/tree/SplashActivity.kt

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class SplashActivity : AppCompatActivity() {
4141
private var started = false
4242

4343
override fun onCreate(savedInstanceState: Bundle?) {
44-
applyAppLanguage()
44+
LanguageManager.init(this)
4545
super.onCreate(savedInstanceState)
4646
ThemeModeState.switchTheme(this)
4747
binding = ActivitySplashBinding.inflate(layoutInflater)
@@ -190,21 +190,6 @@ class SplashActivity : AppCompatActivity() {
190190
.apply()
191191
}
192192

193-
// =================== CÁC HÀM TIỆN ÍCH KHÁC ===================
194-
195-
private fun applyAppLanguage() {
196-
runCatching {
197-
val langFile = File(filesDir, "home/usr/log/language")
198-
val lang = langFile.takeIf { it.exists() }?.readText()?.trim()?.takeIf { it.isNotEmpty() } ?: return
199-
val locale = Locale.forLanguageTag(lang.replace("_", "-"))
200-
if (Locale.getDefault() != locale) {
201-
Locale.setDefault(locale)
202-
val config = Configuration(resources.configuration).apply { setLocale(locale) }
203-
resources.updateConfiguration(config, resources.displayMetrics)
204-
}
205-
}
206-
}
207-
208193
private fun gotoHome() {
209194
startActivity(
210195
if (intent?.getBooleanExtra("JumpActionPage", false) == true)

0 commit comments

Comments
 (0)