在应用中更改语言-如何重新启动? [英] Change language in app - how to restart?

查看:67
本文介绍了在应用中更改语言-如何重新启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为用户提供在我的应用程序中更改语言的可能性.

I want to give the user the possibility to change the language in my app.

此处,以单点触控代码将首选语言设置为荷兰语,将备用语言设置为英语:

The way to do this is described here, in monotouch code to set the preferred language to Dutch and alternate language to English:

  NSUserDefaults.StandardUserDefaults.SetValueForKey
                (NSArray.FromStrings("nl", "en"), new NSString("AppleLanguages"));

您必须重新启动应用程序,此方法才会生效.但是在iPhone 4上,当您关闭应用程序时,该应用程序不会重新启动,而是被隐藏了.有没有办法在关闭后强制应用重新启动?

You have to restart the application before this will take effect. But on the iPhone 4 the app does not restart when you close it, it is just hidden. Is there a way to force an app to restart after closing?

推荐答案

感谢Dimitris.因此,在运行时更改语言并不是那么简单.

Thanks Dimitris. So changing the language at runtime is not that simple.

我找到了一种适用于我的情况的解决方案:

I found a solution which works in my case:

当用户更改语言时,我使用Mauro Delrio在.在单点触控中:

When the user changes the language I use the solution described by Mauro Delrio in "How to force NSLocalizedString to use a specific language". In monotouch:

    string newLanguage = "nl";
    myBundle = NSBundle.FromPath(NSBundle.MainBundle.PathForResource(newLanguage, "lproj"));

所有字符串现在都将使用myBundle.LocalizedString(...)以选定的语言加载.当然,已经打印在视图上的所有内容都尚未翻译.但是我找到了一种重置所有视图的简便方法.在我的应用中,我使用了一个MainTabController,它看起来像这样:

All strings will now be loaded in the selected language with myBundle.LocalizedString(...). Of course, everything which was already printed on a view is not yet translated. But I found an easy way to reset all views. In my app I use a MainTabController which looks like this:

public class MainTabBarController : UITabBarController
{
    public override void ViewDidLoad()
    {
        Reset();
        SelectedIndex = 2;
    }

    public void Reset()
    {
        ViewControllers = new UIViewController[]
        {
            new ViewControllerTab1(),
            new ViewControllerTab2(),
            new ViewControllerTab3(),
            new ViewControllerTab4(),
            new ViewControllerTab5()
        };
    }
}

所以我要做的就是像下面这样调用Reset:

So all I have to do is call Reset like:

((AppDelegate)UIApplication.SharedApplication.Delegate).MainTabBarController.Reset();

以正确的语言处理并重新创建所有当前视图.似乎是个把戏,但它完全合法且有据可查,请参阅

All current views are disposed and re-created in the correct language. Seems like a trick, but it is perfectly legal and documented, see Apple documentation for MainTabBarController viewControllers property. It even activates the same tab index as the one which was active, so for the user it seems that nothing but the language is changed.

当然,所有视图中所有未保存的数据都将丢失,因此,如果这是一个问题,则必须在重置之前找到一种保存方法.

Of course, any unsaved data in all views is lost, so if this is a problem, you have to find a way to save this before resetting.

这篇关于在应用中更改语言-如何重新启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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