iPad App - 以编程方式更改语言 [英] iPad App - Change languages programatically

查看:152
本文介绍了iPad App - 以编程方式更改语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要2种语言,英语和法语。

I have an app that requires 2 languages, English and French.

我已经在各自的en.lproj中设置了Localizable.strings文件, fr.lproj文件夹...当我更改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.

如何做我得到应用程序来更改(当前)语言,以便当我调用一个方法(重新)设置所有UILabels的文本和UIImageViews的图像时,他们从相反的.lproj文件夹的Localizable.strings文件中读取?!?

我知道如何使用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发布的上述答案中找到:如何强迫NSLocalizedString使用特定的语言),并将它们放在一起。

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'时ethod被触发]然后调用父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"];
    }

-

感谢大家的帮助!!!

-Chris Allinson

-Chris Allinson

这篇关于iPad App - 以编程方式更改语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆