与格式NSLocalizedString(NSLocalizedString with format

2019-07-20 06:04发布

如何我会用NSLocalizedString该字符串:

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

当使用stringWithFormat之前,我已经用它的方式如下:

[NSString stringWithFormat:@"%d %@", itemCount, NSLocalizedString(@"number of items", nil)];

Answer 1:

[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];


Answer 2:

由于句子可以用在某些语言不同的顺序那么我认为你应该使用定位参数与可变部分构成[NSString stringWithFormat:]

NSString *format = NSLocalizedString(@"number_of_items", @"Number of items");

这将加载英语以下字符串:

@"Is \"%1$@\" still correct for \"%2$@\" tap \"OK\" otherwise tap \"Change\" to choose new contact details"

也许别的东西法语(我不知道法国人,所以我不会尝试翻译,但它很可能具有不同的顺序,第一和第二个参数):

"French \"%2$@\" french \"%1$@\" french"

您可以放心地格式化字符串为正常:

NSString *translated = [NSString stringWithFormat:format individual.contactInfo, individual.name];


Answer 3:

我只想补充我在我的很多项目都使用一个非常有用的定义。

我已经添加了这个功能,我的header prefix文件:

#define NSLocalizedFormatString(fmt, ...) [NSString stringWithFormat:NSLocalizedString(fmt, nil), __VA_ARGS__]

这允许你定义像下面这样一个本地化的字符串:

 "ExampleScreenAuthorizationDescriptionLbl"= "I authorize the payment of %@ to %@.";

并且它可以通过使用:

self.labelAuthorizationText.text = NSLocalizedFormatString(@"ExampleScreenAuthorizationDescriptionLbl", self.formattedAmount, self.companyQualifier);


Answer 4:

迅速

//Localizable.strings

“我的本地化字符串” =“FOO%@巴兹”;

例:

myLabel.text = String(format: NSLocalizedString("my-localized-string", 
                                       comment: "foo %@ baz"), "bar") //foo bar baz


文章来源: NSLocalizedString with format