Xamarin Community Toolkit LocalizedString
The LocalizedString
class enables users to respond to system culture changes in C# code at runtime. It uses the built-in LocalizationResourceManager helper class to react to changes in the active CultureInfo.
Examples
The following code raises the PropertyChanged
event and regenerates the string using function provided in constructor, when the LocalizationResourceManager.Current.PropertyChanged
event is raised. As a result, the page will be updated with the localized value using the new culture.
ViewModel:
public LocalizedString AppVersion { get; } = new(() => string.Format(AppResources.Version, AppInfo.VersionString));
Page:
<Label Text="{Binding AppVersion.Localized}"/>
Output:
Version: 1.0.0
Note
For this example to work, you also need to update AppResources.Culture
when LocalizationResourceManager.Current.CurrentCulture
changes.
To do this, add the following code above the LocalizationResourceManager.Current.Init()
call:
LocalizationResourceManager.Current.PropertyChanged += (_, _) => AppResources.Culture = LocalizationResourceManager.Current.CurrentCulture;
Properties
Property | Type | Description |
---|---|---|
Localized | string | Returns a localized string using the current culture. |
Events
Events | Description |
---|---|
PropertyChanged | Provides notification of a culture change. |
Sample project
You can see this class in action in the Xamarin community toolkit sample app.