SayWhat.Forms
1.0.403
dotnet add package SayWhat.Forms --version 1.0.403
NuGet\Install-Package SayWhat.Forms -Version 1.0.403
<PackageReference Include="SayWhat.Forms" Version="1.0.403" />
paket add SayWhat.Forms --version 1.0.403
#r "nuget: SayWhat.Forms, 1.0.403"
// Install SayWhat.Forms as a Cake Addin
#addin nuget:?package=SayWhat.Forms&version=1.0.403
// Install SayWhat.Forms as a Cake Tool
#tool nuget:?package=SayWhat.Forms&version=1.0.403
SayWhat.Forms
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).
Supported Controls
- 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
Usage
Click here For a full example and demo.
Initialization
var resourceManager = new ResourceManager("SayWhat.Forms.Demo.Resources.AppResources", Assembly.GetAssembly(typeof(Main Page))); Utilities.SayWhat.Settings.Initialize(resourceManager, CurrentCulture);
It is recommended to initialize in the app.xaml.cs or app.cs file as it is done here.
Xaml
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. </p>
<sw:LocalizedContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:sw="clr-namespace:SayWhat.Forms.Controls;assembly=SayWhat.Forms"
x:Class="SayWhat.Forms.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>
C#
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"}
}
}
}
}
}
Code Behind
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 SayWhat.Forms.Messages;
using SayWhat.Forms.Utilities;
using System;
using Xamarin.Forms;
namespace SayWhat.Forms.Controls { public class LocalizedLabel : Label, IDisposable { public static readonly BindableProperty TextResourceNameProperty = BindableProperty.Create( nameof(TextResourceName), typeof(string), typeof(LocalizedLabel), null, BindingMode.Default, propertyChanged: TextResourceNameChanged);
public LocalizedLabel()
{
MessagingCenter.Subscribe<object>(new object(), CultureChangedMessage.Message, (o) => UpdateText(this));
}
public string TextResourceName
{
get => GetValue(TextResourceNameProperty) as string;
set => SetValue(TextResourceNameProperty, value);
}
public static void TextResourceNameChanged(BindableObject bindable, object oldValue, object newValue)
{
LocalizedLabel label = (LocalizedLabel)bindable;
label.TextResourceName = (string) newValue;
SetText(label);
}
public static void SetText(LocalizedLabel label)
{
label.Text = DynamicLocalizer.GetText(label.TextResourceName);
}
public void UpdateText(LocalizedLabel label)
{
label.Text = DynamicLocalizer.GetText(label.TextResourceName);
}
public void Dispose()
{
MessagingCenter.Unsubscribe<object>(new object(), CultureChangedMessage.Message);
}
}
}
Future Updates
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.
When Maui is fully released, I look forward to updating the framework to accommodate it, if applicable.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Xamarin.Forms (>= 5.0.0.2401)
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.403 | 251 | 5/29/2022 |