2016-08-10 40 views
1

比方說,我有以下字符串:我應該如何使用多個參數進行本地化?

[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,第三是colleagueName請最好的方法是什麼?

回答

1

由於通常情況下,我的同事幾乎就按照我的要求就在這裏找到了解決辦法!顯然Objective-C具有位置參數

這些位置是1索引的,所以%[email protected]引用第一個參數。

NSString *firstParam = @"1st"; 
NSString *secondParam = @"2nd"; 
NSLog(@"First %[email protected] Second: %[email protected]", firstParam, secondParam); 
NSLog(@"Second %[email protected] First: %[email protected]", firstParam, secondParam); 

此打印:

First 1st Second: 2nd 
Second 2nd First: 1st 
-1

你可以嘗試這樣的:

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]; 
    } 
相關問題