42

How would I use NSLocalizedString for this string:

[NSString stringWithFormat:@"Is “%@“ still correct for “%@“ tap “OK“ otherwise tap “Change“ to choose new contact details", individual.contactInfo, individual.name];

When using stringWithFormat before I've used it in the following manner:

[NSString stringWithFormat:@"%d %@", itemCount, NSLocalizedString(@"number of items", nil)];
Peter Warbo
  • 11,136
  • 14
  • 98
  • 193
  • Hot Licks has the right answer, but I'd also suggest reading http://nshipster.com/nslocalizedstring/ – Chris Wagner Feb 20 '13 at 16:16
  • I guess my answer doesn't handle the embedded quotes, but they were broken in the original. – Hot Licks Feb 20 '13 at 17:28
  • One apparent short-coming of the iOS implementation for localized strings is that the comment is not included as a part of the key. For this reason you can have two different messages with the same English wording but different comments, and the OS will not (apparently) be able to disambiguate them. – Hot Licks Feb 20 '13 at 17:32
  • This is a [very good article](https://medium.com/@mendibarouk/enhance-your-localized-capabilities-on-your-ios-applications-d3ba17138077) about localization in Swift for a robust architecture – Mendy Apr 07 '19 at 08:10

4 Answers4

74
[NSString stringWithFormat:NSLocalizedString(@"Is “%@“ still correct for “%@“ tap “OK“ otherwise tap “Change“ to choose new contact details", @"Query if parm 1 is still correct for parm 2"), individual.contactInfo, individual.name];
Hot Licks
  • 47,103
  • 17
  • 93
  • 151
34

Given sentences can be constructed with the variable parts in a different order in some languages then I think you should use positional arguments with [NSString stringWithFormat:]:

NSString *format = NSLocalizedString(@"number_of_items", @"Number of items");

Which would load the following string for English:

@"Is \"%1$@\" still correct for \"%2$@\" tap \"OK\" otherwise tap \"Change\" to choose new contact details"

And perhaps something else for French (I don't know French so I won't attempt a translation, but it could well have the first and second argument in a different order):

"French \"%2$@\" french \"%1$@\" french"

And you can safely format the string as normal:

NSString *translated = [NSString stringWithFormat:format individual.contactInfo, individual.name];
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • You don't need to use position-specifying tokens in the English version (if the order conforms without). They can still be used in the French (or whatever) if needed. – Hot Licks Feb 20 '13 at 17:34
  • 2
    @HotLicks I think it makes sense to use them everywhere if used at all. – trojanfoe Feb 20 '13 at 17:37
13

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

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);
Alexander
  • 7,178
  • 8
  • 45
  • 75
  • Works like a charm and very elegant. – agarcian Oct 13 '14 at 20:16
  • 3
    Note, though, that this kills the `genstrings` tool that only searches for direct references to NSLocalizedStrings. It does not process macros. – AHM Dec 09 '14 at 14:59
2

Swift

//Localizable.strings

"my-localized-string" = "foo %@ baz";

Example:

myLabel.text = String(format: NSLocalizedString("my-localized-string", 
                                       comment: "foo %@ baz"), "bar") //foo bar baz
Community
  • 1
  • 1
Peter Kreinz
  • 7,979
  • 1
  • 64
  • 49