远程通知字符串的本地化破坏了设备设置语言和区域设置 Swift

Localization of remote notifications strings breaks device set language and zone settings Swift

我已经开始翻译我的应用程序,这是我第一次这样做,但直到现在一切都 运行 顺利,只需添加一种新语言即可轻松翻译所有用户界面,并将值翻译成新的 Main.strings (Italian) 作为我现在正在使用的那个。我的问题始于我在远程通知中使用的那些 NSLocalizedString。我没有从 Xcode 中收到任何错误,但是一旦我将这些翻译添加到 Main.strings (Italian) 文件中,设备上的界面就会恢复为英语,如果我注释掉它然后去设定的语言。我是否必须将 NSLocalizedString 的翻译放在其他地方或者我在这里做错了什么?我查看了其他帖子,解决方案与我在这里做的事情差不多。 一如既往地非常感谢。 我就是这样做的:

远程通知:

PushNotifications.sendPushNotification(to: customerFcmToken, title: String(format: NSLocalizedString("Order number: %1@", comment: ""), orderId), subtitle: String(format: NSLocalizedString("Shop: %1@", comment: ""), UserDetails.fullName!),body: String(format: NSLocalizedString("Thank you %1@! We received your order and we'll let you know when we start preparing it and when it's ready. Bye", comment: ""), customerName))

翻译成 Main.strings(意大利语)

"Order number: %1@" = "Ordine numero: %1@"
"Shop: %1@" = "Negozio: %1@"
"Thank you %1@! We received your order and we'll let you know when we start preparing it and when it's ready. Bye" = "Grazie %1@! Abbiamo ricevuto il tuo ordine e ti faremo sapere quando cominceremo la sua preparazione e quando sarà pronto per essere ritirato. Ciao."

找到问题了。 它实际上与整个本地化设置有关,所以我将把正确的步骤留给其他苦苦挣扎的人。

添加新语言之前:

  1. addFile 命令创建一个新的字符串文件。将其命名为 Localizable。

  2. 在属性检查器中从下拉菜单中选择 English 对其进行本地化。它不显示 (Base).

  3. 现在添加一种新语言。它现在也将为 Localizable.string 文件创建本地化文件。

  4. 为您将在代码中使用的所有 NSLocalizableString 添加 key/value 对。不要遗漏行尾的;,遗漏文件将无法读取。

所以现在代码是:

英语:

// Order Revceived
"orderReceivedTitle" = "Order number: %1@";
"orderReceivedSubtitle" = "Shop: %1@";
"orderReceivedBody" = "Thank you %1@! We received your order and we'll let you know when we start preparing it and when it's ready. Bye";

意大利语:

// Order Revceived
"orderReceivedTitle" = "Ordine numero: %1@";
"orderReceivedSubtitle" = "Negozio: %1@";
"orderReceivedBody" = "Grazie %1@! Abbiamo ricevuto il tuo ordine e ti faremo sapere quando cominceremo la sua preparazione e quando sarà pronto per essere ritirato. Ciao.";

推送通知:

PushNotifications.sendPushNotification(to: customerFcmToken, title: String(format: NSLocalizedString("orderReceivedTitle", comment: ""), orderId), subtitle: String(format: NSLocalizedString("orderdReceivedSubtitle", comment: ""), UserDetails.fullName!),body: String(format: NSLocalizedString("orderReceivedBody", comment: ""), customerName))

这将以发送设备设置的语言发送远程通知,接收设备将以该语言接收通知。

下一步是在接收设备设置的lagunage中显示远程通知。

为此,发送的有效负载必须使用 "loc-key""loc-args",并且在接收设备上的 Localizable.string 文件中也有它们,因为我在两个相关应用程序上使用远程通知:商店(发送)和客户(接收)。 我会在完成后立即更新答案,包括那部分代码。