如何在swift ios中将常规字符串占位符添加到已翻译的复数.stringdict中 [英] How to add regular string placeholders to a translated plurals .stringdict in swift ios

查看:262
本文介绍了如何在swift ios中将常规字符串占位符添加到已翻译的复数.stringdict中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在swift for iOS中使用plurar stringdict翻译此字符串

I want to translate this string using a plurar stringdict in swift for iOS


  • 保持在%1 $ @

  • 留在%1 $ @

使用一个没有占位符的简单复数,这要归功于这个问题
但是当我添加一个字符串占位符我在访问它时会遇到崩溃。

Using a simple plural without placeholders works, thanks to this question But when I add a string placeholder I get a crash when accessing it.

使用以下xml 常规复数

<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@format@</string>
<key>format</key>
<dict>
    <key>NSStringFormatSpecTypeKey</key>
    <string>NSStringPluralRuleType</string>
    <key>NSStringFormatValueTypeKey</key>
    <string>li</string>
    <key>one</key>
    <string>Sleeps at your place</string>
    <key>other</key>
    <string>Sleep at your place</string>
</dict>
</dict>

并使用此swift代码引用上面的复数而不使用字符串占位符:

And using this swift code to reference the plural above without string placeholder:

 let format = NSLocalizedString("key_to_plural_above", comment: "")
 let label = String.localizedStringWithFormat(format, kidsIds.count)

问题是当我在翻译中添加字符串占位符时我遇到了崩溃当我试着读它。下面的xml是由翻译工具( lokalise )生成的,所以我认为它是正确的。

The problem is when I add a string placeholder to the translation I get a crash when I try to read it. The xml below is generated by a translation tool (lokalise) so I assume it's correct.

<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@format@</string>
<key>format</key>
<dict>
    <key>NSStringFormatSpecTypeKey</key>
    <string>NSStringPluralRuleType</string>
    <key>NSStringFormatValueTypeKey</key>
    <string>li</string>
    <key>one</key>
    <string>Sleeps at %1$@</string>
    <key>other</key>
    <string>Sleep at %1$@</string>
</dict>

使用此快速代码获取上述复数,我得到一个未知的崩溃,没有任何堆栈跟踪:

let format = NSLocalizedString("key_to_plural_above", comment: "")
let label = String.localizedStringWithFormat(format, kidsIds.count, "Name")


推荐答案

位置参数 n $ 是基于1的,所以在

Positional parameters n$ are one-based, so in

let label = String.localizedStringWithFormat(format, kidsIds.count, "Name")

名称是第二个参数,你用%2 $ @ 引用它:

"Name" is the second parameter, and you reference it with %2$@:

<key>NSStringLocalizedFormatKey</key>
<string>%#@format@</string>
<key>format</key>
<dict>
    <key>NSStringFormatSpecTypeKey</key>
    <string>NSStringPluralRuleType</string>
    <key>NSStringFormatValueTypeKey</key>
    <string>li</string>
    <key>one</key>
    <string>Sleeps at %2$@</string>
    <key>other</key>
    <string>Sleep at %2$@</string>
</dict>

在您的代码中,%1 $ @ 引用第一个参数 kidsIds.count
这不是导致崩溃的字符串。

In your code, %1$@ refers to the first argument kidsIds.count. That is not a string which leads to the crash.

或者,将它放入NSStringLocalizedFormatKey:

Alternatively, put it into the NSStringLocalizedFormatKey:

<key>NSStringLocalizedFormatKey</key>
<string>%#@format@ at %@</string>
<key>format</key>
<dict>
    <key>NSStringFormatSpecTypeKey</key>
    <string>NSStringPluralRuleType</string>
    <key>NSStringFormatValueTypeKey</key>
    <string>li</string>
    <key>one</key>
    <string>Sleeps</string>
    <key>other</key>
    <string>Sleep</string>
</dict>

这篇关于如何在swift ios中将常规字符串占位符添加到已翻译的复数.stringdict中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆