Multiple language support in Universal App

2019-04-08 20:08发布

问题:

This is not a question about standard localization - I know how to localize the app, use resources, Uid's and so on - this works perfectly.

The problem is that the app comes within a bundle, therefore when the user installs the app it covers only languages that are selected in device/phone settings. But I would like to provide an option in settings that would allow choosing a language regarding the settings. For this purpose, I can use ApplicationLanguages.PrimaryLanguageOverride, which works very nice when deployed via VS, but as I've mentioned - version from the store lacks resources, as not all are installed.

Does anybody know how to bypass this bundle behavior?

The problem is also that I'm using MAT (multilingual app toolkit) and my translation comes with xliff files. I've spent quite a lot of time to find a way to convert them to resw files, without success. Is there any way to do it (or I've to write my own converter)?

回答1:

You need to use ResourceContext:

var context = new ResourceContext(); // deliberately not using getForCurrentView()
context.Languages = new string() {"fr-fr"}; 
ResourceMap resourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("Resources");
String str = resourceMap.GetValue("string1", context).ValueAsString;

More info at: 'How to load string resources' and 'ResourceContext class'ResourceContext class'.

PS. I have app in store and there is no problem with changing language without reinstall so all resources must be there



回答2:

Check out this: UWP: Resource file for languages is not deployed correctly you need to get rid of bundle in order for my code from above to work. Or you could check if chosen language is installed in OS and if not you could not allow user to choose it using:

Windows.System.UserProfile.GlobalizationPreferences.Languages