mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
feat: desktop session follows the app language (#1971)
This commit is contained in:
1 parent
55362d68ef
commit
b16b67e6c4
6 files changed
+234
-1
No files matched your search
@@ -0,0 +1,69 @@
|
||||
package com.droiddeck.launcher.runtime
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.droiddeck.launcher.core.Downloader
|
||||
import com.droiddeck.launcher.core.FileUtils
|
||||
import com.droiddeck.launcher.core.Hashes
|
||||
import com.droiddeck.launcher.session.DesktopLocale
|
||||
import java.io.File
|
||||
import java.util.Locale
|
||||
|
||||
object DesktopLanguage {
|
||||
private const val TAG = "DesktopLanguage"
|
||||
private const val INDEX = "desktop-kde.i18n.txt"
|
||||
private const val ABSENT = "absent"
|
||||
|
||||
class Pack(val locale: String, val sha256: String, val size: Long)
|
||||
|
||||
internal fun parseIndex(text: String): Map<String, Pack> = text.lineSequence().mapNotNull { line ->
|
||||
val parts = line.trim().split(' ')
|
||||
if (parts.size != 3 || !parts[0].matches(Regex("[A-Za-z_]+")) || !parts[1].matches(Regex("[0-9a-fA-F]{64}"))) return@mapNotNull null
|
||||
Pack(parts[0], parts[1], parts[2].toLongOrNull() ?: return@mapNotNull null)
|
||||
}.associateBy { it.locale }
|
||||
|
||||
/** KDE ships region packs (pt_BR, zh_TW) beside bare-language ones (pt, es). */
|
||||
internal fun candidates(locale: Locale): List<String> {
|
||||
val region = DesktopLocale.lang(locale).removeSuffix(".UTF-8")
|
||||
if (region == DesktopLocale.DEFAULT.removeSuffix(".UTF-8") || region.startsWith("en")) return emptyList()
|
||||
return listOfNotNull(region, "zh_TW".takeIf { region == "zh_HK" }, region.substringBefore('_')).distinct()
|
||||
}
|
||||
|
||||
internal fun baseUrl(desktopUrl: String) = desktopUrl.substringBeforeLast('/') + "/"
|
||||
|
||||
private fun marker(root: File, tag: String, name: String) = File(root, ".droiddeck-i18n-$tag-$name")
|
||||
|
||||
/** Installs the desktop's translations for [locale]; false when a download failed and is worth retrying. */
|
||||
@Synchronized
|
||||
fun ensure(context: Context, locale: Locale): Boolean {
|
||||
val root = LinuxRuntime.rootDir(context)
|
||||
if (!DesktopCatalog.desktopInstalled(root)) return true
|
||||
val entry = DesktopCatalog.desktopEntry(context)
|
||||
val pending = candidates(locale).filterNot { marker(root, entry.version, it).isFile }
|
||||
if (pending.isEmpty()) return true
|
||||
val base = baseUrl(entry.url)
|
||||
val index = parseIndex(Downloader.downloadString(base + INDEX) ?: return false)
|
||||
var complete = true
|
||||
for (name in pending) {
|
||||
val pack = index[name]
|
||||
if (pack == null) {
|
||||
FileUtils.writeString(marker(root, entry.version, name), ABSENT)
|
||||
continue
|
||||
}
|
||||
val download = File(context.cacheDir, "i18n-$name-${pack.sha256.take(12)}.download")
|
||||
try {
|
||||
val ok = Downloader.downloadFile(
|
||||
base + "desktop-kde-i18n-$name.tar.zst", download, true, pack.sha256, java.util.function.Consumer<Float> { },
|
||||
) && pack.sha256.equals(Hashes.sha256(download), ignoreCase = true) &&
|
||||
LinuxRuntimeInstaller.extract(context, download, root, null)
|
||||
if (ok) FileUtils.writeString(marker(root, entry.version, name), pack.sha256) else complete = false
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "language pack $name: $e")
|
||||
complete = false
|
||||
} finally {
|
||||
download.delete()
|
||||
}
|
||||
}
|
||||
return complete
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.droiddeck.launcher.session
|
||||
|
||||
import java.util.Locale
|
||||
|
||||
object DesktopLocale {
|
||||
const val DEFAULT = "C.UTF-8"
|
||||
|
||||
private val regions = mapOf(
|
||||
"en" to "US", "id" to "ID", "ms" to "MY", "cs" to "CZ", "da" to "DK", "de" to "DE",
|
||||
"es" to "ES", "fr" to "FR", "it" to "IT", "hu" to "HU", "nl" to "NL", "nb" to "NO",
|
||||
"pl" to "PL", "pt" to "PT", "ro" to "RO", "fi" to "FI", "sv" to "SE", "vi" to "VN",
|
||||
"tr" to "TR", "el" to "GR", "bg" to "BG", "ru" to "RU", "uk" to "UA", "ar" to "EG",
|
||||
"hi" to "IN", "th" to "TH", "ja" to "JP", "ko" to "KR", "zh" to "CN",
|
||||
)
|
||||
|
||||
private val systemRegions = mapOf(
|
||||
"en" to setOf("US", "GB", "AU", "CA", "IN", "NZ", "IE", "ZA", "SG"),
|
||||
"fr" to setOf("FR", "CA", "BE", "CH", "LU"),
|
||||
"de" to setOf("DE", "AT", "CH", "LU"),
|
||||
"it" to setOf("IT", "CH"),
|
||||
"nl" to setOf("NL", "BE"),
|
||||
"pt" to setOf("PT", "BR"),
|
||||
"sv" to setOf("SE", "FI"),
|
||||
"ar" to setOf("EG", "SA", "AE", "MA", "DZ", "TN", "JO", "KW", "IQ"),
|
||||
)
|
||||
|
||||
private val traditionalRegions = setOf("TW", "HK", "MO")
|
||||
|
||||
fun lang(locale: Locale): String {
|
||||
val language = when (locale.language) {
|
||||
"in" -> "id"
|
||||
"no", "nn" -> "nb"
|
||||
else -> locale.language
|
||||
}
|
||||
val region = when {
|
||||
language == "es" && SteamLanguage.forLocale(locale) == "latam" ->
|
||||
locale.country.takeIf { it.length == 2 && it != "US" } ?: "MX"
|
||||
language == "zh" -> chineseRegion(locale)
|
||||
locale.country in systemRegions[language].orEmpty() -> locale.country
|
||||
else -> regions[language] ?: return DEFAULT
|
||||
}
|
||||
return "${language}_$region.UTF-8"
|
||||
}
|
||||
|
||||
private fun chineseRegion(locale: Locale): String = when {
|
||||
locale.script == "Hans" -> "CN"
|
||||
locale.script == "Hant" -> if (locale.country == "HK") "HK" else "TW"
|
||||
locale.country == "HK" -> "HK"
|
||||
locale.country in traditionalRegions -> "TW"
|
||||
else -> "CN"
|
||||
}
|
||||
|
||||
fun language(locale: Locale): String? {
|
||||
val lang = lang(locale)
|
||||
if (lang == DEFAULT) return null
|
||||
val full = lang.removeSuffix(".UTF-8")
|
||||
val short = full.substringBefore('_')
|
||||
return when {
|
||||
short == "en" -> full
|
||||
full == "zh_HK" -> "zh_HK:zh_TW:zh"
|
||||
else -> "$full:$short"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -648,6 +648,10 @@ class SessionService : Service() {
|
||||
}
|
||||
|
||||
if (SessionState.mode == MODE_STEAM || SessionState.mode == MODE_DESKTOP) OfflineMode.apply(this, root)
|
||||
if (SessionState.mode == MODE_DESKTOP) {
|
||||
runCatching { com.droiddeck.launcher.runtime.DesktopLanguage.ensure(this, com.droiddeck.launcher.core.AppLanguage.effective(this)) }
|
||||
.onFailure { Log.w(TAG, "desktop language pack: $it") }
|
||||
}
|
||||
|
||||
val networkLink = LinuxNetworkLinkComponent(this, root)
|
||||
networkLink.attach(this)
|
||||
@@ -777,7 +781,13 @@ class SessionService : Service() {
|
||||
guest.add("USER=root")
|
||||
guest.add("PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games")
|
||||
guest.add("TERM=xterm-256color")
|
||||
guest.add("LANG=C.UTF-8")
|
||||
if (SessionState.mode == MODE_DESKTOP) {
|
||||
val appLocale = com.droiddeck.launcher.core.AppLanguage.effective(this)
|
||||
guest.add("LANG=" + DesktopLocale.lang(appLocale))
|
||||
DesktopLocale.language(appLocale)?.let { guest.add("LANGUAGE=$it") }
|
||||
} else {
|
||||
guest.add("LANG=C.UTF-8")
|
||||
}
|
||||
guest.add("BL_STEAM_LANGUAGE=" + SteamLanguage.forLocale(
|
||||
com.droiddeck.launcher.core.AppLanguage.effective(this),
|
||||
com.droiddeck.launcher.core.AppLanguage.system(this).country,
|
||||
|
||||
Reference in new issue
Block a user