WebDucer Translation library for Xamarin.Forms
Library with an Service to get and set the current application languge and to get the device language. It includes an XAML extension, to translate text in XAML with resources of the project (one or more resource).
States
Service | Last | Develop | Master |
---|---|---|---|
AppVeyor | |||
SonarQube coverage | |||
SonarQube technical debt | |||
SonarQube quality gate | |||
Nuget |
Services
- Abstractions:
IResourceManagersSource
- Interface for the resources source singleton to use in DIITranslationService
- Translation service to be used in view models over dependency injection
- Implementations
TranslationExtension
- XAML extension for translationsTranslationService
- Implementation for translation service interface (usesIResourceManagerSource
andIMultilingual
from Plugin.Multilingual)
Sample
Init ResourceManagersSource
in your App.xaml.cs
to be able to use this in XAML and code. Register as Singleton or Instance in your depency injection framework, if you use one. You can register as much of resource managers as you need. The first found translation will be taken. So you can override common library translation with your own, if you register your manager as first.
App.caml.cs
// Create Singleton (without DI)
protected override async void OnInitialized()
{
InitializeComponent();
var resourceSource = ResourceManagersSource.Init(
AppResources.ResourceManager,
CommonTranslations.ResourceManager); // Add your collection
mainPage = new NavigationPage(new MainPage());
}
// Register in DI
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterInstance(ResourceManagersSource.Init(
AppResources.ResourceManager,
CommonTranslations.ResourceManager)); // Add your collection of sources
containerRegistry.RegisterInstance(Plugin.Multilingual.CrossMultilingual.Current);
containerRegistry.RegisterSingleton<ITranslationService, TranslationService>();
containerRegistry.RegisterForNavigation<NavigationPage>();
containerRegistry.RegisterForNavigation<MainPage>();
}
Usage in XAML-Files
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:i18N="clr-namespace:WD.Translations;assembly=WD.Translations"
Title="{i18N:Translation PAGE_TITLE}">
<ContentPage.Content>
<Label Text="{i18N:Translation HELLO_WORLD}"/>
</ContentPage.Content>
</ContentPage>
Usage in ViewModels
public MyVieModel(ITranslationService translationService)
{
_translServ = translationService;
}
public string Title
{
get { return _translServ.GetTransalation("PAGE_TITLE"); }
}
public void SomeMethod()
{
// Translation e.g: "Value have to be between {0} and {1}."
var validator = new Validator
{
_translServ.GetFormattedTranslation("VALIDATION_ERROR_MESSAGE", 0, 500)
};
}