Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does localizedStringWithFormat work?

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.

like image 562
Fogmeister Avatar asked Oct 07 '14 13:10

Fogmeister


People also ask

How does NSLocalizedString work?

NSLocalizedString is a Foundation macro that returns a localized version of a string. It has two arguments: key , which uniquely identifies the string to be localized, and comment , a string that is used to provide sufficient context for accurate translation.

What is NSString?

A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.


1 Answers

Update; 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.

In other words, beside formatting, ensures the text is not re-translated (by somehow marking it as already translated).

Original answer

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.

like image 71
KirkSpaziani Avatar answered Oct 19 '22 05:10

KirkSpaziani