MvvmCross localization: switch at runtime

馋奶兔 提交于 2020-01-12 05:50:08

问题


Is there a way to change the current language to another one at runtime ?

For example: be able to switch when a button is clicked or when starting the app, get the user language and switch.

How to tell the plugin to check the user language at startup ?

Thanks in advance for your help.


回答1:


Is there a way to change the current language to another one at runtime ?

yes, call builder.LoadResources(whichLanguage) on your MvxTextProviderBuilder.cs

For example: be able to switch when a button is clicked

The UI framework isn't really setup to perform switching live. When you switch between languages then the new JSON resource files will all be loaded OK - but existing displayed text will not be updated. This is a bit like most mobile operating systems - if you want to switch language you often need to reboot!

If you wanted to add dynamic switching then you'd have to find a way to tell the UI to completely refresh all text - I suspect this wouldn't be hard to do, but it might require some manual coding on every page/View which has already been created and displayed :/

or when starting the app, get the user language and switch.

This is a much more straight-forward way to do i18n. It's normally OK because MvvmCross mainly targets phones - and phones are normally single user devices which don't switch languages very often.

  • You could, for example, use some variable (e.g. System.Globalization.CultureInfo.CurrentUICulture) to work out the best language to display.

  • Or you detect on load whether the user has picked a language yet - if they have, then show HomeViewModel - if they haven't then show a LanguagePickerViewModel - this is what we did in StarWarsKinect - the StartNavigationObject is a perfect place for this sort of logic!

How to tell the plugin to check the user language at startup ?

Currently, you'll have to code this logic as part of your app startup.

I'd definitely be open to providing an improved plugin on this - perhaps with a demo!


If you are looking at i18n, then one alternative implementation to consider is Vernacular - the team at Rdio have a very good offering for Mono and MS platforms - https://github.com/rdio/vernacular



来源:https://stackoverflow.com/questions/13455635/mvvmcross-localization-switch-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!