GT ClassMethodsTranslation

downloadFileBatch

複数の翻訳ファイルを1回のリクエストでダウンロードするためのdownloadFileBatchメソッドのAPIリファレンス

概要

downloadFileBatch メソッドは、複数の翻訳ファイルを1回のバッチリクエストでダウンロードします。

const gt = new GT({ projectId: 'your-project-id', apiKey: 'your-api-key' });

const result = await gt.downloadFileBatch([
  'translation-id-1', 
  'translation-id-2', 
  'translation-id-3'
]);

バッチ効率: この方法は、複数のファイルを単一の API 呼び出しでまとめてダウンロードできるよう最適化されており、個別に downloadTranslatedFile を複数回呼び出す場合と比べて、ネットワークのオーバーヘッドを抑え、パフォーマンスを向上します。

参考

パラメータ

名称説明
fileIdsstring[]ダウンロード対象の翻訳ファイルIDの配列
options?DownloadFileBatchOptionsダウンロード要求のための任意の構成設定

DownloadFileBatchOptions

名前説明
timeout?numberリクエストのタイムアウト(ミリ秒)

戻り値

Promise<DownloadFileBatchResult> - ダウンロードしたファイルとメタデータを含みます。

type DownloadFileBatchResult = {
  files: File[];
  count: number;
}
プロパティ説明
filesFile[]ダウンロード済みファイルのオブジェクト配列
countnumber正常にダウンロードされたファイルの数

ファイル構造

type File = {
  id: string;
  fileName: string;
  data: string;
  metadata: any;
}
プロパティ説明
idstring翻訳ファイルのID
fileNamestring元のファイル名
datastringファイルの内容(Base64をデコードしたUTF-8文字列)
metadataany追加のファイルメタデータ

index.ts
// (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));
}

// (5) ファイルをダウンロード
const result = await gt.downloadFileBatch(translationIds);

注意

  • ファイルIDは有効な翻訳IDでなければなりません。まずは checkFileTranslations で完了を確認してください
  • 可能な場合、ファイルは要求したIDと同じ順序で返されます
  • バッチ内で一部のファイルのダウンロードに失敗しても、バッチ全体は失敗しません

次のステップ

このガイドはどうでしたか?

downloadFileBatch