MvvmCross 本地化:运行时切换 [英] MvvmCross localization: switch at runtime
问题描述
有没有办法在运行时将当前语言更改为另一种语言?
例如:能够在点击按钮或启动应用时切换,获取用户语言并切换.
如何告诉插件在启动时检查用户语言?
预先感谢您的帮助.
有没有办法在运行时将当前语言更改为另一种语言?
是的,在您的
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.
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 aLanguagePickerViewModel
- this is what we did in StarWarsKinect - theStartNavigationObject
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
这篇关于MvvmCross 本地化:运行时切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!