ios - 如何在代码中提取由 stringsdict 文件本地化的参数?

标签 ios objective-c cocoa cocoa-touch localization

有没有办法从 stringsdict 文件中提取本地化单词以在代码中执行字符串比较?

例如

NSStringLocalizedFormatKey:%@ 和 %#@lu_total_users@ 评论了你的帖子

可以生成 John 和其他 2 人对您的帖子发表评论的输出。

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

John and 2 others commented on your post.

这可能吗?

这是我的字符串字典文件:

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

最佳答案

您可以在 stringsdict 文件中将句子拆分为两条规则。查看下面的代码和示例 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];

在你的 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>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>

关于ios - 如何在代码中提取由 stringsdict 文件本地化的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31768209/

相关文章:

objective-c - Lego Mindstorm NXT、Cocoa 和 HiTechnic 传感器

iOS - 更改语言后不应用默认值

ios - 搜索栏故障

ios - UISearchBar 在事件时与 UITableView 内容重叠

objective-c - 多点触控 Cocos2d TouchesBegan 个人与触摸集

Swift 中的 Objective-C 枚举

objective-c - 试图替换已弃用的 loadnibnamed :owner

ios - 如何在 View 中制作特定 CGRect 的 UIImage

ios - 将 ARCamera 旋转变换应用于节点 (ARKit)

macos - 如何激活其他最小化的应用程序?操作系统