Xamarin.IOS: localization does not work

2019-02-27 01:08发布

问题:

I have a Xamarin.IOS application and try to localize some images. As a guideline I took this article: https://developer.xamarin.com/guides/ios/advanced_topics/localization_and_internationalization/

I try to display the image "ImageInstructionStep1"

Here is a screenshot of my Ressources Folder:

All images have the build action "BundleResource". I assign the image in the code with this line:

        imageInstructionStep.Image = UIImage.FromBundle("ImageInstructionStep1");

And I registered the localizations in my Info.Plist:

    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundleLocalizations</key>
    <array>
        <string>de</string>
        <string>fr</string>
        <string>it</string>
    </array>

But it always displays the english version.

I checked the locale the app has when I start it with this:

        NSLocale.CurrentLocale.LocaleIdentifier;
        NSLocale.AutoUpdatingCurrentLocale.LocaleIdentifier;

and both give as a result "de_US". My expectations would have been that this would load the german localization.

Also I tried clean and rebuild the app to the simulator, including remove it from the simulator before without success.

Maybe interessting as a sidenote: all my textes who are localized in resx file are shown in german as expected.

What do I miss?

回答1:

So as I pointed out in the comments, you should put fallback resources into the Base.lproj folder.

As for your issue with InfoPlist.strings, keep in mind that they are applied only for strings in your Info.plist, so that is when you are asking for permissions etc. This could look as follows for da.lproj:

"NSLocationWhenInUseUsageDescription" = "Vil du tillade App'en til at bruge din lokation?";
"NSCalendarsUsageDescription" = "Ost";

and so on.

As for other strings, such as OK, Cancel and other buttons, you will need to add a Localizable.strings file for each folder with contents like:

"Back" = "Tilbage";
"Done" = "Færdig";
"Cancel" = "Anuller";