Culture Localization with Xamarin Forms and .Net Standard

mallibonemallibone USUniversity ✭✭

I recently converted a localized app from PCL to .Net Standard which is localized with RESX files as described on this page: https://developer.xamarin.com/guides/xamarin-forms/advanced/localization/

After migration I noticed that .Net Standard does no longer need the assistant code to determine the language set on the device under Android and iOS. However, when I additionally to the culture also provide a culture such as en-GB.resx it is not recognized and will always fall back to the more generic language. Any idea how I can get .Net Standard to recognize the culture?

Answers

  • ZoliZoli NLMember ✭✭
    edited December 2017

    After migration I noticed that .Net Standard does no longer need the assistant code to determine the language set on the device under Android and iOS.

    What do you mean by this?
    I also use (still with PCL) string localization, but I don't do anything in my Android/iOS code, simple call AppResources.IDS_MY_STRING to get the proper localized string.
    (I don't use extra cultures, only languages)

  • mallibonemallibone USUniversity ✭✭

    @Zoli thanks for your reply, sorry for the confusing sentence. Consider me following the PCL sample to the letter from the docs. So far it seems only possible to get a language but not respect the culture in .Net Standard. Which is odd since the tutorial clearly shows it with cultures.

  • ZoliZoli NLMember ✭✭

    Ok, it's clear now. As of the original question, my latest information about .Net Standard was that it did not support localization resources (one reason I stayed away from it yet). But it seems my info is outdated then (except cultures).

  • FrankJagFrankJag Member

    @mallibone & @Zoli: .Net Standard is clearly supporting localization using RESX exact in the way as described in the mentioned article.
    I just did it - after some trouble. You may have been run into the same mistake:
    Once you create a new localized RESX file you should carefully choose the upper/lower case of the culture identifier in the file name. On the next build this file name is used as name of folders. These folder(-name)s are included also in the apk deployed to the android device. Android as a linux is case sensitive in the file system and will not find your "wrong" localization if you have used wrong upper/lower cases. - This describes the problem.
    You may think fixing the upper/lower case of the culture identifier in the RESX file name should fix the problem. It isn't that simple. The generated folder names stay in the wrong version on a rebuild, because windows don't care about the cases in the file system. You should rename your RESX file, build/clean your solution, then quit the visual studio and delete the obj and bin folders in your .NET Standard and Android projects, then restart the visual studio, rebuild and deploy your App to your device. Now it should work.
    Additional hints:

    • You may check the spelling of the folders by looking into the generated apk file.
    • Running your app in an emulator shows the search folders of the localization in the debug output within the visual studio.
    • Your .Net Standard localization code should always work fine if you build an UWP executable using the .Net Standard assembly, because windows don't care about the spelling in the file system.
  • BananaTieBananaTie DKMember ✭✭

    For brand new Xamarin.forms projects, the article mentioned earlier by @mallibone seems not to working - or at least I am not able to make it work. All the articles I have found explaining how to do it (11 so far, not counting duplicates), are older than december 2017 and all are relying on a PCL project for Android/iOS Localization in Xamarin. The mentioned article itself relies on the strongly typed translation in a PCL project, but it also states in the beginning the sample code is broken.

    New Xamarin projects seems not to generate PCL projects, and adding a RESX file to the common code project between Android and iOS produces no way (that I could figure out) to access the .NET standard Localization.

    Is there someone here that could either produce a minimal example for cross platform localization in Xamarin without using PCL - or show me an article that helps explain it?

  • FrankJagFrankJag Member

    @BananaTie: See the attached file. It's one of my code katas :) - a small app for generating passwords using .net standard for the app's core implementation.
    The xaml contains only resource keys. The neutral localization is English and there is a second localization for German (de). It works fine for the Android and UWP version of the app. There is an iOS project included, but yet without the dependencies implemented or tested.
    The TranslateExtension is basic the same as in the article mentioned by @mallibone. I modified it a little bit to use up to date C# language features and added a simple dependency injection for the unit testing.
    I hope this helps. - Good luck!

  • BananaTieBananaTie DKMember ✭✭

    Hi @FrankJag
    Thank you for the project - it sounds perfect. I will look into it ASAP.

  • BananaTieBananaTie DKMember ✭✭

    Hello @FrankJag

    That is a great example - works perfectly on Android.

    Thank you so much for your help!

Sign In or Register to comment.