NSLocalizedString格式

Pet*_*rbo 38 objective-c nslocalizedstring

我将如何使用NSLocalizedString此字符串:

[NSString stringWithFormat:@"Is “%@“ still correct for “%@“ tap “OK“ otherwise tap “Change“ to choose new contact details", individual.contactInfo, individual.name];
Run Code Online (Sandbox Code Playgroud)

在使用stringWithFormat之前我以下列方式使用它:

[NSString stringWithFormat:@"%d %@", itemCount, NSLocalizedString(@"number of items", nil)];
Run Code Online (Sandbox Code Playgroud)

Hot*_*cks 69

[NSString stringWithFormat:NSLocalizedString(@"Is “%@“ still correct for “%@“ tap “OK“ otherwise tap “Change“ to choose new contact details", @"Query if parm 1 is still correct for parm 2"), individual.contactInfo, individual.name];
Run Code Online (Sandbox Code Playgroud)

  • 不,但是stringWithFormat可以. (9认同)
  • 如果您使用的是Swift,则可以使用`String(format:NSLocalizedString(“ foo%@ baz”),“ bar”)`。 (2认同)

tro*_*foe 32

给定句子可以用某些语言中不同顺序的变量部分构造,那么我认为你应该使用位置参数[NSString stringWithFormat:]:

NSString *format = NSLocalizedString(@"number_of_items", @"Number of items");
Run Code Online (Sandbox Code Playgroud)

哪个会为英语加载以下字符串:

@"Is \"%1$@\" still correct for \"%2$@\" tap \"OK\" otherwise tap \"Change\" to choose new contact details"
Run Code Online (Sandbox Code Playgroud)

也许是法语的其他东西(我不懂法语所以我不会尝试翻译,但它可能有不同顺序的第一和第二个参数):

"French \"%2$@\" french \"%1$@\" french"
Run Code Online (Sandbox Code Playgroud)

你可以安全地格式化字符串:

NSString *translated = [NSString stringWithFormat:format individual.contactInfo, individual.name];
Run Code Online (Sandbox Code Playgroud)

  • @HotLicks我认为在所有地方都可以使用它们。 (2认同)

Ale*_*der 12

我只想添加一个非常有用的定义,我在许多项目中使用它.

我已将此功能添加到我的header prefix文件中:

#define NSLocalizedFormatString(fmt, ...) [NSString stringWithFormat:NSLocalizedString(fmt, nil), __VA_ARGS__]
Run Code Online (Sandbox Code Playgroud)

这允许您定义如下的本地化字符串:

 "ExampleScreenAuthorizationDescriptionLbl"= "I authorize the payment of %@ to %@.";
Run Code Online (Sandbox Code Playgroud)

它可以通过以下方式使用:

self.labelAuthorizationText.text = NSLocalizedFormatString(@"ExampleScreenAuthorizationDescriptionLbl", self.formattedAmount, self.companyQualifier);
Run Code Online (Sandbox Code Playgroud)

  • 但请注意,这会导致`genstrings`工具仅搜索对NSLocalizedStrings的直接引用.它不处理宏. (3认同)