Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用多个参数进行本地化?_Ios_Objective C_Cocoa Touch_Localization - Fatal编程技术网

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