Dynamic localization framework for Xamarin.Forms. Using wrapper classes for controls and pages, applications are able to update localized text to the UI regardless of chosen design patterns or UI implementation (c# or xaml).
- The framework supports titles on all pages
- LocalizedCarouselPage
- LocalizedContentPage
- LocalizedFlyoutPage
- LocalizedNavigationPage
- LocalizeTemplated
- As well as text and placeholders (if the control supports it) for:
- LocalizedButton
- LocalizedEntry
- LocalizedLabel
Click here For a full example and demo.
var resourceManager = new ResourceManager("SayWhat.Forms.Demo.Resources.AppResources", Assembly.GetAssembly(typeof(MainPage))); Utilities.SayWhat.Settings.SetResourceManager(resourceManager); Utilities.SayWhat.Settings.UpdateCulture(new CultureInfo({CultureHere}));
It is recommended to initialize in the app.xaml.cs or app.cs file as it is done here.
In Xaml, create a variable from the namespace. Here I have assigned it to 'sw' for SayWhat". However, it can be anything such as: il8n, localization, translations, etc.
Now you can use controls and set the resource name of the string you want translated to the resourcename property on each control.
<?xml version="1.0" encoding="utf-8" ?>
<sw:LocalizedContentPage
xmlns="https://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="https://schemas.microsoft.com/winfx/2009/xaml"
xmlns:sw="clr-namespace:SayWhat.Maui.Controls;assembly=SayWhat.Maui"
x:Class="SayWhat.Maui.Demo.MainPage"
TitleResourceName="TranslatedTitle">
<StackLayout
HorizontalOptions="Center"
VerticalOptions="Center">
<sw:LocalizedLabel
TextResourceName="HelloWorld"
FontSize="18"/>
<sw:LocalizedEntry
PlaceHolderResourceName="PlaceholderText" />
<sw:LocalizedButton
TextResourceName="UpdateLanguage"
Clicked="Button_Clicked" />
</StackLayout>
</sw:LocalizedContentPage>
Translation of the above example in C#.
Using SayWhat.Forms
public MainPage : LocalizedContentPage
{
public MainPage()
{
TitleResourceName = "TranslatedTitle"
Content = new StackLayout
{
Children =
{
new LocalizedLabel { TextResourceName="HelloWorld", FontSize="18"},
new LocalizedEntry { PlaceHolderResourceName="PlaceholderText" },
new LocalizedButton { TextResourceName="UpdateLanguage", Clicked="Button_Clicked"}
}
}
}
}
The idea is a fairly simple one. Basically wrap the original control or page, take items that are translatable and when the culture is updated notify the control to retrieve the updated value. And of course, allow the garbage collector to clean up when no longer in use.
using CommunityToolkit.Mvvm.Messaging;
using SayWhat.Maui.Messages;
using SayWhat.Maui.Utilities;
namespace SayWhat.Maui.Controls
{
public class LocalizedContentPage : ContentPage
{
public static readonly BindableProperty TitleResourceNameProperty =
BindableProperty.Create(
nameof(TitleResourceName),
typeof(string),
typeof(LocalizedContentPage),
null,
BindingMode.Default,
propertyChanged: TitleResourceNameChanged);
public LocalizedContentPage()
{
WeakReferenceMessenger.Default.Register(this, (o, s) => UpdateText(this));
}
public string TitleResourceName
{
get => GetValue(TitleResourceNameProperty) as string;
set => SetValue(TitleResourceNameProperty, value);
}
public static void TitleResourceNameChanged(BindableObject bindable, object oldValue, object newValue)
{
var page = bindable as LocalizedContentPage;
page.TitleResourceName = (string)newValue;
SetPlaceHolder(page);
}
public static void SetPlaceHolder(LocalizedContentPage page)
{
page.Title = DynamicLocalizer.GetText(page.TitleResourceName);
}
public void UpdateText(LocalizedContentPage page)
{
page.Title = DynamicLocalizer.GetText(page.TitleResourceName);
}
public void Dispose()
{
WeakReferenceMessenger.Default.Unregister(this);
}
}
}
In the future, I hope to support more localized items such as images, but if you want to jump in the game, feel free to fork the repository too! Hopefully, this framework will make someone's life a little easier if the requirement of dynamic localization ever comes their way.