小组件换行文案更新
This commit is contained in:
@@ -9,7 +9,7 @@ private let keyWidgetConfig = "widget.config.v1"
|
||||
private let keyWidgetUserProfile = "widget.userProfile.v1_2"
|
||||
private let keyWidgetDailyReco = "widget.dailyReco.v1"
|
||||
|
||||
private let fallbackTextTC = "你已经很努力了,今天也值得被温柔对待。"
|
||||
private let fallbackTextTC = "你已經很努力了,今天也值得被溫柔對待。"
|
||||
private let fallbackTextEN = "You’ve been doing great — you deserve kindness today."
|
||||
|
||||
private func defaults() -> UserDefaults? {
|
||||
@@ -29,17 +29,24 @@ private func localDayKey(_ date: Date = Date()) -> String {
|
||||
}
|
||||
|
||||
private func resolveLang() -> String {
|
||||
// 仅支持 en/tc
|
||||
let preferred = Locale.preferredLanguages.first?.lowercased() ?? "en"
|
||||
return preferred.hasPrefix("zh") ? "tc" : "en"
|
||||
// 仅支持 en/tc:根据设备语言选择
|
||||
// - 传统中文(Hant / TW / HK / MO)=> tc
|
||||
// - 其他语言(含简中 zh-Hans / zh-CN)=> en(默认)
|
||||
let preferred = (Locale.preferredLanguages.first ?? "en").lowercased()
|
||||
if preferred.hasPrefix("zh-hant") { return "tc" }
|
||||
if preferred.hasPrefix("zh-tw") { return "tc" }
|
||||
if preferred.hasPrefix("zh-hk") { return "tc" }
|
||||
if preferred.hasPrefix("zh-mo") { return "tc" }
|
||||
return "en"
|
||||
}
|
||||
|
||||
private func resolveTitle(lang: String) -> String {
|
||||
lang == "en" ? "Mindfulness" : "正念"
|
||||
// 需求:品牌文案「正念」统一改为 Hey Mama
|
||||
return "Hey Mama"
|
||||
}
|
||||
|
||||
private func resolveFooterHint(lang: String) -> String {
|
||||
lang == "en" ? "Tap to open the app" : "点我回到 App"
|
||||
lang == "en" ? "Tap to open the app" : "點我回到 App"
|
||||
}
|
||||
|
||||
private func joinUrl(base: String, path: String) -> String {
|
||||
@@ -76,16 +83,44 @@ private func writeJsonDict(_ dict: [String: Any], forKey key: String) {
|
||||
defaults()?.set(raw, forKey: key)
|
||||
}
|
||||
|
||||
private func readCachedText() -> (dayKey: String?, lang: String, text: String)? {
|
||||
private func readCachedText(family: WidgetFamily) -> (dayKey: String?, lang: String, text: String)? {
|
||||
guard let d = readJsonDict(forKey: keyWidgetDailyReco) else { return nil }
|
||||
let lang = (d["lang"] as? String) ?? resolveLang()
|
||||
let dayKey = d["day_key"] as? String
|
||||
if let item = d["item"] as? [String: Any], let text = item["text"] as? String, !text.isEmpty {
|
||||
return (dayKey: dayKey, lang: lang, text: text)
|
||||
if let item = d["item"] as? [String: Any] {
|
||||
if let text = pickWidgetText(item: item, family: family), !text.isEmpty {
|
||||
return (dayKey: dayKey, lang: lang, text: text)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
private func familyKey(_ family: WidgetFamily) -> String {
|
||||
switch family {
|
||||
case .systemSmall:
|
||||
return "small"
|
||||
case .systemMedium:
|
||||
return "medium"
|
||||
case .systemLarge:
|
||||
return "large"
|
||||
default:
|
||||
return "small"
|
||||
}
|
||||
}
|
||||
|
||||
private func pickWidgetText(item: [String: Any], family: WidgetFamily?) -> String? {
|
||||
// 优先使用 App 侧预换行的结果(wrapped_text_by_family),否则回退 raw text
|
||||
if let family = family,
|
||||
let wrappedByFamily = item["wrapped_text_by_family"] as? [String: Any] {
|
||||
let key = familyKey(family)
|
||||
if let v = wrappedByFamily[key] as? String, !v.isEmpty {
|
||||
return v
|
||||
}
|
||||
}
|
||||
if let raw = item["text"] as? String, !raw.isEmpty { return raw }
|
||||
return nil
|
||||
}
|
||||
|
||||
private func readApiBaseUrl() -> String? {
|
||||
guard let d = readJsonDict(forKey: keyWidgetConfig) else { return nil }
|
||||
let base = d["apiBaseUrl"] as? String
|
||||
@@ -174,7 +209,7 @@ struct EmotionProvider: TimelineProvider {
|
||||
let today = localDayKey(Date())
|
||||
|
||||
// 1) 今日缓存优先
|
||||
if let cached = readCachedText(), cached.dayKey == today {
|
||||
if let cached = readCachedText(family: context.family), cached.dayKey == today {
|
||||
let entry = EmotionEntry(
|
||||
date: Date(),
|
||||
lang: cached.lang,
|
||||
@@ -201,7 +236,7 @@ struct EmotionProvider: TimelineProvider {
|
||||
}
|
||||
|
||||
// 3) 网络失败:用最近缓存或兜底
|
||||
if let cached = readCachedText() {
|
||||
if let cached = readCachedText(family: context.family) {
|
||||
let entry = EmotionEntry(
|
||||
date: Date(),
|
||||
lang: cached.lang,
|
||||
@@ -332,8 +367,9 @@ struct EmotionWidget: Widget {
|
||||
StaticConfiguration(kind: kind, provider: EmotionProvider()) { entry in
|
||||
EmotionWidgetView(entry: entry)
|
||||
}
|
||||
.configurationDisplayName("情绪小组件")
|
||||
.description("一段温柔提醒,陪你回到当下。")
|
||||
// 名称/描述:支持多语言(使用 Widget Extension 自己的 Localizable.strings)
|
||||
.configurationDisplayName("WIDGET_DISPLAY_NAME")
|
||||
.description("WIDGET_DESCRIPTION")
|
||||
.supportedFamilies([.systemSmall, .systemMedium, .systemLarge])
|
||||
}
|
||||
}
|
||||
|
||||
3
client/ios/情绪小组件/en.lproj/Localizable.strings
Normal file
3
client/ios/情绪小组件/en.lproj/Localizable.strings
Normal file
@@ -0,0 +1,3 @@
|
||||
"WIDGET_DISPLAY_NAME" = "Emotion Widget";
|
||||
"WIDGET_DESCRIPTION" = "A gentle reminder to return to the present.";
|
||||
|
||||
3
client/ios/情绪小组件/zh-Hant.lproj/Localizable.strings
Normal file
3
client/ios/情绪小组件/zh-Hant.lproj/Localizable.strings
Normal file
@@ -0,0 +1,3 @@
|
||||
"WIDGET_DISPLAY_NAME" = "情緒小組件";
|
||||
"WIDGET_DESCRIPTION" = "一段溫柔提醒,陪你回到當下。";
|
||||
|
||||
Reference in New Issue
Block a user