The universal steps required to localize an app are:
- Create string Resource files with the convention:
- Resources/Strings/filename.resx
- Resources/Strings/filename.[additionalLanguage1].resx
- Resources/Strings/filename.[additionalLanguage2].resx
- Consume the resource strings in your app
- Optional set CultureInfo.CurrentUICulture and reload resource stings
This is a wrapper on Microsoft.Extensions.Localization
NuGet package.
Call UseToolkitMauiLocalization
in your MainProgram.cs
and nominate your default string resource:
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseToolkitMauiLocalization<AppStrings>()
public partial class MainPage : ContentPage
{
public LocalizationManager LM { get; }
public MainPage(LocalizationManager LM)
{
this.LM = LM;
}
}
<ContentPage xmlns:i18n="clr-namespace:Toolkit.Maui.Localization;assembly=Toolkit.Maui.Localization">
<!-- LBL_HELLO is a string resource containing "Hello, World!" -->
<Label Text="{i18n:Localize LBL_HELLO}"/>
<ContentPage.Resources>
<ResourceDictionary>
<i18n:PluralConverter x:Key="PluralConverter"/>
</ResourceDictionary>
</ContentPage.Resources>
<Button
x:Name="CounterBtn"
Text="{MultiBinding
{Binding Count},
{i18n:Localize STR_CLICKED_N_TIMES},
{i18n:Localize STR_CLICKED_1_TIME},
{i18n:Localize STR_CLICK_ME},
Converter={x:StaticResource PluralConverter}}"
SemanticProperties.Hint="Counts the number of times you click"
Clicked="OnCounterClicked"
HorizontalOptions="Center" />
<ContentPage.Resources>
<ResourceDictionary>
<i18n:StringFormatConverter x:Key="StringFormatConverter"/>
</ResourceDictionary>
</ContentPage.Resources>
<!-- STR_CONVERSE is a string resource that contains: {0} says: "{1}" -->
<!-- Name and Greeting are strings that contain "Jack" and "Hello" respectively. -->
<Label
Text="{MultiBinding
{Binding Name},
{Binding Greeting},
{i18n:Localize STR_CONVERSE},
Converter={x:StaticResource StringFormatConverter}}" />