How does localizedStringWithFormat work?

2019-03-26 05:20发布

问题:

Please note

I am not asking about NSLocalizedString() the macro. I am asking about the NSString class function + (instancetype)localizedStringWithFormat:(NSString *)format, ....

These are two separate things.

Question

I'm trying to use the NSString class method localizedStringWithFormat but I just can't work out how I'm supposed to use it.

Whatever I try I don't seem to get the words to appear in the translation file using Xcode 6 export for localization. I've tried the top two variations here but nothing.

The example in the docs shows...

NSString *myString = [NSString localizedStringWithFormat:@"%@:  %f", @"Cost", 1234.56];

Does this mean I have to separate the translated words from the numbers? i.e. could I not just use...

NSString *myString = [NSString localizedStringWithFormat:@"Cost:  %f", 1234.56];

If I can use this then what would the translated phrase be and what would the translation be?

If not then why use this at all? Why not just use...

NSString *myString = [NSString stringWithFormat:@"%@:  %f", NSLocalizedString(@"Cost"), 1234.56];

What is the difference with this last one?

Either way, can someone show me how to get the actual words translated in this please.

Update

OK, at the moment I'm using a stupid work around that just seems to be misusing everything lol.

If I do...

NSString *myString = [NSString stringWithFormat:@"%@:  %f", NSLocalizedString(@"Cost"), 1234.56];

Then it translated the word "Cost" but doesn't use the correct locale for the numbers.

If I use...

NSString *myString = [NSString localizedStringWithFormat:@"%@:  %f", @"Cost", 1234.56];
// or
NSString *myString = [NSString localizedStringWithFormat:@"Cost:  %f", 1234.56];

Then Xcode "Export for localization" just ignores it completely and nothing about "Cost" is added to the translation file at all so nothing gets translated.

So, I've resorted to using...

[NSString localizedStringWithFormat:@"%@:  %f", NSLocalizedString(@"Cost", @"Cost context stuff..."), 1234.56];

This adds "Cost" to the translation file and converts the number to the correct locale but seems like I'm using a lot of redundant stuff here.

回答1:

When you think of NSLocalizedStrings - think key/value. You can play some tricks with the key, like with your second example:

@"Cost:  %f"

That's a key - the value would probably be something that looks like @"SomeOtherWord: %f", - %f is still used a part of a format string afterwards.

The last example you gave:

NSString *myString = [NSString stringWithFormat:@"%@:  %f", NSLocalizedString(@"Cost"), 1234.56];

Allows your separate out the translation of the word "Cost" with the reset of the string, which might be fine - but could be insufficient to effectively convey your meaning in all languages. Maybe there's a language that you wouldn't Label:value, then just translating "Cost" doesn't get you to good localization.

NSLocalizedString can help with most of those situations, but localization is hard and filled with special cases, so your code might have special cases too - just try your best to keep those isolated from the rest of the logic.

EDIT: Addressing actual question as it evolved:

localizedStringWithFormat:

Will change how format specifiers are replaced.

From Apple's NSString docs:

Discussion

This method is equivalent to using initWithFormat:locale: and passing the current locale as the locale argument.