Mtf.MAUI.Blazor.LanguageService
1.0.43
dotnet add package Mtf.MAUI.Blazor.LanguageService --version 1.0.43
NuGet\Install-Package Mtf.MAUI.Blazor.LanguageService -Version 1.0.43
<PackageReference Include="Mtf.MAUI.Blazor.LanguageService" Version="1.0.43" />
<PackageVersion Include="Mtf.MAUI.Blazor.LanguageService" Version="1.0.43" />
<PackageReference Include="Mtf.MAUI.Blazor.LanguageService" />
paket add Mtf.MAUI.Blazor.LanguageService --version 1.0.43
#r "nuget: Mtf.MAUI.Blazor.LanguageService, 1.0.43"
#:package Mtf.MAUI.Blazor.LanguageService@1.0.43
#addin nuget:?package=Mtf.MAUI.Blazor.LanguageService&version=1.0.43
#tool nuget:?package=Mtf.MAUI.Blazor.LanguageService&version=1.0.43
Translator Class Documentation
The Translator class provides translation support for Blazor components derived from ComponentBase. It integrates with the Lng class to translate string properties of a component dynamically at runtime and optionally restore their original values later.
Namespace
Mtf.LanguageService.MAUI.Blazor
Methods
Translate(ComponentBase component)
Translates all writable string properties of a Blazor component.
Parameters:
component(ComponentBase): The component whose properties should be translated.
Returns:
Dictionary<object, string>: A collection containing the original values before translation.
Exceptions:
ArgumentNullException: Thrown whencomponentisnull.
Description:
- Uses reflection to inspect all instance properties of the component.
- Searches for writable string properties.
- Translates property values through
Lng.Elem. - Stores original values so they can be restored later with
SetOriginalTexts.
SetOriginalTexts(Dictionary<object, string> originalTexts)
Restores original values previously captured by Translate.
Parameters:
originalTexts(Dictionary<object, string>): Dictionary returned by a previous call toTranslate.
Exceptions:
ArgumentNullException: Thrown whenoriginalTextsisnull.
Description:
- Restores translated property values to their original state.
- Uses the internally stored property mapping to determine which property should be restored.
- Supports fallback restoration for common text-related properties.
Features
Reflection-Based Translation
The translator automatically discovers string properties without requiring explicit configuration.
Supported property examples:
TextTitleHeaderPlaceholderLabelContentCaptionDescriptionHeaderTextLabelTextButtonTextTitleText
Translation Integration
All translations are performed through:
Lng.Elem(text);
This allows the translator to work with any language provider supported by the LanguageService library.
Original Value Tracking
The translator stores original values before replacing them.
This enables:
var originals = Translator.Translate(this);
// ...
Translator.SetOriginalTexts(originals);
Safe Execution
The implementation includes:
- Null checking
- Reflection error handling
- Property validation
- Type checking
This prevents translation failures from breaking component rendering.
Usage Example
Basic Translation
using Microsoft.AspNetCore.Components;
using Mtf.LanguageService.MAUI.Blazor;
public partial class EmployeeList : ComponentBase
{
private string Title { get; set; } = "Employees";
private string DeleteMessage { get; set; } = "Are you sure you want to delete this employee?";
protected override void OnInitialized()
{
Translator.Translate(this);
}
}
Restoring Original Values
private Dictionary<object, string> originals;
protected override void OnInitialized()
{
originals = Translator.Translate(this);
}
private void Restore()
{
Translator.SetOriginalTexts(originals);
}
Switching Language
Lng.DefaultLanguage = Language.Hungarian;
Translator.Translate(this);
Direct Translation
var translated = Lng.Translate(
Language.Hungarian,
"Alkalmazottak",
Language.English);
Notes
Component Limitations
Blazor components do not expose a visual tree in the same way as MAUI or WPF controls.
Because of this limitation:
- Component fields and properties can be translated.
- Razor markup content cannot be automatically discovered and translated.
- Static HTML content must use explicit translation calls.
Example:
<h3>@Lng.Elem("Employees")</h3>
<button>
@Lng.Elem("Add New Employee")
</button>
Reflection Scope
The translator processes:
- Public properties
- Protected properties
- Private properties
Only writable string properties are considered.
Performance
Translation occurs only when Translate is invoked.
Since reflection is used, it is recommended to perform translation during component initialization rather than during frequent rendering operations.
Extendability
Additional property names can be supported by extending the CommonProperties collection.
Custom translation rules can also be implemented by modifying the reflection logic.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0-android36.0 is compatible. net10.0-ios26.0 is compatible. net10.0-maccatalyst26.0 is compatible. net10.0-windows10.0.19041 is compatible. |
-
net10.0-android36.0
- Microsoft.AspNetCore.Components (>= 10.0.10)
- Microsoft.Maui.Controls (>= 10.0.90)
-
net10.0-ios26.0
- Microsoft.AspNetCore.Components (>= 10.0.10)
- Microsoft.Maui.Controls (>= 10.0.90)
-
net10.0-maccatalyst26.0
- Microsoft.AspNetCore.Components (>= 10.0.10)
- Microsoft.Maui.Controls (>= 10.0.90)
-
net10.0-windows10.0.19041
- Microsoft.AspNetCore.Components (>= 10.0.10)
- Microsoft.Maui.Controls (>= 10.0.90)
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.43 | 132 | 8/16/2026 |