Possible to use variables and/or parameters with NSLocalizedString?
Asked Answered
T

8

61

I have tried using a variable as an input parameter to NSLocalizedString, but all I am getting back is the input parameter. What am I doing wrong? Is it possible to use a variable string value as an index for NSLocalized string?

For example, I have some strings that I want localized versions to be displayed. However, I would like to use a variable as a parameter to NSLocalizedString, instead of a constant string. Likewise, I would like to include formatting elements in the parameter for NSLocalizedString, so I would be able to retrieved a localized version of the string with the same formatting parameters. Can I do the following:

Case 1: Variable NSLocalizedstring:

NSString *varStr = @"Index1";
NSString *string1 = NSLocalizedString(varStr,@"");

Case 2: Formatted NSLocalizedString:

NSString *string1 = [NSString stringWithFormat:NSLocalizedString(@"This is an %@",@""),@"Apple"];

(Please note that the variable can contain anything, not just a fixed set of strings.)

Thanks!

Thetisa answered 10/8, 2010 at 4:0 Comment(0)
T
4

It turns out that a missing target entry is to blame. Just checking that my current build target includes the Localizable.string file solved the problem!

Thetisa answered 14/11, 2010 at 9:18 Comment(0)
C
127

If what you want is to return the localized version of "This is an Apple/Orange/whatever", you'd want:

NSString *localizedVersion = NSLocalizedString(([NSString stringWithFormat:@"This is an %@", @"Apple"]), nil);

(I.e., the nesting of NSLocalizedString() and [NSString stringWithFormat:] are reversed.)

If what you want is the format to be localized, but not the substituted-in value, do this:

NSString *finalString = [NSString stringWithFormat:NSLocalizedString(@"SomeFormat", nil), @"Apple"];

And in your Localizable.strings:

SomeFormat = "This is an %@";
Coxcomb answered 10/8, 2010 at 4:17 Comment(4)
Yeah, but the variable can contain anything, not just "apples" or "oranges". So I need to maintain flexibility.Thetisa
Let me append my answer just in case.Coxcomb
Thank you very much; the appended answer is in fact what I was looking for -- and I hope you get an 'accept' checkbox soon.Stibine
You are not addressing the case 1: "Using variables and/or parameters with NSLocalizedString?". You can't do so and should use only const strings because localized strings should be translated before run time and string variable can be anything at runtime.Granulocyte
T
24

I just want to add one very helpful definition which I use in many of my projects.

Inspired by androids possibility, I've added this function to my header prefix file:

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

This allows you to define a localized string like the following:

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

and it can be used via:

self.labelAuthorizationText.text = NSLocalizedFormatString(@"ExampleScreenAuthorizationDescriptionLbl", self.formattedAmount, self.companyQualifier);
Thereof answered 2/6, 2014 at 6:57 Comment(0)
R
12

For swift :

let myString = String(format: NSLocalizedString("I authorize the payment of %d ", comment: ""), amount)
Ramose answered 13/4, 2017 at 14:56 Comment(0)
G
6
extension String {
    public var localizedString: String {
        return NSLocalizedString(self, comment: "")
    }

    public func localizedString(with arguments: [CVarArg]) -> String {
        return String(format: localizedString, arguments: arguments)
    }
}

Localizable.string:

"Alarm:Popup:DismissOperation:DeviceMessage" = "\"%@\" will send position updates on a regular basis again.";
"Global:Text:Ok" = "OK";

Usage:

let message = "Alarm:Popup:DismissOperation:DeviceMessage".localizedString(with: [name])

and

let title = "Global:Text:Ok".localizedString
Girardo answered 13/11, 2018 at 12:2 Comment(1)
This extensions cleans the code up so much!Immix
T
4

It turns out that a missing target entry is to blame. Just checking that my current build target includes the Localizable.string file solved the problem!

Thetisa answered 14/11, 2010 at 9:18 Comment(0)
F
2

If you have more than one variable in your localized string can you use this solution:

In Localizable.strings

"winpopup" = "#name# wins a #type# and get #points# points(s)"; 

And use stringByReplacingOccurrencesOfString to insert the values

NSString *string = NSLocalizedString(@"winpopup", nil); //"#name# wins a #type# and get #points# points(s)"
NSString *foo = [string stringByReplacingOccurrencesOfString:@"#name#" withString:gameLayer.turn];
NSString *fooo = [foo stringByReplacingOccurrencesOfString:@"#type#" withString:winMode];
NSString *msg = [fooo stringByReplacingOccurrencesOfString:@"#points#" withString:[NSString stringWithFormat:@"%i", pkt]];
NSLog(@"%@", msg);
Forbes answered 13/4, 2012 at 10:2 Comment(3)
this seems very complicated. you can just use numbered placeholders: "exampleKey" = "%1$@ has bought %3$d apples and %2$d oranges." [NSString stringWithFormat:NSLocalizedString(@"exampleKey", nil), @"Markus", 4, 3] This would output: Markus has bought 3 apples and 4 oranges.Doradorado
And than go and explain these type of cryptographic strings to your translators around the world.Forbes
%1 or %2 is what must be used.Streaky
A
0

Your ideas should work. But if you are getting back the input parameter, that means that the input parameter was not found as a key in your Localizable.strings file. Check the syntax and location of that file.

Affiche answered 10/8, 2010 at 4:58 Comment(0)
B
0

This works for me:

NSMutableString *testMessage = [NSMutableString stringWithString:NSLocalizedString(@"Some localized text", @"")];
testMessage = [NSMutableString stringWithString:[testMessage stringByAppendingString:someStringVariable]];
Barneybarnhart answered 18/11, 2020 at 15:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.