dotnetCampus.SourceLocalizations
0.1.0-alpha18
This is a prerelease version of dotnetCampus.SourceLocalizations.
dotnet add package dotnetCampus.SourceLocalizations --version 0.1.0-alpha18
NuGet\Install-Package dotnetCampus.SourceLocalizations -Version 0.1.0-alpha18
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="dotnetCampus.SourceLocalizations" Version="0.1.0-alpha18" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="dotnetCampus.SourceLocalizations" Version="0.1.0-alpha18" />
<PackageReference Include="dotnetCampus.SourceLocalizations" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add dotnetCampus.SourceLocalizations --version 0.1.0-alpha18
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: dotnetCampus.SourceLocalizations, 0.1.0-alpha18"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=dotnetCampus.SourceLocalizations&version=0.1.0-alpha18&prerelease
#tool nuget:?package=dotnetCampus.SourceLocalizations&version=0.1.0-alpha18&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
dotnetCampus.SourceLocalizations
Build | NuGet |
---|---|
dotnetCampus.SourceLocalizations is a source generator that can convert text localization files (e.g. .toml) into C# code and provide strong type support for localization keys.
Features
static void Main()
{
Console.WriteLine(LocalizedText.Current.App.Title); // "Hello, World!"
Console.WriteLine(LocalizedText.Current.App.Description); // "This is a sample application."
Console.WriteLine(LocalizedText.Current.Cli.Usage); // "Usage: dotnetCampus.SourceLocalizations [options]"
Console.WriteLine(LocalizedText.Current.PressAnyKeyToExit); // "Press any key to exit..."
}
- Source Generators
- Generate C# codes
- Generate properties for implementation types (so that reflections on types can get localized properties which is very important for WPF Bindings)
- Generate localized types for each language item which contains more than one arguments (This fixes different argument orders among different languages.)
- File formats
- TOML
- YAML
🤡 Might be deprecated in the future.
- UI Frameworks Support
- Avalonia
😉 We look forward to your better suggestions.
- MAUI
😶🌫️ Not tested yet
- Uno Platform
😉 We look forward to your better suggestions.
- Wpf
😉 We look forward to your better suggestions.
- Avalonia
- Diagnostics Analyzers and Code Fixes
- Detect (and generate) missing localization keys
- Detect (and remove) unused localization keys
- Detect arguments mismatch among localized texts (e.g.
Hello, {name:string}
in en butこんにちは、{errorCode:int}
in ja) - Detect invalid IETF language tags and report errors
Installation
dotnet add package dotnetCampus.SourceLocalizations
Usage
1. Create localization files
// Localizations/en.toml
App.Title = "Hello, World!"
App.Description = "This is a sample application."
Cli.Usage = "Usage: dotnetCampus.SourceLocalizations [options]"
PressAnyKeyToExit = "Press any key to exit..."
// Localizations/zh-hans.toml
App.Title = "你好,世界!"
App.Description = "这是一个示例应用程序。"
Cli.Usage = "用法:dotnetCampus.SourceLocalizations [选项]"
PressAnyKeyToExit = "按任意键退出..."
The file name must conform to the IETF BCP 47 standard.
2. Write a localization class
// LocalizedText.cs
using dotnetCampus.SourceLocalizations;
namespace SampleApp;
// The default language is used to generate localization interfaces, so it must be the most complete one.
// The current language is optional. If not specified, the current OS UI language will be used.
// The notification is optional. If true, when the current language changes, the UI will be notified to update the localization text.
[LocalizedConfiguration(Default = "en", Current = "zh-hans", SupportsNotification = false)]
public partial class LocalizedText;
3. Use the generated code
Console, library or any other UI framework:
// Program.cs
static void Main()
{
Console.WriteLine(LocalizedText.Current.App.Title); // "Hello, World!"
Console.WriteLine(LocalizedText.Current.App.Description); // "This is a sample application."
Console.WriteLine(LocalizedText.Current.Cli.Usage); // "Usage: dotnetCampus.SourceLocalizations [options]"
Console.WriteLine(LocalizedText.Current.PressAnyKeyToExit); // "Press any key to exit..."
}
Avalonia:
<TextBlock Text="{Binding App.Title, Source={x:Static l:LocalizedText.Current}}" />
<TextBlock Text="{Binding App.Description, Source={x:Static l:LocalizedText.Current}}" />
WPF:
<TextBlock Text="{Binding App.Title, Source={x:Static l:LocalizedText.Current}, Mode=OneWay}" />
<TextBlock Text="{Binding App.Description, Source={x:Static l:LocalizedText.Current}, Mode=OneWay}" />
Uno Platform:
<TextBlock Text="{x:Bind l:Lang.Current.App.Title}" />
<TextBlock Text="{x:Bind l:Lang.Current.App.Description}" />
// Uno Platform MainPage.xaml.cs
using dotnetCampus.Localizations;
namespace dotnetCampus.SampleUnoApp;
public sealed partial class MainPage : Page
{
public MainPage() => InitializeComponent();
// IMPORTANT: The Lang property must be public.
public ILocalizedValues Lang => global::dotnetCampus.SampleUnoApp.Localizations.LocalizedText.Current;
}
Advanced Usage
If you want to add real-time language switching support, you can modify the LocalizedText
class as follows:
[LocalizedConfiguration(Default = "en-US", SupportsNotification = true)]
public static partial class LocalizedText
{
public static AppBuilder UseCompiledLang(this AppBuilder appBuilder)
{
if (OperatingSystem.IsWindows())
{
var language = GetUserProfileLanguage() ?? CultureInfo.CurrentUICulture.Name;
_ = SetCurrent(language);
SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;
}
else
{
// On other operating systems, the current language is automatically set to the current UI culture.
}
return appBuilder;
}
[SupportedOSPlatform("windows")]
private static void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
{
if (e.Category is UserPreferenceCategory.Locale)
{
Dispatcher.UIThread.InvokeAsync(async () =>
{
var language = GetUserProfileLanguage();
if (language is not null)
{
await SetCurrent(language);
}
}, DispatcherPriority.Background);
}
}
[SupportedOSPlatform("windows")]
private static string? GetUserProfileLanguage()
{
// Retrieve the current language settings from the registry.
//
// Compared to CultureInfo.CurrentUICulture.Name or Win32 API's GetUserDefaultUILanguage, the registry can get updated standard language tags,
// and supports user-defined language preferences without needing to log off.
// Note: Even restarting the application will get the old settings; only logging off the system will get the new ones.
var languageNames = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64)
.OpenSubKey(@"Control Panel\International\User Profile", false)?
.GetValue("Languages", null) as IReadOnlyList<string>;
return languageNames?.FirstOrDefault();
}
}
Then, you can use the UseCompiledLang
method in your App.xaml.cs
file:
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.UseCompiledLang()
.XxxOthers()
;
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. net9.0 was computed. 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. |
.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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- System.Collections.Immutable (>= 9.0.3)
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 |
---|---|---|
0.1.0-alpha18 | 0 | 4/25/2025 |
0.1.0-alpha17 | 0 | 4/25/2025 |
0.1.0-alpha16 | 40 | 4/22/2025 |
0.1.0-alpha15 | 122 | 3/31/2025 |
0.1.0-alpha14 | 121 | 3/31/2025 |
0.1.0-alpha13 | 97 | 3/28/2025 |
0.1.0-alpha12 | 94 | 3/27/2025 |
0.1.0-alpha11 | 434 | 3/26/2025 |
0.1.0-alpha10 | 446 | 3/25/2025 |
0.1.0-alpha09 | 127 | 3/20/2025 |
0.1.0-alpha08 | 115 | 3/20/2025 |
0.1.0-alpha07 | 122 | 3/19/2025 |
0.1.0-alpha06 | 119 | 3/18/2025 |
0.1.0-alpha05 | 72 | 11/8/2024 |
0.1.0-alpha04 | 90 | 7/3/2024 |
0.1.0-alpha03 | 150 | 6/7/2024 |
0.1.0-alpha02 | 72 | 6/7/2024 |
0.1.0-alpha01 | 70 | 5/28/2024 |