Search code examples
localizationnsstringsystemuidevice

Localization with %@


I am trying to localize the text "System Name:" in the code below:

NSString *systemName = [NSString stringWithFormat:@"System Name: %@", [[UIDevice
currentDevice] systemName];

I do this by changing the code to this:

NSString *systemName = NSLocalizedString(@"SystemNameKey", @"System Name Info");

In my Localizable.strings file, I add the following code:

"SystemNameKey" = "System Name: %@", [[UIDevice currentDevice] systemName];

Of course, this will not work because UIKit is not imported into the Localizable.stings, and not surprisingly, when I add the import code, it does not work. I am sure there is an alternate way of doing this that I'm just not thinking of at the moment. Any ideas? I feel like I'm missing something really obvious.


Solution

  • You should try doing it this way:

    NSString *systemNameLocalized = NSLocalizedString(@"SystemNameKey", @"System Name Info");
    NSString *systemName = [NSString stringWithFormat:systemNameLocalized, [[UIDevice currentDevice] systemName]];
    

    and in your Localizable.string file:

    "SystemNameKey" = "System Name: %@";