Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Thanks Dimitris. So changing the language at runtime is not that simple.</p> <p>I found a solution which works in my case:</p> <p>When the user changes the language I use the solution described by Mauro Delrio in <a href="https://stackoverflow.com/questions/1669645/how-to-force-nslocalizedstring-to-use-a-specific-language">"How to force NSLocalizedString to use a specific language"</a>. In monotouch:</p> <pre><code> string newLanguage = "nl"; myBundle = NSBundle.FromPath(NSBundle.MainBundle.PathForResource(newLanguage, "lproj")); </code></pre> <p>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:</p> <pre><code>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() }; } } </code></pre> <p>So all I have to do is call Reset like:</p> <pre><code>((AppDelegate)UIApplication.SharedApplication.Delegate).MainTabBarController.Reset(); </code></pre> <p>All current views are disposed and re-created in the correct language. Seems like a trick, but it is perfectly legal and documented, see <a href="http://developer.apple.com/library/ios/documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html#//apple_ref/occ/instp/UITabBarController/viewControllers" rel="nofollow noreferrer">Apple documentation for MainTabBarController viewControllers property</a>. 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.</p> <p>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. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload