开发者

Change language in app - how to restart?

开发者 https://www.devze.com 2023-02-06 00:13 出处:网络
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 Engl

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"));

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?


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

I found a solution which works in my case:

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"));

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()
        };
    }
}

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.


No there is no way to restart an app. You can only force it to terminate when the users presses the home button by setting the property "UIApplicationExitsOnSuspend" in your Info.plist file to true.


The trick to use specific language by selecting it from the app is to force the NSLocalizedString to use specific bundle depending on the selected language ,

here is the post i have written for this http://learning-ios.blogspot.com/2011/04/advance-localization-in-ios-apps.html

and here is the code of one sample app https://github.com/object2dot0/Advance-Localization-in-ios-apps


As an additional note, to force the UIBarButtonSystemItem to change their locale, you have to add

<key>CFBundleDevelopmentRegion</key> 
 <string>nl</string> 

to your info.plist. Just fire up TextEdit and place it somewhere. Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消