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

sorin
  • 161,544
  • 178
  • 535
  • 806
chits12345
  • 11
  • 2

2 Answers2

1

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.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
0

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!
Valdimar
  • 364
  • 5
  • 18