有没有一种方法可以从stringsdict文件中提取本地化单词以执行代码中的字符串比较?

例如。

NSStringLocalizedFormatKey%@ and %#@lu_total_users@ commented on your post

可能产生John and 2 others commented on your post.的输出

2 others部分是从stringsdict文件中的规则生成的。我想在文本的那部分添加一些字符串属性(例如,粗体)以产生如下输出:


  约翰和另外2个人对您的信息发表了评论。


这可能吗?

这是我的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>activity.text.new_comment.group</key>
    <dict>
        <key>NSStringLocalizedFormatKey</key>
        <string>%@ and %#@lu_total_users@ commented on your post</string>
        <key>lu_total_users</key>
        <dict>
            <key>NSStringFormatSpecTypeKey</key>
            <string>NSStringPluralRuleType</string>
            <key>NSStringFormatValueTypeKey</key>
            <string>lu</string>
            <key>one</key>
            <string>one other</string>
            <key>other</key>
            <string>%lu others</string>
        </dict>
    </dict>
</dict>
</plist>

最佳答案

您可以在stringdict文件中将句子分为两个规则。查看下面的代码和示例plist文件。

(我尝试使用第二个名称传递第四个参数,并在more键的“ one”情况下使用该名称,但这在我的计算机上不起作用,并显示字符串损坏。YMMV。)

在代码中:

int totalUsers = 2; // for example, obviously coming from somewhere else
int secondaryUsers = 0;
NSString *firstName = @"John";
if (totalUsers > 1) {
    secondaryUsers = totalUsers - 1;
}
NSString *commented =  [NSString localizedStringWithFormat:NSLocalizedString(@"COMMENTED", @"Users who commented string"), totalUsers, secondaryUsers, firstName];


在您的stringdict文件中:

<?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>COMMENTED</key>
        <dict>
            <key>NSStringLocalizedFormatKey</key>
            <string>%1$#@users@</string>
            <key>users</key>
            <dict>
                <key>NSStringFormatSpecTypeKey</key>
                <string>NSStringPluralRuleType</string>
                <key>NSStringFormatValueTypeKey</key>
                <string>d</string>
                <key>one</key>
                <string>%3$@ commented on your post.</string>
                <key>other</key>
                <string>%3$@ and %2$#@more@ commented on your post.</string>
            </dict>
            <key>more</key>
            <dict>
                <key>NSStringFormatSpecTypeKey</key>
                <string>NSStringPluralRuleType</string>
                <key>NSStringFormatValueTypeKey</key>
                <string>d</string>
                <key>other</key>
                <string>%d others</string>
            </dict>
        </dict>
    </dict>
</plist>

09-07 13:38