It’s a common practice to localize applications these days to help attract more users. Many implement localization based on the settings of the device, and generally speaking that is a fine practice as most mobile devices only have one user and will likely want to view all of their applications in the same culture. However, I was recently asked to build a kiosk application for a company that reassigns devices daily. That means the devices are not customized for the user, and some may not speak English as fluently and they do Spanish or French. In researching this, those who were used to implementing localization based on the device settings tended to look toward reloading the entire UI to pull from the right culture resources. Reloading the whole UI would likely make for a bad user experience.
I decided to try another approach to making the localization dynamic using forms messaging and virtual base methods when the culture is updated. Its a rather simple approach that gets the job done, follows MVVM, and is easily unit testable.
Step 1 – Store your culture setting
Store your culture setting and send a message or event when the change occurs.
Step 2 – Dynamic Localizer
This class is like a modified version of the Translate Markup Extension . However, the call here is made from the ViewModels with the next steps below.
Step 3 – Update your base ViewModel class
Add a message or event subscription in your navigation methods to get updates when you change your culture and add a virtual method that gets called in your subscription.
Step 4 – Update your existing ViewModels
Update your ViewModels to override the update method and assign your localized binded properties using the dynamic class you created.
You did it!
That’s all there is to it. You’ve created a simple way to update all your localized bindable items without restarting or changing device settings.