You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何利用DeepL .NET API实现待翻译文档的源语言自动检测

实现DeepL文档源语言自动检测的方法

使用DeepL .NET API开发时,要实现和官网一样的文档源语言自动检测功能,可通过以下方式完成:

核心方案

当调用文档翻译接口时,不指定源语言参数,DeepL API会自动检测文档的源语言,翻译完成后可从返回结果中获取检测到的语言信息。

代码实现示例

using DeepL;
using System.IO;
using System.Threading.Tasks;

public async Task DetectDocumentSourceLanguage()
{
    // 替换为你的DeepL API密钥
    var authKey = "YOUR_DEEPL_API_KEY";
    var client = new DeepLClient(authKey);

    var inputDoc = new FileInfo(@"你的待翻译文档路径");
    var outputDoc = new FileInfo(@"翻译结果输出路径");

    // 发起翻译请求,不设置源语言,启用自动检测
    var result = await client.TranslateDocumentAsync(
        inputDoc,
        outputDoc,
        targetLanguage: "目标语言代码(如EN/ZH)",
        sourceLanguage: null
    );

    // 获取检测到的源语言代码和名称
    var detectedLangCode = result.SourceLanguage;
    var detectedLangName = LanguageCode.GetLanguageName(detectedLangCode);
}

额外说明

  • 若仅需检测语言无需翻译文档,可先提取文档文本(需借助对应格式的文本处理库,如PDF用iTextSharp、Word用DocX),再调用DetectLanguageAsync方法检测文本语言。
  • 语言代码遵循DeepL官方规范,可通过LanguageCode.GetLanguageName将代码转换为直观的语言名称。

内容的提问来源于stack exchange,提问作者Kanchan Kumbhar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 01:03:27