在loc-args中发送用于iOS推送通知的本地化字符串键

时间:2014-08-07 10:28:16

标签: ios7 apple-push-notifications nslocalizedstring localizable.strings

我的应用程序支持4种语言和推送通知。当我向APNS发送推送通知时,我发送了loc_key& loc-args。现在我需要在loc-args数组中发送本地化字符串,以便在应用收到推送通知时我可以在iOS应用端翻译这些字符串。

但是当我在loc-args中发送本地化字符串时,它不是在通知中心显示已翻译的字符串,而是显示本地化的密钥。

我的字符串文件包含以下2条消息:

"WINNER_ALERT"= "Congratulations! %@ won the match & became %@ player";
"ROLE_PROFESSIONAL_LOCALIZED_KEY" = "professional"

服务器发送以下有效负载

{
    aps =     {
        alert =         {
            "loc-args" =             (
                "John",
                "ROLE_PROFESSIONAL_LOCALIZED_KEY"
            );
            "loc-key" = "WINNER_ALERT";
        };
        badge = 1;
        sound = default;
    };
}

当我发送上述有效负载然后在iOS通知中心时,消息看起来像

Congratulations! John won the match & became ROLE_PROFESSIONAL_LOCALIZED_KEY player

而不是

Congratulations! JOHN won the match & became professional player

有谁能告诉我是否可以在loc-args中发送本地化字符串?如果是,我的有效载荷有什么问题?

提前致谢

2 个答案:

答案 0 :(得分:4)

我相信您的Localizable.strings文件应如下所示:

"WINNER_ALERT"= "Congratulations! %@ won the match & became %@ player";

推送通知有效负载应如下所示:

{
    aps =     {
        alert =         {
            "loc-args" = (
                "JOHN",
                "professional"
            );
            "loc-key" = "WINNER_ALERT";
        };
        badge = 1;
        sound = default;
    };
}

这将得到所需的结果:

Congratulations! JOHN won the match & became professional player

这就是为什么(根据Apple's Local and Remote Notification Programming Guide):

  • loc-key:Localizable.strings文件中用于当前本地化的警报消息字符串的键
  • loc-args:代替loc-key
  • 中的格式说明符而出现的变量字符串值

答案 1 :(得分:4)

<@> @Zak的答案就像一个魅力。

此外,如果您需要professional进行本地化,则它应该是WINNER_ARERT字符串的一部分。

如果需要为professional传递不同的字符串,则应创建多个本地化字符串。例如。 :

1)"WINNER_ALERT_PROFESSIONAL"= "Congratulations! %@ won the match & became professional player";

2)"WINNER_ALERT_SEMI_PROFESSIONAL"= "Congratulations! %@ won the match & became semi-professional player";