Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Sources/VQuantKit/Core/VQL10n.swift3// Description: Every user-facing string the package produces comes from its String Catalog4// (Resources/Localizable.xcstrings: English and French, with plural rules). Server-authored text —5// report content, engine titles, warnings, findings — is never translated: it stays as the server wrote it.6import Foundation78/// Lookup in the package's String Catalog. Keys are the English source text (`"%lld defects left"`).9public enum VQL10n {10 /// The languages the catalog is translated into.11 public static let languages: [VQLanguage] = VQLanguage.allCases1213 static func bundle(_ language: VQLanguage) -> Bundle {14 // fr-CA and fr share the French table; anything else reads English15 for name in language == .fr ? ["fr", "fr-CA"] : ["en"] {16 if let path = Bundle.module.path(forResource: name, ofType: "lproj"), let b = Bundle(path: path) { return b }17 }18 return .module19 }2021 static func locale(_ language: VQLanguage) -> Locale { Locale(identifier: language == .fr ? "fr_CA" : "en_CA") }2223 /// The string of `key` in `language`, with `arguments` substituted (`%@`, `%lld`, positional `%1$@`). A key the24 /// catalog does not hold comes back as it is (English), never empty.25 public static func string(_ key: String, _ language: VQLanguage = .preferred, arguments: [CVarArg] = []) -> String {26 let format = bundle(language).localizedString(forKey: key, value: key, table: nil)27 guard !arguments.isEmpty else { return format }28 return String(format: format, locale: locale(language), arguments: arguments)29 }3031 public static func string(_ key: String, _ language: VQLanguage = .preferred, _ arguments: CVarArg...) -> String {32 string(key, language, arguments: arguments)33 }34}3536public extension VQLanguage {37 /// The language of a locale: French for any `fr…`, English otherwise.38 init(locale: Locale) { self = (locale.language.languageCode?.identifier ?? locale.identifier).lowercased().hasPrefix("fr") ? .fr : .en }39}40