chits12345
chits12345

Reputation: 11

NSLocalizedString Problem- Localization of a variable that holds a string

//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"
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)?

Upvotes: 1

Views: 784

Answers (2)

Valdimar
Valdimar

Reputation: 374

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!

Upvotes: 0

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

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.

Upvotes: 1

Related Questions