Ios 如何使用多个参数进行本地化?
假设我有以下字符串:Ios 如何使用多个参数进行本地化?,ios,objective-c,cocoa-touch,localization,Ios,Objective C,Cocoa Touch,Localization,假设我有以下字符串: [NSString stringWithFormat:@"Booked for %@ at %@", colleagueName, time]; 我意识到我忘了定位那个字符串,所以我替换了它: [NSString stringWithFormat:NSLocalizedString(@"bookings.bookedFor", "Booked for user at time"), colleagueName, time]; 现在,当进行翻译时,我发现语言X反过来需要
[NSString stringWithFormat:@"Booked for %@ at %@", colleagueName, time];
我意识到我忘了定位那个字符串,所以我替换了它:
[NSString stringWithFormat:NSLocalizedString(@"bookings.bookedFor", "Booked for user at time"), colleagueName, time];
现在,当进行翻译时,我发现语言X反过来需要参数;更接近于:
<time> for booking of <colleague> is done.
已完成预订的。
现在,我需要格式化字符串的第二个参数是time
,第三个参数是collaguename
,请问,解决这个问题的最佳方法是什么?您可以这样尝试:
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
if ([language isEqualToString:@"X"]) {//X is language code like "fr","de"
[NSString stringWithFormat:NSLocalizedString(@"bookings.bookedFor", "Booked for user at time"), time, colleagueName];
} else {
[NSString stringWithFormat:NSLocalizedString(@"bookings.bookedFor", "Booked for user at time"), colleagueName,time];
}
您可以这样尝试:
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
if ([language isEqualToString:@"X"]) {//X is language code like "fr","de"
[NSString stringWithFormat:NSLocalizedString(@"bookings.bookedFor", "Booked for user at time"), time, colleagueName];
} else {
[NSString stringWithFormat:NSLocalizedString(@"bookings.bookedFor", "Booked for user at time"), colleagueName,time];
}
通常情况下,我的同事几乎在我提出问题时就找到了解决方案!显然,Objective-C有位置参数 位置是1索引的,因此
%1$@
引用第一个参数
NSString *firstParam = @"1st";
NSString *secondParam = @"2nd";
NSLog(@"First %1$@ Second: %2$@", firstParam, secondParam);
NSLog(@"Second %2$@ First: %1$@", firstParam, secondParam);
这张照片是:
First 1st Second: 2nd
Second 2nd First: 1st
通常情况下,我的同事几乎在我提出问题时就找到了解决方案!显然,Objective-C有位置参数 位置是1索引的,因此
%1$@
引用第一个参数
NSString *firstParam = @"1st";
NSString *secondParam = @"2nd";
NSLog(@"First %1$@ Second: %2$@", firstParam, secondParam);
NSLog(@"Second %2$@ First: %1$@", firstParam, secondParam);
这张照片是:
First 1st Second: 2nd
Second 2nd First: 1st