Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSLocalizedString question

NSLocalizedString question
Thread Tools
macrophyllum
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Nov 1, 2004, 01:15 PM
 
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
Guest
Status:
Reply With Quote
Nov 1, 2004, 01:45 PM
 
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. )
     
macrophyllum  (op)
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Nov 1, 2004, 02:07 PM
 
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";
     
hayesk
Guest
Status:
Reply With Quote
Nov 1, 2004, 02:52 PM
 
I think this what you want instead of NSLocalizedString:

localizedStringWithFormat - it's in the NSString class. Look it up here (or in your own docs):

http://developer.apple.com/documenta...ingWithFormat_
     
smeger
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Nov 1, 2004, 07:40 PM
 
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.
     
macrophyllum  (op)
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
Nov 1, 2004, 09:18 PM
 
Thanks smeger. Works great.
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 01:16 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,