diff --git a/Tests/app-name-test.swift b/Tests/app-name-test.swift index 64a6857609..fa050daa48 100644 --- a/Tests/app-name-test.swift +++ b/Tests/app-name-test.swift @@ -92,41 +92,6 @@ struct AppNameTest { "two blank keys still fall back to the filename", blankBoth?.installedAppName == "Ghost") - func codes(_ preferred: [String]) -> [String] { - BundleLocalization.indexedLanguages(preferred) - } - check( - "a Simplified Chinese Mac looks up the zh_CN Apple actually keys by", - codes(["zh-Hans-CN"]).contains("zh_CN")) - check( - "a script-bearing tag also reads the zh-Hans folder most apps ship, before its region", - codes(["zh-Hans-US"]) - == ["zh-Hans-US", "zh_Hans_US", "zh-Hans", "zh_Hans", "zh-US", "zh_US", "zh", "en"]) - check( - "a script-only tag maximizes to reach the same key", - codes(["zh-Hans"]).contains("zh_CN")) - check( - "Traditional Chinese resolves to its own region, not the mainland's", - codes(["zh-Hant-TW"]).contains("zh_TW") && !codes(["zh-Hant-TW"]).contains("zh_CN")) - check( - "English stays last so a Chinese reader still types \"Calendar\"", - codes(["zh-Hans-CN"]).last == "en") - check( - "Portuguese keeps its exact, underscore and bare-language forms", - codes(["pt-BR"]) == ["pt-BR", "pt_BR", "pt", "en"]) - check( - "Bokmål reads Apple's no key after an explicit nb localization", - codes(["nb-NO"]) == ["nb-NO", "nb_NO", "nb", "no", "en"]) - check( - "a bare Bokmål preference also reaches no", - codes(["nb"]) == ["nb", "no", "en"]) - check( - "repeated Norwegian preferences never repeat a resource lookup", - codes(["nb-NO", "nb", "no"]) == ["nb-NO", "nb_NO", "nb", "no", "en"]) - check( - "Nynorsk does not acquire the Bokmål resource alias", - codes(["nn-NO"]) == ["nn-NO", "nn_NO", "nn", "en"]) - /// The whole path: a bundle translated only in its loctable, read as the scan reads it. func makeLocalizedApp(_ fileName: String, table: [String: Any]) -> URL { let url = root.appendingPathComponent(fileName) @@ -142,7 +107,8 @@ struct AppNameTest { func names(_ url: URL, _ preferred: [String], region: String? = "en") -> [String] { BundleLocalization.names( for: url, base: url.deletingPathExtension().lastPathComponent, - developmentRegion: region, languages: codes(preferred)) + developmentRegion: region, + languages: BundleLocalization.indexedLanguages(preferred)) } let calculator = makeLocalizedApp( @@ -222,6 +188,59 @@ struct AppNameTest { check( "an English Mac indexes only the English name", names(monitor, ["en-US"]) == ["Activity Monitor"]) + check( + "Traditional Chinese selects its own system localization", + names(monitor, ["zh-Hant-TW"]) == ["活動監視器", "Activity Monitor"]) + + let netease = root.appendingPathComponent("NeteaseMusic.app") + let neteaseResources = netease.appendingPathComponent("Contents/Resources") + for (code, name) in [("zh-Hans", "网易云音乐"), ("en", "NetEaseMusic")] { + let directory = neteaseResources.appendingPathComponent("\(code).lproj") + try? fm.createDirectory(at: directory, withIntermediateDirectories: true) + let data = try? PropertyListSerialization.data( + fromPropertyList: ["CFBundleDisplayName": name], format: .xml, options: 0) + try? data?.write(to: directory.appendingPathComponent("InfoPlist.strings")) + } + check( + "a Chinese Mac labels a third-party script-localized app in Chinese", + names(netease, ["zh-Hans-CN"]) == ["网易云音乐", "NetEaseMusic"]) + + let serbianNotes = root.appendingPathComponent("Notes.app") + let serbianResources = serbianNotes.appendingPathComponent("Contents/Resources") + for (code, name) in [("sr-Latn", "Beleške"), ("en", "Notes")] { + let directory = serbianResources.appendingPathComponent("\(code).lproj") + try? fm.createDirectory(at: directory, withIntermediateDirectories: true) + let data = try? PropertyListSerialization.data( + fromPropertyList: ["CFBundleDisplayName": name], format: .xml, options: 0) + try? data?.write(to: directory.appendingPathComponent("InfoPlist.strings")) + } + check( + "a regional preference selects a non-Chinese script-only localization", + names(serbianNotes, ["sr-Latn-RS"]) == ["Beleške", "Notes"]) + + let serbianDevelopment = makeLocalizedApp( + "Beleške.app", table: ["en": ["CFBundleName": "Notes"]]) + check( + "a script-bearing development language ranks the base name with that script", + names(serbianDevelopment, ["sr-Latn-RS"], region: "sr-Latn") + == ["Beleške", "Notes"]) + + let bilingual = makeLocalizedApp( + "Name.app", + table: ["fr": ["CFBundleName": "Nom"], "en": ["CFBundleName": "Name"]]) + check( + "an unsupported first preference does not outrank a supported second preference", + names(bilingual, ["ja-JP", "fr-FR"]) == ["Nom", "Name"]) + + let latinAmerican = makeLocalizedApp( + "Name.app", + table: [ + "es_419": ["CFBundleName": "Nombre"], + "en": ["CFBundleName": "Name"], + ]) + check( + "a regional Spanish preference reaches the Latin American fallback", + names(latinAmerican, ["es-MX"]) == ["Nombre", "Name"]) // WeChat names itself only in `zh-Hans.lproj`, which a `zh-Hans-US` Mac never reached. let weChat = root.appendingPathComponent("WeChat.app") diff --git a/Tinycast/Platform/BundleLocalization.swift b/Tinycast/Platform/BundleLocalization.swift index 9af7fe01ea..30249deedf 100644 --- a/Tinycast/Platform/BundleLocalization.swift +++ b/Tinycast/Platform/BundleLocalization.swift @@ -4,38 +4,7 @@ import Foundation enum BundleLocalization { /// Preferred languages first, English last: a user who reads Thai still types "Calendar". nonisolated static func indexedLanguages(_ preferred: [String]) -> [String] { - var codes: [String] = [] - var seen = Set() - for tag in preferred + ["en"] { - let bare = tag.split(separator: "-").first.map(String.init) ?? tag - let aliases = resourceAlias(for: bare).map { [$0] } ?? [] - for form in [tag] + scriptForms(tag) + [bare] + aliases { - // loctable keys and .lproj folders use underscores where a language tag uses "-". - let underscored = form.replacingOccurrences(of: "-", with: "_") - for code in [form, underscored] - where !code.isEmpty && seen.insert(code).inserted { - codes.append(code) - } - } - } - return codes - } - - private static func resourceAlias(for language: String) -> String? { - language == "nb" ? "no" : nil - } - - /// Most apps ship a script-bearing tag as `zh-Hans`; Apple keys it by region alone, as `zh_CN`. - private static func scriptForms(_ tag: String) -> [String] { - let subtags = tag.split(separator: "-") - guard subtags.contains(where: { $0.count == 4 && $0.allSatisfy(\.isLetter) }) - else { return [] } - let language = Locale.Language(identifier: tag) - guard let code = language.languageCode?.identifier else { return [] } - let region = - language.region ?? Locale.Language(identifier: language.maximalIdentifier).region - return [language.script?.identifier, region?.identifier].compactMap { $0 } - .map { "\(code)-\($0)" } + preferred + ["en"] } /// Every name the bundle carries, most preferred language first. `base` — an app's file name, a @@ -45,13 +14,7 @@ enum BundleLocalization { ) -> [String] { let resources = bundleURL.appendingPathComponent("Contents/Resources", isDirectory: true) let table = plist(at: resources.appendingPathComponent("InfoPlist.loctable")) - let development = developmentRegion.flatMap { languageCode(of: $0) } - let developmentAlias = development.flatMap { resourceAlias(for: $0) } - let developmentFallback = languages.last { code in - guard let development else { return false } - return code.caseInsensitiveCompare(development) == .orderedSame - || code == developmentAlias - } + let development = developmentRegion.flatMap { localizationIdentifier(of: $0) } var result: [String] = [] var seen = Set() var isBaseRenamed = false @@ -61,17 +24,30 @@ enum BundleLocalization { result.append(name) } - for code in languages { + let folders = + (try? FileManager.default.contentsOfDirectory( + at: resources, includingPropertiesForKeys: nil, options: [.skipsHiddenFiles]))? + .filter { $0.pathExtension == "lproj" } + .map { $0.deletingPathExtension().lastPathComponent } ?? [] + let tableCodes = table.map { Array($0.keys) } ?? [] + var available = Set(tableCodes + folders) + if let development { available.insert(development) } + let codes = localizedCodes(available: available.sorted(), preferences: languages) + + let developmentCodes = codes.filter { code in + guard let development else { return false } + return localizationIdentifier(of: code) == development + } + + for code in codes { let strings = plist( at: resources.appendingPathComponent("\(code).lproj/InfoPlist.strings")) let translated = [table?[code] as? [String: Any], strings] .compactMap { $0.flatMap(AppDisplayName.inInfo) } translated.forEach(append) - if let development, - code.caseInsensitiveCompare(development) == .orderedSame || code == developmentAlias - { + if developmentCodes.contains(code) { if !translated.isEmpty { isBaseRenamed = true } - if code == developmentFallback && !isBaseRenamed { append(base) } + if code == developmentCodes.last && !isBaseRenamed { append(base) } } } // A development region this Mac doesn't read still leaves the name searchable. @@ -79,10 +55,37 @@ enum BundleLocalization { return result } + /// An unsupported preference must not trigger Foundation's English backstop. + private static func localizedCodes(available: [String], preferences: [String]) -> [String] { + var result: [String] = [] + var seen = Set() + let preferences = preferences.flatMap { preference -> [String] in + let language = Locale.Language(identifier: preference).languageCode?.identifier + return language == "nb" ? [preference, "no"] : [preference] + } + for preference in preferences { + let language = Locale.Language(identifier: preference) + let candidates = available.filter { + let candidate = Locale.Language(identifier: $0) + return localizationIdentifier(of: $0).map { Locale.Language(identifier: $0).languageCode } + == localizationIdentifier(of: preference).map { Locale.Language(identifier: $0).languageCode } + && candidate.script == language.script + } + guard !candidates.isEmpty else { continue } + for code in Bundle.preferredLocalizations( + from: candidates, forPreferences: [preference]) + where seen.insert(code).inserted { + result.append(code) + } + } + return result + } + /// `CFBundleDevelopmentRegion` still ships its pre-BCP-47 spelling: Safari's reads "English". - private static func languageCode(of region: String) -> String? { - Locale.Language(identifier: Locale.canonicalLanguageIdentifier(from: region)) - .languageCode?.identifier + private static func localizationIdentifier(of region: String) -> String? { + let identifier = Locale.canonicalLanguageIdentifier(from: region) + guard Locale.Language(identifier: identifier).languageCode != nil else { return nil } + return identifier == "no" ? "nb" : identifier } private static func plist(at url: URL) -> [String: Any]? { diff --git a/docs/features/launcher.md b/docs/features/launcher.md index 64cb2e519f..e97b6b928f 100644 --- a/docs/features/launcher.md +++ b/docs/features/launcher.md @@ -178,14 +178,16 @@ matches, and is learned, under the same key. ASCII text skips ICU entirely on a `BundleLocalization` reads both `InfoPlist.loctable` and `.lproj/InfoPlist.strings` for `Locale.preferredLanguages` plus English. This matters because `CFBundle` resolves only `InfoPlist.strings`, and every app under `/System/Applications` translates in the loctable alone — so -all 65 of them read English on every Mac, whatever language it is set to. - -A tag carrying a script is read under two more codes, because no one folder name covers it: a -`zh-Hans-CN` Mac also reads `zh-Hans`, the folder most third-party apps ship, then `zh_CN`, the key -Apple's own loctables use. A script-only `zh-Hans` maximizes to reach the same region. A tag without a -script otherwise keeps its exact, underscore and bare-language forms. Bokmål (`nb`) additionally -reads `no` after `nb`, because Apple's Norwegian loctables use that key; an explicit `nb.lproj` still -wins. Other languages, including Nynorsk (`nn`), gain no alias. +all 65 of them read English on every Mac, whatever language it is set to. Foundation's bundle +localization selection ranks the codes that actually exist: `zh-Hans-CN` reaches both Apple's +`zh_CN` tables and the `zh-Hans.lproj` folders common in third-party bundles. + +The scanner reads the localization codes each app actually ships, then uses Foundation to rank +matching language and script candidates for each preferred language. It does not synthesize folder +names from language tags. A preference the app does not support leaves the next preference in place. + +Bokmål (`nb`) also reads Apple's `no` resources after explicit `nb` resources. Nynorsk (`nn`) +remains separate, and a translated development name suppresses the base filename across both codes. The user's own language wins the **display name**, so a row reads the way Finder reads it. The rest, English included, ride along as alternate titles, matched as typed and never transliterated.