Objective C 中的 NSLocalizedString

NSLocalized String in Objective C

在 swift 中创建本地化字符串使用 `NSLocalizedString(key: , value: , comment:) 但在较旧的 objective C https://developer.apple.com/documentation/foundation/nslocalizedstring 在 xliff 文件中似乎只有键和注释,键似乎同时充当键和值。

我的问题是如何为 objective C 中的 nslocalizedstring 提供键和值?

您正在查看宏的简化形式。完整的形式叫做 saying

NSString * NSLocalizedStringWithDefaultValue(
  NSString *key,
  NSString *tableName,
  NSBundle *bundle,
  NSString *value,
  NSString *comment
)

https://developer.apple.com/documentation/foundation/nslocalizedstringwithdefaultvalue

只要将你不关心的东西设置为零即可。

matt回答正确;然而,直接拼写出来:

在Swift中:

NSLocalizedString(key, value: value, comment: comment)

在Objective C中:

NSLocalizedStringWithDefaultValue(key, nil, NSBundle.mainBundle, value, comment)

Objective C 版本让您深入了解它是如何完成的。这是 NSBundle.h.

NSLocalizedString* 宏的代码
#define NSLocalizedString(key, comment) \
        [NSBundle.mainBundle localizedStringForKey:(key) value:@"" table:nil]
#define NSLocalizedStringFromTable(key, tbl, comment) \
        [NSBundle.mainBundle localizedStringForKey:(key) value:@"" table:(tbl)]
#define NSLocalizedStringFromTableInBundle(key, tbl, bundle, comment) \
        [bundle localizedStringForKey:(key) value:@"" table:(tbl)]
#define NSLocalizedStringWithDefaultValue(key, tbl, bundle, val, comment) \
        [bundle localizedStringForKey:(key) value:(val) table:(tbl)]