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

时间:2019-06-26 05:45:04

标签: ios swift nslocalizedstring

我已经开始翻译我的应用程序,这是我第一次这样做,但是直到现在一切都运行顺利,只需添加新语言即可轻松翻译所有用户界面,并以新的{{1} },因为我现在正在使用。我的问题始于我在远程通知中使用的Main.strings (Italian)。我没有从Xcode收到任何错误,但是,只要将它们的翻译添加到NSLocalizedString文件中,设备上的界面就会回到英语,如果我注释掉翻译,那么它将使用设置的语言。我是否必须将Main.strings (Italian)的翻译放在其他地方,或者我在这里做错了什么?我看了其他帖子,解决方案几乎就是我在这里做的事情。 一如既往的感谢。 这就是我的做法:

远程通知:

NSLocalizedString

Main.strings(意大利语)中的翻译

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))

1 个答案:

答案 0 :(得分:0)

发现了问题。 它实际上与整个本地化设置有关,因此我将为那些为此苦苦挣扎的人留下正确的步骤。

在添加新语言之前:

  1. 通过addFile命令创建一个新的String文件。将其命名为Localizable。

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

  3. 现在添加新的语言。现在,它还将为Localizable.string文件创建本地化文件。

  4. 为将在代码中使用的所有NSLocalizableString添加键/值对。不要错过该行结尾的;,如果丢失该文件将无法读取。

所以现在的代码是:

英语:

// 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))

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

下一步是在接收方设置为接收消息的远程通知中显示

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