Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to localize strings with numbers inside a sentence. I dont use iOS's Localizable.strings file since it's tedious and error prone and I may decide to rephrase words later on.

Here is what I did:

#define sf_buy_bombs @"Are you sure you want to buy %i bombs for $%i? "

where 'sf' suffix means 'string format'

here is when i need to use it:

[NSString stringWithFormat: [Helper getLocalizedStringWithString:sf_buy_bombs], num_shop_items_bundle, price_bombs]

and a helper method to handle the translation

+(NSString*) getLocalizedStringWithString: (NSString*) str {
    if ([Helper isSimplifiedChinese]) {
        if ([str isEqualToString:sf_buy_bombs]) {
            return @"确定要购买%i个炸弹道具吗? ($ %i)";
        }
    }
    return str;
}

The final label displayed is "确定要购买%i个炸弹道具吗? ($ %i)", the number are not substituted in.

share|improve this question

1 Answer

up vote 1 down vote accepted

In your Helper class

#define sf_buy_bombs @"Are you sure you want to buy %i bombs for $%i? "

and

+(NSString *)localizedStringWithString{
    return sf_buy_bombs;//all other stuffs 
}

In the required class:

NSString *string=[NSString stringWithFormat:[Helper localizedStringWithString],10,100];
NSLog(@"%@",string); //gives correct output as checked by me as :

//Are you sure you want to buy 10 bombs for $100? 

EDIT: with your Chinese font string: Find working Model here.

enter image description here

share|improve this answer
what is Array class ur referring to – OMGPOP Apr 8 at 14:29
Sorry for my testing i used Array, in your case it is Helper. Also check for the method name, I have skipped get – Anoop Vaidya Apr 8 at 14:30
dude, that's what i did. if it's english i return the original string and numbers are sub'ed in. but if it go through translation process and return 确定要购买%i个炸弹道具吗? ($ %i), the numbers are not sub'ed in anymore – OMGPOP Apr 8 at 14:32
@OMGPOP: check my update....it is working. – Anoop Vaidya Apr 8 at 14:35
Weird. im doing the same thing – OMGPOP Apr 8 at 14:37
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.