Resolved Concisely insert variables in localized string?

Discussion in 'Mac Programming' started by ArtOfWarfare, May 5, 2012.

  1. ArtOfWarfare, May 5, 2012
    Last edited: May 6, 2012

    macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #1
    I currently have some rather long obj-c calls that look like this:
    Code:
    [NSString stringWithFormat:@"%@ %i%% %@", NSLocalizedString(@"Capacity1", @""), [[device objectForKey:kDeviceDesignPercent] intValue], NSLocalizedString(@"Capacity2", @"")]
    This produces, a message that looks something like this:
    "Maximum capacity is 92% of design capacity."

    (But localized to the user's language, of course.)

    My question is... is there some way I could write this so that I only have one entry in my strings file (Capacity) instead of two (Capacity1 and Capacity2).

    I'm thinking such a call might look something like:
    Code:
    [NSString stringWithFormat:@"%@", NSLocalizedString(@"Capacity", @""), [[device objectForKey:kDeviceDesignPercent] intValue]
    And then inside of my strings file, I could include the %i. Why?

    I feel like the code would look simpler and I'd have to explain less to my translator. Oh, it would also make modifying the message so that if I decide to change how many variables are in it easier.

    So, any help would be greatly appreciated. Thanks!
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
  3. ArtOfWarfare, May 6, 2012
    Last edited: May 6, 2012

    thread starter macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    Excellent!

    I almost didn't post the question because I didn't think it was actually possible! Now I'm glad I did. :)

    Thanks for that answer.

    For those wondering, I have changed my code to this:
    Code:
    [NSString stringWithFormat:NSLocalizedString(@"Capacity %d%%", @""), [[device objectForKey:kDeviceDesignPercent] intValue]]
    And my strings file to this:
    Code:
    "Capacity %d%%" = "Max capacity is %d%% of designed capacity.";
     

Share This Page