fut*_*te7 60 localization objective-c nslocalizedstring
我已经尝试使用变量作为NSLocalizedString的输入参数,但我得到的所有内容都是输入参数.我究竟做错了什么?是否可以使用变量字符串值作为NSLocalized字符串的索引?
例如,我有一些字符串,我希望显示本地化版本.但是,我想使用变量作为NSLocalizedString的参数,而不是常量字符串.同样,我想在NSLocalizedString的参数中包含格式化元素,因此我将能够使用相同的格式参数检索字符串的本地化版本.我可以做以下事情:
案例1:变量NSLocalizedstring:
NSString *varStr = @"Index1";
NSString *string1 = NSLocalizedString(varStr,@"");
Run Code Online (Sandbox Code Playgroud)
案例2:格式化的NSLocalizedString:
NSString *string1 = [NSString stringWithFormat:NSLocalizedString(@"This is an %@",@""),@"Apple"];
Run Code Online (Sandbox Code Playgroud)
(请注意,变量可以包含任何内容,而不仅仅是一组固定的字符串.)
谢谢!
Wev*_*vah 121
如果你想要的是返回本地版本的"这是Apple/Orange/what",你需要:
NSString *localizedVersion = NSLocalizedString(([NSString stringWithFormat:@"This is an %@", @"Apple"]), nil);
Run Code Online (Sandbox Code Playgroud)
(即,嵌套NSLocalizedString()和[NSString stringWithFormat:]反转.)
如果您想要的是要本地化的格式,而不是替换值,请执行以下操作:
NSString *finalString = [NSString stringWithFormat:NSLocalizedString(@"SomeFormat", nil), @"Apple"];
Run Code Online (Sandbox Code Playgroud)
在你的Localizable.strings:
SomeFormat = "This is an %@";
Run Code Online (Sandbox Code Playgroud)
Ale*_*der 22
我只想添加一个非常有用的定义,我在许多项目中使用它.
受到机器人可能性的启发,我已将此功能添加到我的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)
对于swift:
let myString = String(format: NSLocalizedString("I authorize the payment of %d ", comment: ""), amount)
Run Code Online (Sandbox Code Playgroud)
extension String {
public var localizedString: String {
return NSLocalizedString(self, comment: "")
}
public func localizedString(with arguments: [CVarArg]) -> String {
return String(format: localizedString, arguments: arguments)
}
}
Run Code Online (Sandbox Code Playgroud)
可本地化的字符串:
"Alarm:Popup:DismissOperation:DeviceMessage" = "\"%@\" will send position updates on a regular basis again.";
"Global:Text:Ok" = "OK";
Run Code Online (Sandbox Code Playgroud)
用法:
let message = "Alarm:Popup:DismissOperation:DeviceMessage".localizedString(with: [name])
Run Code Online (Sandbox Code Playgroud)
和
let title = "Global:Text:Ok".localizedString
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30420 次 |
| 最近记录: |