Bitzsoft.Integrations.Translation.Abstractions 1.0.0

dotnet add package Bitzsoft.Integrations.Translation.Abstractions --version 1.0.0
                    
NuGet\Install-Package Bitzsoft.Integrations.Translation.Abstractions -Version 1.0.0
                    
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="Bitzsoft.Integrations.Translation.Abstractions" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bitzsoft.Integrations.Translation.Abstractions" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Bitzsoft.Integrations.Translation.Abstractions" />
                    
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 Bitzsoft.Integrations.Translation.Abstractions --version 1.0.0
                    
#r "nuget: Bitzsoft.Integrations.Translation.Abstractions, 1.0.0"
                    
#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 Bitzsoft.Integrations.Translation.Abstractions@1.0.0
                    
#: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=Bitzsoft.Integrations.Translation.Abstractions&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Bitzsoft.Integrations.Translation.Abstractions&version=1.0.0
                    
Install as a Cake Tool

Bitzsoft.Integrations.Translation.Abstractions

翻译服务抽象层 -- 统一传统机器翻译(阿里云 / 百度 / 腾讯云 / 有道 / DeepL / Google)与 AI 大模型翻译 (OpenAI 兼容网关 / Anthropic / 通义 Qwen-MT / 百度大模型 / 火山引擎大模型)的接口定义、结果模型、领域错误码与多供应商工厂。

本包仅含抽象层(接口、工厂、模型、异常、统一 Options 基类)。各厂商实现见独立子包,或一步到位的聚合包 Translation.All。

功能特性

  • 统一契约:一套 ITranslationProvider 接口覆盖单文本、批量与语言检测,结果携带检测语言与置信度
  • AI 扩展契约:IAiTranslationProvider : ITranslationProvider 增加风格 / 术语约束、温度、系统提示词模板
  • 领域词汇表注入点:TranslationTerm(原文=译文)随请求传递,原生支持术语干预的供应商严格采用
  • 统一语言编码:TranslationLanguages 把 zh-CN / ZH / Chinese 等别名收敛为统一短码,厂商包内部映射
  • 统一 Options 基类:TranslationProviderOptions 沉淀 BaseAddress / ApiKey / AppId+AppSecret / 超时 / 重试 通用配置
  • 标准错误码:TranslationErrors 收敛配额、鉴权失败、语言不支持、网络不可达等跨供应商错误语义
  • Provider 工厂:ITranslationProviderFactory 按编码(aliyun / deepl / openai ...)运行时解析,支撑按语言 / 用途路由
  • 健康探测:CheckHealthAsync 以最小代价(用量 / 语言列表 / 模型列表)探测供应商可用性

安装

dotnet add package Bitzsoft.Integrations.Translation.Abstractions

通常无需单独安装本包,安装任意一个厂商实现包或聚合包时会自动引入。

核心契约

ITranslationProvider

public interface ITranslationProvider
{
    // 供应商编码("aliyun" / "deepl" / "google" / "openai" ...)
    string Code { get; }

    // 单文本翻译(结果含检测语言与置信度)
    Task<TranslationResult<TranslationTextResult>> TranslateAsync(
        TranslationTextRequest request, CancellationToken ct = default);

    // 批量翻译(结果顺序与请求一一对应)
    Task<TranslationResult<TranslationBatchResult>> TranslateBatchAsync(
        TranslationBatchRequest request, CancellationToken ct = default);

    // 语言检测(供应商无检测接口时返回 NotSupported)
    Task<TranslationResult<TranslationDetectionResult>> DetectLanguageAsync(
        TranslationDetectRequest request, CancellationToken ct = default);

    // 健康探测(启动探活 / 路由决策)
    Task<TranslationHealthResult> CheckHealthAsync(CancellationToken ct = default);
}

IAiTranslationProvider

// 大模型翻译扩展:风格 / 术语约束、温度、系统提示词模板
public interface IAiTranslationProvider : ITranslationProvider
{
    Task<TranslationResult<TranslationTextResult>> TranslateAsync(
        AiTranslationRequest request, CancellationToken ct = default);
}

AiTranslationRequest 在基础请求之上增加 Style(译文风格)、Instructions(附加指令)、 SystemPromptTemplate(占位符模板)、Temperature(采样温度)与 MaxOutputTokens。 超长文本由 AI 实现包自动按长度上限分段并回拼,输出优先 JSON 模式解析、失败回退纯文本。

ITranslationProviderFactory

public interface ITranslationProviderFactory
{
    ITranslationProvider GetProvider(string code);   // 按编码解析
    IEnumerable<string> GetRegisteredCodes();        // 已注册的供应商编码
}

使用示例

// 注册任意厂商实现包后(如 AddBitzsoftDeepLTranslation),工厂按编码解析
public class TranslationService(ITranslationProviderFactory factory)
{
    public async Task<string> TranslateContractAsync(string text)
    {
        var provider = factory.GetProvider("deepl");

        var result = await provider.TranslateAsync(new TranslationTextRequest
        {
            Text = text,
            SourceLanguage = "auto",
            TargetLanguage = "zh",
            Terms = new[]
            {
                new TranslationTerm("force majeure", "不可抗力"),
                new TranslationTerm("consideration", "对价"),
            }
        });

        return result.IsSuccess
            ? result.Data!.TranslatedText
            : throw new InvalidOperationException($"{result.ErrorCode}: {result.ErrorMessage}");
    }
}

按语言 / 用途路由

// 中文为主的法律文书走 DeepL,站内消息走阿里云,批量走 Google 官方 API
var code = usage switch
{
    "legal" => "deepl",
    "message" => "aliyun",
    _ => "google",
};
var provider = factory.GetProvider(code);

核心类型一览

类型 说明
ITranslationProvider 翻译 Provider 标准契约(单文本 / 批量 / 检测 / 健康)
IAiTranslationProvider AI 大模型翻译扩展契约
ITranslationProviderFactory Provider 工厂(按编码解析)
TranslationResult<T> 结果包装(IsSuccess / Data / ErrorCode / ProviderErrorCode)
TranslationTextRequest / TranslationBatchRequest 翻译请求(语言 / 术语表 / 格式 / 领域提示)
AiTranslationRequest AI 翻译请求(风格 / 指令 / 提示词模板 / 温度)
TranslationTextResult / TranslationBatchResult 翻译结果(译文 / 检测语言 / 置信度)
TranslationDetectionResult 语言检测结果(语言 / 置信度 / 是否可靠)
TranslationTerm 领域术语条目(Source / Target)
TranslationLanguages 统一语言编码规范化(Normalize / IsAuto / ToEnglishName)
TranslationProviderOptions 厂商 Options 统一基类(端点 / 凭据 / 超时 / 重试)
TranslationErrors 统一错误码(配额 / 鉴权 / 语言不支持 / 网络不可达 ...)
TranslationException 翻译统一异常(继承 IntegrationException)
TranslationProviderCode 供应商编码常量(Aliyun / Baidu / Tencent / Youdao / DeepL / Google / OpenAI / Anthropic / Tongyi / BaiduAi / Volcengine)

供应商编码对照表

编码 实现类 所在包 协议
aliyun AliyunTranslationProvider Translation.Aliyun alimt POP RPC(HMAC-SHA1)
baidu BaiduTranslationProvider Translation.Baidu 通用翻译 API(MD5 签名)
tencent TencentTranslationProvider Translation.Tencent TMT TC3-HMAC-SHA256
youdao YoudaoTranslationProvider Translation.Youdao 智云文本翻译 v3(SHA-256 签名)
deepl DeepLTranslationProvider Translation.DeepL DeepL-Auth-Key REST
google GoogleTranslationProvider Translation.Google Cloud Translation v2 REST(官方计费 API)
openai OpenAiTranslationProvider Translation.AI.OpenAI OpenAI 兼容 /chat/completions(OpenAI / GLM / DeepSeek / Kimi / Qwen ...)
anthropic AnthropicTranslationProvider Translation.AI.Anthropic Claude Messages(复用既有 AI.Anthropic 适配)
tongyi TongyiTranslationProvider Translation.AI.Tongyi 百炼 Qwen-MT(原生术语干预 translation_options.terms)
baidu-ai BaiduAiTranslationProvider Translation.AI.Baidu 大模型文本翻译 ait/aiTextTranslate(翻译指令 reference + 术语库干预)
volcengine VolcengineTranslationProvider Translation.AI.Volcengine 机器翻译大模型 matx_translate(原生批量 + 术语直传,Header 鉴权)

与 AI 域抽象的关系

模型调用复用既有 Bitzsoft.Integrations.AI.Abstractions 的 IAIChatClientFactory / IChatClient 体系(密钥进凭据存储、租户感知);翻译特有语义(术语注入、 统一语言编码、分段回拼、JSON 输出回退、领域错误码)全部落在本抽象与翻译实现包内,两个抽象互不依赖。

相关包

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  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 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 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.  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 (12)

Showing the top 5 NuGet packages that depend on Bitzsoft.Integrations.Translation.Abstractions:

Package Downloads
Bitzsoft.Integrations.Translation.AI.Anthropic

Anthropic Claude 大模型翻译实现 — 复用既有 AI.Anthropic 原生 Messages 适配器,法律术语约束提示词 + 分段回拼 + JSON 输出回退,实现 IAiTranslationProvider 契约

Bitzsoft.Integrations.Translation.AI.OpenAI

OpenAI 兼容网关大模型翻译实现 — 通吃 OpenAI / 智谱 GLM / DeepSeek / Kimi / 通义千问 compatible 端点,复用 AI.Abstractions 的 IChatClient 体系,法律术语约束提示词 + 分段回拼 + JSON 输出回退

Bitzsoft.Integrations.Translation.AI.Baidu

百度大模型文本翻译实现 — 翻译指令(reference)注入风格与术语约束、控制台术语库干预(needIntervene)、llm/nmt 双引擎,实现 IAiTranslationProvider 契约

Bitzsoft.Integrations.Translation.All

翻译服务聚合包 — 传统机翻(阿里云 / 百度 / 腾讯云 / 有道 / DeepL / Google 官方)+ AI 翻译(通义 Qwen-MT / 百度大模型 / 火山引擎大模型,OpenAI 兼容 / Anthropic net8+),配置存在性驱动注册

Bitzsoft.Integrations.Translation.Tencent

腾讯云机器翻译(TMT)实现 — TextTranslate / TextTranslateBatch / TextDetect,TC3-HMAC-SHA256 签名,原生批量与独立语言检测

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 108 10/21/2026