|
|
NSLocalizedString question
|
|
|
|
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status:
Offline
|
|
Hi,
I have question regarding NSLocalizedString and passing it other strings for formatting.
For example, I want to do this:
NSString *message = NSLocalizedString(@"Are you sure you want to delete %d widgets", "dialog asking user if they want to delete x number of widgets), number;
But this gives a compile error. So how do I format my NSLocalizedString call so that the %d will be replaced with my number variable?
TIA,
Ian
|
|
|
|
|
|
|
|
|
hayesk
|
|
A couple of things:
- You are missing a quotation mark before the closing parenthesis.
- Also, what does your localization file look like. I didn't think the key could include spaces.
- I don't think putting ,number at the end is correct. You may need to wrap that in an NSString constructor.
(
Last edited by hayesk; Nov 1, 2004 at 01:52 PM.
)
|
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status:
Offline
|
|
Oops, the quotes is just something I forgot to type into the forums.
My localizable.strings file looks like this:
/* dialog asking user if they want to delete x number of widgets*/
"Are you sure you want to delete %d widgets" = "Are you sure you want to delete %d widgets";
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status:
Offline
|
|
Spaces are fine in the key, but I tend to avoid them, just so that I can keep my keys manageable AND so I can quickly see if I've screwed something up and my string isn't being looked up properly.
Use NSLocalizedString like this:
Code:
NSString *myString = NSLocalizedString(@"deleteWidgets", nil);
myString = [NSString stringWithFormat: myString, number];
The first line fetches the string from LocalizableStrings that corresponds to the key "deleteWidgets". This string should be "Are you sure you want to delete %d widgets". The second line sticks the value of "number" into the spot where %d was and reassigns the result to myString. NSLocalizedString returns an autoreleased string, so nothing is leaking anywhere.
You wouldn't use localizedStringWithFormat for this necessarily - localizedStringWithFormat is used when you want to use stringWithFormat and have localization options applied to the substitutions. It's not used to look a string up from your Localizable.strings file.
|
Geekspiff - generating spiffdiddlee software since before you began paying attention.
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status:
Offline
|
|
Thanks smeger. Works great.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|