App localization demo using Xamarin.CommunityToolkit
- Video demonstration of this app: https://www.youtube.com/watch?v=wyKKa09z9VQ
- More info on Xamarin app localization: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/localization/text
- Great extension to manage resx-based resources: https://marketplace.visualstudio.com/items?itemName=TomEnglert.ResXManager
- In
App.xaml.cs
- Initialize
LocalizationResourceManager
with your resouce manager and optionally initial culture; - Subscribe to
PropertyChanged
to ensure that culture forAppResources
is updated whenLocalizationResourceManager.Current.CurrentCulture
is updated.
LocalizationResourceManager.Current.PropertyChanged += (_, _) => AppResources.Culture = LocalizationResourceManager.Current.CurrentCulture;
LocalizationResourceManager.Current.Init(AppResources.ResourceManager, initialCulture);
- For static strings that use
xct:Translate
.
<ContentPage Title="{xct:Translate AppName}">
- For dynamily generated strings use
LocalizedString
(currently in preview).
public LocalizedString AppVersion { get; } = new(() => string.Format(AppResources.Version, AppInfo.VersionString));
<Label Text="{Binding AppVersion.Localized}"/>
- That's it. Now just do this when you need to change current culture:
LocalizationResourceManager.Current.CurrrentCulture = newCulture;
And after that every resource string in the program will be automatically updated.