PutridParrot.Maui.Localization
1.0.0-alpha
dotnet add package PutridParrot.Maui.Localization --version 1.0.0-alpha
NuGet\Install-Package PutridParrot.Maui.Localization -Version 1.0.0-alpha
<PackageReference Include="PutridParrot.Maui.Localization" Version="1.0.0-alpha" />
paket add PutridParrot.Maui.Localization --version 1.0.0-alpha
#r "nuget: PutridParrot.Maui.Localization, 1.0.0-alpha"
// Install PutridParrot.Maui.Localization as a Cake Addin
#addin nuget:?package=PutridParrot.Maui.Localization&version=1.0.0-alpha&prerelease
// Install PutridParrot.Maui.Localization as a Cake Tool
#tool nuget:?package=PutridParrot.Maui.Localization&version=1.0.0-alpha&prerelease
PutridParrot.Maui.Localization
The aim of for this project is to supply helper functionality etc. for Maui to allow us to easily create localizable applications.
Package available via nuget
Credits
This package is heavily taken from the Stack Overflow discussion https://stackoverflow.com/questions/71315229/how-to-implement-net-maui-localization
Getting Started
Install the nuget package PutridParrot.Maui.Localization
In your MauiProgram.cs add the using
using Microsoft.Extensions.Logging;
Within the CreateMauiApp method add
builder.Services.AddLocalization();
Add a folder to Resources such as Resources/Strings
Add a new item, a Resource (with the .resx extension), be default I use AppRes.resx, but you can name it whatever you like. This will be our default language strings resource.
Add more new Resource (.resx extensions) for each of the languages you wish to support, they should be named as per your default resource file, so in my case AppRes then followed by the culture string (for example en-US, fr, de), so will look like this AppRes.de.resx for all German localizations.
To use these in code is as simple as
Debug.WriteLine(AppRes.HelloWorld);
where HelloWorld is a key within the AppRes.resx.
For XAML we use the the LocalizeExtension, however this expects a type which represents the AppRes (or whatever you named it type). So the simplest way to resolve this is create a new class named something like LocalizeAppResExtension which might look like this
internal class LocalizeAppResExtension : LocalizeExtension<AppRes> { }
Now within our XAML we use
<Label Text="{utils:LocalizeAppRes HelloWorld}" />
Changing your current culture
This is easy enough and worth doing when debugging your application as some languages have much longer strings for certain things that are pretty short in English (German, I'm looking at you my old friend).
We can do this pretty simply outselves, but this package includes the Localize class with a some code to do this for us, just use the SetCurrentlCulture static method
Localize.SetCurrentCulture("de);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-android33.0 is compatible. net7.0-ios was computed. net7.0-ios16.1 is compatible. net7.0-maccatalyst was computed. net7.0-maccatalyst16.1 is compatible. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net7.0-windows10.0.19041 is compatible. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- Microsoft.Extensions.Localization (>= 7.0.5)
-
net7.0-android33.0
- Microsoft.Extensions.Localization (>= 7.0.5)
-
net7.0-ios16.1
- Microsoft.Extensions.Localization (>= 7.0.5)
-
net7.0-maccatalyst16.1
- Microsoft.Extensions.Localization (>= 7.0.5)
-
net7.0-windows10.0.19041
- Microsoft.Extensions.Localization (>= 7.0.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0-alpha | 78 | 5/29/2023 |