有兩種方式定位,
本地化 - 您可以使用單一的故事板做的,你只需要仔細設置前導空間和尾隨空間約束,並且在更改語言時,操作系統將負責製作視圖英文版的鏡像(尤其是對於RTL語言)。這是如何工作的,當您從設備設置中更改語言,然後啓動應用程序時,iOS會檢查所選語言,然後加載該特定語言的故事板,但在您的情況下,您必須提供語言更改功能在應用程序本身,那麼你必須編寫一些額外的代碼來動態改變語言。
創建單獨的故事板 - 第二個是創建爲每種語言獨立的情節串連圖板和設計英語第一,然後只複製屏幕並粘貼同在阿拉伯語故事板和剛轉移到使鏡像分量一樣。
注:
在這兩種方法,你所提供的用戶更改應用程序語言的應用程序本身所以,你必須採取一些額外的努力,國產化的東西的默認行爲是您需要重新啓動應用程序才能看到效果,並且在更改設備語言時應用程序語言會發生變化,您不能簡單地更改動態語言並查看發生的UI更改,這種情況不會發生。
所以,你必須在編碼部分更改應用程序和用戶的語言應該能夠看到所選語言的用戶界面如下解釋方面做什麼,
哪個是更好的辦法?
如果應用程序具有簡單的用戶界面,也有小範圍,那麼你可以去本地化,這是多個語言的單一故事板。
但是,如果應用程序有複雜的用戶界面,應用程序的範圍也相當大,那麼我建議你去單獨的故事板,因爲使用本地化有一些限制,或者我們可以說,設置複雜的約束是棘手的用戶界面,如果你被困在用戶界面部分,那麼可能需要更多的時間,在使用單獨的故事板時,情況並非如此,因爲您可以根據需要輕鬆進行更改。
現在讓我們看看一些代碼,
下面是兩個故事板之間切換的代碼,
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if ([kGetSelectedLanguage isEqualToString:kEnglish]) { //Load Arabic Storyboard
[sender setSelected:NO];
// Get English Language Storyboard
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Arabic" bundle:nil];
// Create Navigation controller with RootViewController
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[storyBoard instantiateViewControllerWithIdentifier:@"YourRootViewController"]];
// Set Windows RootViewController
[appDelegate.window setRootViewController: navigationController];
[[NSUserDefaults standardUserDefaults] setObject:kArabic forKey:kSelectedLanguage];
[[NSUserDefaults standardUserDefaults] synchronize];
} else { //Load English Storyboard
[sender setSelected:YES];
// Get English Language Storyboard
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"English" bundle:nil];
// Create Navigation controller with RootViewController
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[storyBoard instantiateViewControllerWithIdentifier:@"YourRootViewController"]];
// Set Windows RootViewController
[appDelegate.window setRootViewController: navigationController];
[[NSUserDefaults standardUserDefaults] setObject:kEnglish forKey:kSelectedLanguage];
[[NSUserDefaults standardUserDefaults] synchronize];
}
這是重新實例化故事板中的定位的情況下(代碼並更改應用程序本身的語言),
//Change language to English
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[delegate setCountryLanguageCode:@"US" AndLanguageCode:@"en"];
//Change language to Arabic
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[delegate setCountryLanguageCode:@"EG" AndLanguageCode:@"ar"];
//Re-instantiate the storyboard
-(void)setCountryLanguageCode:(NSString *)countryCode AndLanguageCode:(NSString *)languageCode{
[NSBundle setLanguage:languageCode];
UIStoryboard *storyRef = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
self.window.rootViewController = [storyRef instantiateInitialViewController];
}
注:
提供了在應用程序不推薦蘋果語言切換功能的是可能導致設備選擇的語言和應用語言之間的混淆,但如果它是你的要求,你必須做這個反正那麼選擇是你的。
希望它可以幫助你。
你是否在應用程序本身提供語言更改選項,如從設置屏幕或其他?你有沒有使用本地化 –
是的,我已經在設置頁面 – Dhiru
@Dhiru提供了該方法,這實際上不是一個好主意。不好的做法,我建議你堅持使用國際化工具Apple已經爲開發者提供了 - https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/Introduction/Introduction.html –