From d13e673115da89982bc29d1868b022afc062b801 Mon Sep 17 00:00:00 2001
From: Ariver <shanghai3168@gmail.com>
Date: Sun, 24 May 2026 13:54:28 +0800
Subject: [PATCH] Add automatic language mode and localized help menu

---
 Apptag/L10n.swift |   67 ++++++++++++++++++++++++++++++++-
 1 files changed, 64 insertions(+), 3 deletions(-)

diff --git a/Apptag/L10n.swift b/Apptag/L10n.swift
index 0a72b7c..0d1f1b3 100644
--- a/Apptag/L10n.swift
+++ b/Apptag/L10n.swift
@@ -7,6 +7,7 @@
 }
 
 enum L10n {
+    static let automaticCode = "auto"
     static var current: [String: String] = [:]
     private(set) static var currentCode = "en"
 
@@ -48,13 +49,52 @@
         ("sv", "Svenska"),
     ]
 
+    static var selectedLanguageCode: String {
+        UserDefaults.standard.string(forKey: "appLanguage") ?? automaticCode
+    }
+
+    static var isAutomatic: Bool {
+        UserDefaults.standard.string(forKey: "appLanguage") == nil
+    }
+
+    static func switchSelection(to code: String) {
+        if code == automaticCode {
+            switchToAutomatic()
+        } else {
+            switchTo(code)
+        }
+    }
+
+    static func switchToAutomatic() {
+        let code = fallbackCode()
+        let previousCode = currentCode
+        load(code)
+        UserDefaults.standard.removeObject(forKey: "appLanguage")
+        let relocalizedTags = previousCode != currentCode
+            ? TagDatabase.relocalizeSystemTagsForCurrentLanguage()
+            : false
+        NotificationCenter.default.post(
+            name: .appLanguageDidChange,
+            object: nil,
+            userInfo: ["code": automaticCode, "effectiveCode": currentCode]
+        )
+        if relocalizedTags {
+            NotificationCenter.default.post(name: .tagLauncherDataDidChange, object: nil)
+        }
+    }
+
     static func switchTo(_ code: String) {
         guard supported.contains(where: { $0.code == code }) else { return }
-        guard code != currentCode else { return }
+        let previousSelection = selectedLanguageCode
+        guard code != currentCode || previousSelection == automaticCode else { return }
         load(code)
         let relocalizedTags = TagDatabase.relocalizeSystemTagsForCurrentLanguage()
         UserDefaults.standard.set(code, forKey: "appLanguage")
-        NotificationCenter.default.post(name: .appLanguageDidChange, object: nil, userInfo: ["code": currentCode])
+        NotificationCenter.default.post(
+            name: .appLanguageDidChange,
+            object: nil,
+            userInfo: ["code": currentCode, "effectiveCode": currentCode]
+        )
         if relocalizedTags {
             NotificationCenter.default.post(name: .tagLauncherDataDidChange, object: nil)
         }
@@ -87,7 +127,7 @@
         currentCode = code
     }
 
-    private static func fallbackCode() -> String {
+    static func fallbackCode() -> String {
         let lang = Locale.preferredLanguages.first ?? "en"
         if lang.hasPrefix("zh-Hant") || lang.hasPrefix("zh-HK") || lang.hasPrefix("zh-TW") { return "zh-Hant" }
         if lang.hasPrefix("zh") { return "zh-Hans" }
@@ -124,3 +164,24 @@
 func tr(_ key: String) -> String {
     L10n.current[key] ?? key
 }
+
+enum HelpDocument {
+    private static let baseURL = "https://github.com/shanghai3168/taglauncher/releases/download/v7.6.0"
+
+    static var currentURL: URL {
+        URL(string: "\(baseURL)/Taglauncher-help-\(languageCode(for: L10n.currentCode)).pdf")!
+    }
+
+    private static func languageCode(for appLanguageCode: String) -> String {
+        switch appLanguageCode {
+        case "zh-Hans": return "zh"
+        case "zh-Hant": return "zh-Hant"
+        case "en", "ar", "ar-Najdi", "cs", "da", "de", "es", "fr", "id",
+             "it", "ja", "ko", "ms", "nb", "nl", "nn", "no", "pl", "pt-BR",
+             "ro", "ru", "sr-Cyrl", "sv", "th", "tr", "uk", "vi":
+            return appLanguageCode
+        default:
+            return "en"
+        }
+    }
+}

--
Gitblit v1.9.3