ios – 复数NSLocalizedString中的String参数

ios – 复数NSLocalizedString中的String参数,第1张

概述我想将字符串%@翻译为%i sun小时,并进行适当的复数化. %@包含一天,%i包含太阳时. 这是我的Localizable.stringsdict文件: <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/Pro 我想将字符串%@翻译为%i sun小时,并进行适当的复数化.

%@包含一天,%i包含太阳时.

这是我的Localizable.stringsdict文件:

<?xml version="1.0" enCoding="UTF-8"?><!DOCTYPE pList PUBliC "-//Apple//DTD PList 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><pList version="1.0">    <dict>        <key>%@ there are up to %i sun hours</key>        <dict>            <key>NsstringLocalizedFormatKey</key>            <string>%#@hours@</string>            <key>hours</key>            <dict>                <key>NsstringFormatSpecTypeKey</key>                <string>NsstringPluralRuleType</string>                <key>NsstringFormatValueTypeKey</key>                <string>i</string>                <key>zero</key>                <string>%@ there are no sun hours</string>                <key>one</key>                <string>There is one sun hour %@ </string>                <key>other</key>                <string>%@ there are up to %i sun hours</string>            </dict>        </dict>    </dict></pList>

这就是我所说的:

[Nsstring stringWithFormat:NSLocalizedString(@"%@ there are up to %i sun hours",nil),dayString,sunHours];

无论我传入哪个NSInteger作为第二个参数,翻译都将始终与“其他”模板一起使用.我把它缩小到NsstringLocalizedFormatKey是错误的,当我使用像%@%#@ hours这样的密钥时,它正在工作.

我希望字符串参数是本地化的一部分.有没有办法做到这一点?

解决方法 基于我已经完成的实验,您需要更改参数的顺序.似乎只有第一个可以用作替换规则的控制值.

这本词典

<key>%i hours %@</key><dict>    <key>NsstringLocalizedFormatKey</key>    <string>%#@hours@</string>    <key>hours</key>    <dict>        <key>NsstringFormatSpecTypeKey</key>        <string>NsstringPluralRuleType</string>        <key>NsstringFormatValueTypeKey</key>        <string>i</string>        <key>zero</key>        <string>%2$@ there are no sun hours</string>        <key>one</key>        <string>There is one sun hour %2$@ </string>        <key>other</key>        <string>%2$@ there are up to %1$d sun hours</string>    </dict></dict>

结合

[Nsstring stringWithFormat:NSLocalizedString(@"%i hours %@",sunHours,dayString];

为我产生预期的结果.请注意,我已将参数索引添加到替换字符串,以便正确放置值.

看起来这些文档在描述这个功能时过于雄心勃勃.例如,给出in the document titled “OS X 10.9 Release Notes”的示例(尽管链接适用于iOS)意味着您应该能够打开第二个参数:

We’re allowing the recursive formatting by applying the entire
argument List to each substituted format specifIEr.

06002

The configuration dictionary can contain

06003

但是根据指南构建字典并没有给出所述结果. (请注意,之后立即给出的示例XML与此字典不匹配.)

可能有一些我误读的东西,(或者可能存在错误),但我无法确切地说明发生了什么.现在,我将离开这个“改变参数顺序将解决你的问题”.

总结

以上是内存溢出为你收集整理的ios – 复数NSLocalizedString中的String参数全部内容,希望文章能够帮你解决ios – 复数NSLocalizedString中的String参数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1058770.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存