开发者

NSLocalizedString Problem- Localization of a variable that holds a string

开发者 https://www.devze.com 2023-03-10 23:15 出处:网络
//Viewcontroller.m code NSLocalizedString(@\"attributes\",@\"Attribute Name\") //Localizable.string code
//Viewcontroller.m code
NSLocalizedString(@"attributes",@"Attribute Name")

//Localizable.string code
"attributes"="attributes-french"; 

This method works great for localization of @"attributes"

Now what should be the code if I want to use a variable

I am using

//Viewcontroller.m code
NSString *Value=@"attributes开发者_StackOverflow"
NSLocalizedString(Value,@"Attribute Name"); 

//Localizable.string code
"Value"="Value-french"; 

This is not working. Can someone tell me the correct way of using NSLocalizdString for localizing a variable (that holds a string)?


You cannot localize on a variable name. You only localize on the value held by the variable. So your Localizable.strings should contain,

"attributes"="attributes-french"

If anything, you can vary portions of the string using %@ as described here.


There's not a problem with the NSLocalizedString call, but rather, the definition in your Localizable.strings file.

Since you define the variable Value as "attributes", that is what the function will use as a key to look up the correct localized string.

This should work correctly:

//Viewcontroller.m code
NSString *Value=@"attributes"
NSLocalizedString(Value,@"Attribute Name"); 

//Localizable.string code
"attributes"="Value-french"; 

I tested similar code in Swift, which then looked like this:

//Viewcontroller.swift code
let Value="attributes"
NSLocalizedString(Value, comment:"Attribute Name")

//Localizable.string code
"attributes"="Value-french"; // <- don't forget the semicolon!
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号