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

    ArtOfWarfare macrumors G3


    Nov 26, 2007
    I currently have some rather long obj-c calls that look like this:
    [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:
    [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. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
  3. ArtOfWarfare, May 6, 2012
    Last edited: May 6, 2012

    ArtOfWarfare thread starter macrumors G3


    Nov 26, 2007

    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:
    [NSString stringWithFormat:NSLocalizedString(@"Capacity %d%%", @""), [[device objectForKey:kDeviceDesignPercent] intValue]]
    And my strings file to this:
    "Capacity %d%%" = "Max capacity is %d%% of designed capacity.";

Share This Page