benxu.AppPlatform.Localization 3.0.2

dotnet add package benxu.AppPlatform.Localization --version 3.0.2
                    
NuGet\Install-Package benxu.AppPlatform.Localization -Version 3.0.2
                    
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="benxu.AppPlatform.Localization" Version="3.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="benxu.AppPlatform.Localization" Version="3.0.2" />
                    
Directory.Packages.props
<PackageReference Include="benxu.AppPlatform.Localization" />
                    
Project file
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 benxu.AppPlatform.Localization --version 3.0.2
                    
#r "nuget: benxu.AppPlatform.Localization, 3.0.2"
                    
#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.
#:package benxu.AppPlatform.Localization@3.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=benxu.AppPlatform.Localization&version=3.0.2
                    
Install as a Cake Addin
#tool nuget:?package=benxu.AppPlatform.Localization&version=3.0.2
                    
Install as a Cake Tool

benxu.AppPlatform.Localization

本地化套件 - 提供多語系資源管理和語系切換功能。

特色

  • JSON 格式語系資源,易於編輯和管理
  • 即時語系切換,支援 Blazor 元件自動更新
  • 使用者偏好持久化,重啟應用程式後保留選擇
  • 可擴充的資源提供者架構
  • 內建繁體中文和英文預設翻譯
  • Blazor Cascading 元件支援

安裝

dotnet add package benxu.AppPlatform.Localization

使用範例

基本使用

using benxu.AppPlatform.MAUI.Bootstrap.Extensions;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();

        builder.UseAppPlatform(options =>
        {
            options.UseLocalization(loc =>
            {
                loc.DefaultCulture = "zh-TW";
                loc.FallbackCulture = "en-US";
                loc.SupportedCultures = new List<string> { "zh-TW", "en-US" };
                loc.PersistUserPreference = true;
            });
        });

        return builder.Build();
    }
}

Blazor 頁面中使用

@using benxu.AppPlatform.Core.Abstractions
@inject ILocalizationService L

<h1>@L["common.loading"]</h1>
<p>@L.GetString("validation.passwordTooShort", 8)</p>

<button @onclick="SwitchToEnglish">English</button>

@code {
    private async Task SwitchToEnglish()
    {
        await L.SetCultureAsync("en-US");
    }
}

新增應用程式自訂翻譯

  1. 在應用程式中建立語系資源檔案:
Resources/Localization/zh-TW.json
Resources/Localization/en-US.json
  1. 設定為內嵌資源:
<ItemGroup>
  <EmbeddedResource Include="Resources\Localization\*.json" />
</ItemGroup>
  1. 註冊資源:
builder.Services.AddLocalizationResources(
    typeof(MauiProgram).Assembly,
    "YourApp.Resources.Localization");

配置選項

選項 預設值 說明
DefaultCulture "zh-TW" 預設語系
FallbackCulture "en-US" 備援語系,當找不到翻譯時使用
SupportedCultures ["zh-TW", "en-US"] 支援的語系清單
UseSystemCulture false 是否使用系統語系作為預設值
PersistUserPreference true 是否持久化使用者選擇的語系
EnableDebugMode false 是否顯示缺失的翻譯鍵值

授權

MIT License - Copyright (c) 2025 benxu

Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on benxu.AppPlatform.Localization:

Package Downloads
benxu.AppPlatform.MAUI.Bootstrap

Bootstrap package for benxu App Platform. Provides fluent API for one-line service registration and lifecycle management.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.2 82 1/17/2026