Protecting Localized Xamarin Forms Applications

Localization is often a key ingredient when building Xamarin Forms applications with a global audience. There are different ways to approach app localization, but most of these leverage the built-in tools provided by the .NET Framework. These include RESX files to configure localized strings and an auto-generated resource class which provides strongly-typed access to the string translation at runtime. This mechanism has been available since .NET 1.1, and is used for localization of WinForm, WPF, ASP.NET, and Xamarin applications. 

Dotfuscator can usually process localized apps without issue. We’ve built smarts into Dotfuscator to handle resource classes and seamlessly process satellite assemblies. However, recently we worked with a customer for whom this was not the case. After obfuscation, their localized Xamarin Forms application remained in the default language (English), regardless of the OS language setting. In this post, I’ll describe how this happened, and how to properly configure Dotfuscator for this scenario.

Please consider the following example. This basic Xamarin Forms app displays a flag image for the country, as well as a few localized strings for the UI controls.

1 Localized App

The default locale is English (en-US), with the ability to change different languages based on OS language preferences. The localization strings are configured via RESX files, along with an auto-generated “AppResources” class file to access the translated strings at runtime:

2 RESX FILES

I’ve protected this app with the default Dotfuscator settings. After doing so, when I change the default OS to a different language, notice the image (flag) will correctly update, but the text strings remain in the default English:

3 Non Localized Strings

This happened because Dotfuscator renamed the auto-generated AppResources class. Normally, Dotfuscator’s Xaml Analysis automatically matches the Xaml to the corresponding class name. However, in this case, the compiled Xaml still looks for the original AppResources class name when displaying the localized strings. 

4 Xaml UnRenamed

In order to prevent the UI from displaying the wrong localized strings, I open the DotfuscatorConfig.xml in the Dotfuscator Professional Config Editor, and exclude the auto-generated AppResources class from the Rename transform:

After configuring this rename exclusion and re-deploying the APK, the localized strings are properly displayed:

6 Proper Localization

In most instances, Dotfuscator handles app localization without issue. But If you notice that the strings do not follow the OS language settings after protecting your localized Xamarin Forms app, you may need to exclude the auto-generated resource class from renaming.

The full example can be downloaded here.

This example is based on the ResX Localization demo found here.

If you have any feedback on this topic, or other topics you would like us to discuss in the Support Corner, please feel free to contact us at support [at] preemptive [dot] com.