# QQTranslator **Repository Path**: uanaoeng/QQTranslator ## Basic Information - **Project Name**: QQTranslator - **Description**: 一个PHP类,用于调用「腾讯翻译API」的「文本翻译接口」和「批量文本翻译接口」。 - **Primary Language**: PHP - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 2 - **Created**: 2020-05-26 - **Last Updated**: 2025-04-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 1. 简介 一个PHP类,用于调用「腾讯翻译API」的「文本翻译接口」和「批量文本翻译接口」。 ## 2. 使用方法 ### 2.1 一次翻译一个句子 ```php setSecretId("替换成你的 id"); $qqtr->setSecretKey("替换成你的 key"); // ----------------------------------------------------------------------------- // 选填参数(查看源码,了解更多设置项) // ----------------------------------------------------------------------------- // 设置项目ID。 // 默认为0,可以根据「控制台-账号中心-项目管理」中的配置填写, // $qqtr->setProjectId(0); // 根据实际情况修改 // 设置请求响应服务器所在地区。 // 默认为香港区域,根据自己需求改 // $qqtr->setRegion("ap-hongkong"); // 根据实际情况修改 // 标记不希望被翻译的文本内容,如句子中的特殊符号、人名、地名等; // 每次请求只支持配置一个不被翻译的单词; // 仅支持配置人名、地名等名词,不要配置动词或短语,否则会影响翻译结果。 // $qqtr->setUntranslatedText("word"); // 根据实际情况修改 // 设置「需要使用的术语库列表」 // 当前实测结果表明,如果指定的「术语库ID」无效,会选用「术语库ID列表」最下方的ID。 // $termRepoIdList = array(); // $termRepoIdList[] = "listIDOne"; // 根据实际情况修改 // $termRepoIdList[] = "listIDTwo"; // 根据实际情况修改 // $qqtr->setTermRepoIDList($termRepoIdList); // 需要使用的例句库列表 // 当前实测结果表明,如果指定的「例句库ID」无效,会选用「例句库ID列表」最下方的ID。 // $sentRepoIDList = array(); // $sentRepoIDList[] = "listIDOne"; // 根据实际情况修改 // $sentRepoIDList[] = "listIDTwo"; // 根据实际情况修改 // $qqtr->setSentRepoIDList($sentRepoIDList); // ----------------------------------------------------------------------------- // 指定要翻译的内容 // ----------------------------------------------------------------------------- $sourceText = 'hello'; // ----------------------------------------------------------------------------- // 获取释义 // ----------------------------------------------------------------------------- // 在下面的示例中,源语言是 en,目标语言是 zh。 $targetText = $qqtr->translateString($sourceText, 'en', 'zh'); // ----------------------------------------------------------------------------- // 打印结果 // ----------------------------------------------------------------------------- echo $targetText; ``` ### 2.2 一次翻译多个句子 ```php setSecretId("替换成你的 id"); $qqtr->setSecretKey("替换成你的 key"); // ----------------------------------------------------------------------------- // 选填参数(查看源码,了解更多设置项) // ----------------------------------------------------------------------------- // 设置项目ID。 // 默认为0,可以根据「控制台-账号中心-项目管理」中的配置填写, // $qqtr->setProjectId(0); // 根据实际情况修改 // // 设置请求响应服务器所在地区。 // // 默认为香港区域,根据自己需求改 // $qqtr->setRegion("ap-hongkong"); // 根据实际情况修改 // 设置「需要使用的术语库列表」 // 当前实测结果表明,如果指定的「术语库ID」无效,会选用「术语库ID列表」最下方的ID。 // $termRepoIdList = array(); // $termRepoIdList[] = "listIDOne"; // 根据实际情况修改 // $termRepoIdList[] = "listIDTwo"; // 根据实际情况修改 // $qqtr->setTermRepoIDList($termRepoIdList); // 设置「需要使用的例句库列表」 // 当前实测结果表明,如果指定的「例句库ID」无效,会选用「例句库ID列表」最下方的ID。 // $sentRepoIDList = array(); // $sentRepoIDList[] = "listIDOne"; // 根据实际情况修改 // $sentRepoIDList[] = "listIDTwo"; // 根据实际情况修改 // $qqtr->setSentRepoIDList($sentRepoIDList); // 注意:「批量文本翻译」暂时不支持指定「不希望被翻译的文本内容」。 // ----------------------------------------------------------------------------- // 指定要翻译的内容 // ----------------------------------------------------------------------------- $sourceTexts = array(); $sourceTexts[] = "Hello, world."; $sourceTexts[] = "Here we are."; $sourceTexts[] = "Good job!"; $sourceTexts[] = "That's all right."; $sourceTexts[] = "Best wish to you."; // ----------------------------------------------------------------------------- // 获取释义 // ----------------------------------------------------------------------------- // 在下面的示例中,源语言是 en,目标语言是 zh。 $targetTexts = $qqtr->translateStrings($sourceTexts, 'en', 'zh'); // ----------------------------------------------------------------------------- // 打印结果 // ----------------------------------------------------------------------------- // 获取释义失败时,返回的是字符串而不是数组 if (is_array($targetTexts)) { foreach ($targetTexts as $targetText) { echo $targetText; echo "
"; } } else { echo $targetTexts; } ``` ## 3. 其他 查看如何申请「腾讯翻译 API」: - https://cloud.tencent.com/document/product/551 查看完整语言列表: - https://cloud.tencent.com/document/api/551/15619 查看完整的请求响应服务器地域列表: - https://cloud.tencent.com/document/api/551/15615 ## 4. 项目地址 - https://gitee.com/uanaoeng/QQTranslator