How do I get Xamarin Forms UWP to install all of my language resources?

BillyMartinBillyMartin USMember ✭✭✭

I'm hoping someone has the answer to this.

I localized my app as James Montemagno instructed in his video and also put the dropdown to choose the language, but Xamarin Forms UWP will only install the resource files for the already-installed languages. How do I get Xamarin Forms UWP to install all of my language resources, even for languages not installed on the target device?

I found this workaround, but it no longer works.

https://docs.microsoft.com/en-us/previous-versions/dn482043(v=vs.140)

Best Answer

Answers

  • JohnHardmanJohnHardman GBUniversity mod

    @BillyMartin said:
    How do I get Xamarin Forms UWP to install all of my language resources, even for languages not installed on the target device?

    You mean your string (and other) resources that you have multiple language versions for in AppResources?
    If so, why do you think they are not installed? Don't you have them as Embedded resources?

    In my code, I set AppResources.Culture to the appropriate System.Globalization.CultureInfo at run-time, and my resources switch as expected.

  • BillyMartinBillyMartin USMember ✭✭✭

    It seems to work fine in Android, but not UWP. The only languages I can change to are the ones previously installed on the laptop.

  • BillyMartinBillyMartin USMember ✭✭✭
    edited July 12

    What I think you are saying, @JohnHardman , is that you check what language the system is using, then set your app to that. I can do that no problem, but what I am trying to do is have the user select a language for my app from a drop down list, whether that language is installed or not.

  • JohnHardmanJohnHardman GBUniversity mod
    edited July 12

    @BillyMartin said:
    What I think you are saying, @JohnHardman , is that you check what language the system is using, then set your app to that. I can do that no problem, but what I am trying to do is have the user select a language for my app from a drop down list, whether that language is installed or not.

    No, I've just tested it (on UWP) where I start in one language, then based on a button press swap it to another language, simply by changing AppResources.Culture. The current page needs to be re-constructed before picking up the changes as I normally populate the UI just the first time that OnAppearing is called (I tend not to do it in the page constructor, nor on subsequent calls to OnAppearing for the same page, due to particular requirements), but any subsequent pages will just get the new language text immediately that they appear.

    Strictly speaking, I also set CultureInfo.CurrentUICulture and CultureInfo.DefaultThreadCurrentUICulture, but I've just checked and they aren't required to make the language text change.

  • BillyMartinBillyMartin USMember ✭✭✭
    edited July 13

    That's interesting. Wish I knew what I was doing different. Everything you said works the same with my app, but only after going into windows settings and downloading the language. Once the language has been installed, then language switching works fine in the app without switching the entire system. Is there a permission I need to set?

    BTW, it works perfectly in debug.

  • JohnHardmanJohnHardman GBUniversity mod

    @BillyMartin said:
    Is there a permission I need to set?

    Not that I know of.

    @BillyMartin said:
    BTW, it works perfectly in debug.

    I've tested in Release - works without issue.

    @BillyMartin said:
    Everything you said works the same with my app, but only after going into windows settings and downloading the language
    ...
    BTW, it works perfectly in debug.

    If it works in a Debug build without you having to download the language, but doesn't work in a Release build, then it's almost certain to be a problem in your code. I suggest searching for anything conditionally compiled for just Release or just Debug builds to see what differs.

  • BillyMartinBillyMartin USMember ✭✭✭

    Found this @JohnHardman & @JamesMontemagno . Think this is the problem I've been talking about:

    UWP apps can be deployed using resource packages. When language resource packages are used, the packages that are installed and registered for a given user are determined by the languages in the user's language profile. The set of languages returned by the ManifestLanguages property is limited to the languages currently available on the system for the user. Languages included in the main app package manifest will always be returned; languages from resource packs will be returned only if the language is applicable for the user (that is, is in the user's preferences) and that resource package has been installed and registered for the user at the time the property is accessed.

    https://docs.microsoft.com/en-us/uwp/api/windows.globalization.applicationlanguages.manifestlanguages

    https://docs.microsoft.com/en-us/windows/uwp/design/globalizing/manage-language-and-region

    https://docs.microsoft.com/en-us/uwp/api/Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride

  • BillyMartinBillyMartin USMember ✭✭✭

    Found something highly relevant. Just have to figure out how Xamarin Forms plays into it.

    **Some kinds of apps (multilingual dictionaries, translation tools, etc.) need to override the default behavior of an app bundle, and build resources into the app package instead of having them in separate resource packages (or resource packs). This topic explains how to do that.

    By default when you build an app bundle (.appxbundle), only your default resources for language, scale, and DirectX feature level are built into the app package. Your translated resources—and your resources tailored for non-default scales and/or DirectX feature levels—are built into resource packages and they are only downloaded onto devices that need them. If a customer is buying your app from the Microsoft Store using a device with a language preference set to Spanish, then only your app plus the Spanish resource package are downloaded and installed. If that same user later changes their language preference to French in Settings, then your app's French resource package is downloaded and installed. Similar things happen with your resources qualified for scale and for DirectX feature level. For the majority of apps, this behavior constitutes a valuable efficiency, and it's exactly what you and the customer want to happen.

    But if your app allows the user to change the language on the fly from within the app (instead of via Settings), then that default behavior is not appropriate. You actually want all of your language resources to be unconditionally downloaded and installed along with the app one time, and then remain on the device. You want to build all of those resources into your app package instead of into separate resource packages..**

    https://docs.microsoft.com/en-us/windows/uwp/app-resources/build-resources-into-app-package

  • BillyMartinBillyMartin USMember ✭✭✭

    Just got it working, @JohnHardman !

    Had to add an xml file named priconfig.packaging.xml to the UWP project as outlined on one of those pages, making sure to leave out this line:

    <autoResourcePackage qualifier="Language" />

        <?xml version="1.0" encoding="utf-8" ?>
        <packaging>
          <autoResourcePackage qualifier="Scale" />
          <autoResourcePackage qualifier="DXFeatureLevel" />
        </packaging>
    

    It also instructed me to make an xml file named priconfig.default.xml listing all of the languages, but this stopped it from building. Probably because the language resources weren't actually in the UWP project, just int the ZF Project. Leaving it out didn't seem to matter, though.

    Thank you for all of your kind help!

  • JohnHardmanJohnHardman GBUniversity mod

    @BillyMartin - I suspect that might be something that has changed between versions of Visual Studio. The closest I have to that is

    <AppxBundleAutoResourcePackageQualifiers Condition="'$(AppxBundleAutoResourcePackageQualifiers)' == ''">Language|Scale|DXFeatureLevel</AppxBundleAutoResourcePackageQualifiers>
    

    in C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\AppxPackage\Microsoft.AppxPackage.Targets

  • BillyMartinBillyMartin USMember ✭✭✭

    Yeah, @JohnHardman . That code you have says that those 3 things will only be installed as needed, but be made available in an automatically downloaded and installed bundle if ever needed. The article said that we had to exclude language from the bundle so that all languages would be installed even when not needed. Not sure why yours works with that bit of code. Let's just blame it all on Microsoft! LOL

Sign In or Register to comment.