GT ClassMethodsTranslation
checkFileTranslations
checkFileTranslations 方法的 API 参考,用于在无需下载文件的情况下检查翻译状态
概览
checkFileTranslations 方法无需下载文件内容即可检查 files 的翻译状态。
这对于监控翻译进度、查看完成情况以及确定哪些翻译已可用非常有用。
const gt = new GT({ projectId: 'your-project-id', apiKey: 'your-api-key' });
const status = await gt.checkFileTranslations([
{
versionId: 'version-123',
fileName: 'app.json',
locale: 'es'
}
]);参考资料
参数
| 名称 | 类型 | 描述 |
|---|---|---|
data | FileTranslationQuery[] | 要检查的文件翻译查询的数组 |
options? | CheckFileTranslationsOptions | 请求的可选配置 |
FileTranslationQuery
| 名称 | 类型 | 描述 |
|---|---|---|
versionId | string | 要检查的文件版本 ID |
fileName? | string | 文件名标识符(未提供 fileId 时必填) |
fileId? | string | 文件 ID 标识符(未提供 fileName 时必填) |
locale | string | 要检查翻译状态的目标 locale |
CheckFileTranslationsOptions
| 名称 | 类型 | 描述 |
|---|---|---|
timeout? | number | 请求超时时间(毫秒) |
返回
Promise<CheckFileTranslationsResult> - 包含翻译状态信息。
type CheckFileTranslationsResult = {
translations: CompletedFileTranslationData[];
}| 属性 | 类型 | 描述 |
|---|---|---|
translations | CompletedFileTranslationData[] | 翻译状态数据数组 |
CompletedFileTranslationData 结构体
type CompletedFileTranslationData = {
id: string;
fileId: string;
versionId: string;
fileName: string;
locale: string;
status: 'completed' | 'pending' | 'failed';
completedAt: string | null;
approvedAt: string | null;
publishedAt: string | null;
createdAt: string;
updatedAt: string;
}示例
基本用法
检查特定 files 和 locales 的翻译状态:
// (1) 创建一个 GT 实例
const targetLocales = ['es', 'fr', 'de'];
const gt = new GT({
projectId: 'your-project-id',
apiKey: 'your-api-key',
});
// (2) 上传文件
const fileUpload = {
content: fileContents,
fileName: filePath,
fileFormat: 'JSON',
locale: 'en',
};
const files = [ { source: fileUpload } ];
const { uploadedFiles } = await gt.uploadSourceFiles(
files,
{ sourceLocale: 'en' }
);
// (3) 将文件翻译任务加入队列
const enqueueResult = await gt.enqueueFiles(
uploadedFiles,
{
sourceLocale: 'en',
targetLocales: targetLocales,
});
// (4) 等待所有翻译完成
let translationIds = [];
const statusQuery = Object.values(enqueueResult.data).flatMap(({fileName, versionId}) => {
return targetLocales.map((locale) => ({locale, fileName, versionId}));
});
while (true) {
const status = await gt.checkFileTranslations(statusQuery);
if (status.translations.length === statusQuery.length) {
translationIds = status.translations.map(translation => translation.id);
break;
}
await new Promise(resolve => setTimeout(resolve, 1000));
}注意事项
- 翻译状态包括
pending、completed和failed - 已完成的翻译在发布前可能仍需审批
- 监控多个翻译任务时,使用此方法可高效进行状态检查
后续步骤
- 查看快速上手,了解如何使用此方法的完整示例
- 查看
downloadTranslatedFile以下载已完成的翻译 - 查看
enqueueFiles以创建翻译任务 - 查看
querySourceFile以获取源文件和翻译信息
本指南如何?