Lang.Avalonia
12.0.2
See the version list below for details.
dotnet add package Lang.Avalonia --version 12.0.2
NuGet\Install-Package Lang.Avalonia -Version 12.0.2
<PackageReference Include="Lang.Avalonia" Version="12.0.2" />
<PackageVersion Include="Lang.Avalonia" Version="12.0.2" />
<PackageReference Include="Lang.Avalonia" />
paket add Lang.Avalonia --version 12.0.2
#r "nuget: Lang.Avalonia, 12.0.2"
#:package Lang.Avalonia@12.0.2
#addin nuget:?package=Lang.Avalonia&version=12.0.2
#tool nuget:?package=Lang.Avalonia&version=12.0.2
Lang.Avalonia
简体中文 | English
Lang.Avalonia is a plugin-based localization library for Avalonia UI. The core package provides the XAML markup extension, binding pipeline, converters, I18nManager, and the ILangPlugin contract. Resource loading is provided by JSON, XML, and RESX plugins, with an optional source generator for type-safe resource keys.
| Package | NuGet | Downloads |
|---|---|---|
| Lang.Avalonia | ||
| Lang.Avalonia.Json | ||
| Lang.Avalonia.Xml | ||
| Lang.Avalonia.Resx | ||
| Lang.Avalonia.Analysis |
Features
- Unified XAML and C# localization entry points.
- Runtime language switching through
I18nManager.Instance.Culture. - JSON, XML, and RESX resource providers behind the same
ILangPlugincontract. - Default-culture fallback and original-key fallback.
- Type-safe resource key generation with T4 templates or
Lang.Avalonia.Analysis. - Support for fixed-culture preview bindings, formatted strings, and dynamic binding arguments.
Package Selection
| Resource format | Packages | Typical use |
|---|---|---|
| JSON | Lang.Avalonia + Lang.Avalonia.Json |
Editable files, cross-platform tooling, source-generator demos |
| XML | Lang.Avalonia + Lang.Avalonia.Xml |
Structured language files with nested nodes |
| RESX | Lang.Avalonia + Lang.Avalonia.Resx |
.NET ResourceManager and satellite assemblies |
| Type-safe keys | Lang.Avalonia.Analysis |
Compile-time constants from AdditionalFiles |
Quick Start
Install the core package and one provider:
dotnet add package Lang.Avalonia
dotnet add package Lang.Avalonia.Json
Create language files under I18n and copy JSON/XML files to the output directory:
<ItemGroup>
<None Update="I18n\*.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
Example JSON resource:
{
"language": "English",
"description": "English resources",
"cultureName": "en-US",
"Localization": {
"Main": {
"MainView": {
"Title": "Lang.Avalonia localization workspace",
"ChangeLanguage": "Language"
}
}
}
}
Register the plugin during app startup:
using Lang.Avalonia;
using Lang.Avalonia.Json;
using System.Globalization;
I18nManager.Instance.Register(new JsonLangPlugin(), new CultureInfo("en-US"), out var error);
if (!string.IsNullOrWhiteSpace(error))
{
// Log or show the initialization error.
}
Use the generated constants in AXAML:
xmlns:c="https://codewf.com"
xmlns:mainLangs="clr-namespace:Localization.Main"
<SelectableTextBlock Text="{c:I18n {x:Static mainLangs:MainView.Title}}" />
<SelectableTextBlock Text="{c:I18n {x:Static mainLangs:MainView.Title}, CultureName=ja-JP}" />
Use the same keys from C#:
var title = I18nManager.Instance.GetResource(Localization.Main.MainView.Title);
var englishTitle = I18nManager.Instance.GetResource(Localization.Main.MainView.Title, "en-US");
Switch language at runtime:
I18nManager.Instance.Culture = new CultureInfo("zh-CN");
Type-Safe Keys
Two generation paths are supported:
- Demo T4 templates generate
I18n/Language.csfrom JSON, XML, or RESX resources. Lang.Avalonia.AnalysisgeneratesLanguage.g.csat compile time fromAdditionalFiles.
Source generator example:
<ItemGroup>
<PackageReference Include="Lang.Avalonia.Analysis" Version="*" PrivateAssets="all" />
<AdditionalFiles Include="I18n\*.json" />
</ItemGroup>
Generated field values preserve the original resource keys. Only C# identifier names are sanitized.
Demos And Design Notes
The repository contains four demos:
| Demo | Purpose |
|---|---|
Lang.Avalonia.Json.Demo |
JSON files copied to output and loaded by JsonLangPlugin |
Lang.Avalonia.Xml.Demo |
XML files copied to output and loaded by XmlLangPlugin |
Lang.Avalonia.Resx.Demo |
RESX resources loaded through ResourceManager |
Lang.Avalonia.Analysis.Demo |
JSON resources plus source-generated keys |
Design documentation and SVG diagrams are available in docs/design.en-US.md.
Fallback Rules
Resource lookup follows this order:
- Explicit
CultureNamewhen provided; otherwiseI18nManager.Instance.Culture. - The default culture passed to
Register. - The original resource key.
Notes
- JSON and XML providers scan
AppDomain.CurrentDomain.BaseDirectoryby default. - JSON and XML providers can also read embedded resources through
AddResource. - RESX provider discovers generated resource designer types by
ResourceManager; the default type filter mark isi18n. - Dynamic Avalonia bindings are supported for keys and format arguments.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net11.0 is compatible. |
NuGet packages (4)
Showing the top 4 NuGet packages that depend on Lang.Avalonia:
| Package | Downloads |
|---|---|
|
Shinya.Avalonia
Shinya.Framework |
|
|
Lang.Avalonia.Xml
Avalonia UI 国际化解决方案的 XML 插件,提供 XML 格式的多语言资源加载支持 |
|
|
Lang.Avalonia.Json
Avalonia UI 国际化解决方案的 Json 插件,提供 Json 格式的多语言资源加载支持 |
|
|
Lang.Avalonia.Resx
Avalonia UI 国际化解决方案的 Resx 插件,提供 Resx 格式的多语言资源加载支持 |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on Lang.Avalonia:
| Repository | Stars |
|---|---|
|
MaaXYZ/MFAAvalonia
基于 Avalonia 的 MAAFramework 通用 GUI 项目 | A universal GUI project for MAAFramework based on Avalonia
|