iPad App-以编程方式更改语言 [英] iPad App - Change languages programmatically
问题描述
我有一个需要2种语言(英语和法语)的应用程序.
I have an app that requires 2 languages, English and French.
我已经在各自的"en.lproj"和"fr.lproj"文件夹中设置了Localizable.strings文件...并且当我更改iPad的语言时(在本机设置应用程序中),然后启动我的应用程序,实际上可以加载正确的文本(即英文或法文).
I have already set up the Localizable.strings files in their respective "en.lproj" and "fr.lproj" folders ... and when I change the iPad's language (in the native settings app), then launch my app, it does in fact load the correct text (i.e. either English copy or French copy).
我需要在两种语言之间切换UISegmentedControl,而不必重新启动应用程序.
I need to have a UISegmentedControl toggle between the 2 languages without having to restart the app.
如何获取该应用程序以更改(当前)语言,以便在我调用(重新)设置所有UILabel的文本和UIImageViews图像的方法时,它们从相对的.lproj文件夹的Localizable中读取.字符串文件?!?
我知道如何使用UISegmentedControl,这不是我的问题.我正在寻找用于设置应用程序的捆绑语言或语言环境或某些内容的代码行(因为我对internationalization.localization相当陌生).
I know how to use UISegmentedControl, and that is not my question. I'm looking more for a line of code that sets the application's bundle language or locale or something (as I'm quite new to internationalization.localization).
-
我如何为UIImageView设置图像的示例:
Example of how I set the image for a UIImageView:
myUIImageView1.image = [UIImage imageNamed:NSLocalizedString(@"myUIImageView1", @"comment for the translator")];
我如何设置UILabel文本的示例:
Example of how I set the text of a UILabel:
myLabel1.text = NSLocalizedString(@"myLabel1", @"comment for the translator");
推荐答案
发现了解决方案!
以下测试应用程序具有从正确的"Localizable.strings"文件(基于所选语言)读取所需字符串的功能: https://github.com/object2dot0/Advance-Localization-in-ios-应用
The following test app had a function that read the desired string from the correct 'Localizable.strings' file (based on the language selected): https://github.com/object2dot0/Advance-Localization-in-ios-apps
-
我接受了此代码,以及设置应用程序主要语言所需的代码(可在Brayden发布的以上答案中找到:
I took this code, and the code required to set the app's primary language (found in the above answer posted by Brayden: How to force NSLocalizedString to use a specific language), and put them together.
这是我的代码现在的样子(注意-我的UISegmentedControl在视图的viewController中调用一个函数[当UISegmentedControl的"Value Changed"方法被触发时),然后在父viewController中调用toggleLanguage函数):
Here's what my code looks like now (note - my UISegmentedControl calls a function in it's view's viewController [when the UISegmentedControl's 'Value Changed' method is triggered] that then calls the toggleLanguage function in the parent viewController):
-(void)toggleLanguage:(NSString *)primaryLanguage secondaryLanguage:(NSString *)secondaryLanguage
{
//set app's primary language
defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSArray arrayWithObjects:primaryLanguage,secondaryLanguage,nil] forKey:@"AppleLanguages"];
[defaults synchronize];
//update UILabel and UIImageViews
[self setLanguageSpecificItems];
}
-(NSString *)languageSelectedStringForKey:(NSString *)key
{
//read primary language
NSArray *appleLanguagesArray = [defaults objectForKey:@"AppleLanguages"];
NSString *currentLanguage = [appleLanguagesArray objectAtIndex:0];
//get the path to the desired lproj file
NSString *path;
if(currentLanguage==@"en")
{
path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
}
else
if(currentLanguage==@"fr")
{
path = [[NSBundle mainBundle] pathForResource:@"fr" ofType:@"lproj"];
}
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
//find and return the desired string
NSString* str=[languageBundle localizedStringForKey:key value:@"" table:nil];
return str;
}
-(void)setLanguageSpecificItems
{
myUIImageView1.image = [UIImage imageNamed:[self languageSelectedStringForKey:@"myUIImageView1"]];
myLabel1.text = [self languageSelectedStringForKey:@"myLabel1"];
}
-
感谢大家的帮助!
-克里斯·艾林森
这篇关于iPad App-以编程方式更改语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!