GT ClassMethodsTranslation

downloadTranslatedFile

完了した翻訳ファイルをダウンロードするための downloadTranslatedFile メソッドの APIリファレンス

概要

downloadTranslatedFile メソッドは、1 つの翻訳済みファイルの内容を UTF-8 文字列としてダウンロードします。 このメソッドは、General Translation プラットフォームでの処理が完了した翻訳を取得するために使用します。

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

const content = await gt.downloadTranslatedFile({
  fileId: 'file-123',
  locale: 'es',
  versionId: 'version-456'
});

翻訳ステータス: このメソッドは、翻訳が完了している場合にのみ動作します。 ダウンロードする前に、まず checkFileTranslations を使って翻訳が完了していることを確認してください。

リファレンス

パラメータ

名称説明
fileFileInfoダウンロード対象のファイルを指定するファイル情報オブジェクト
options?DownloadFileOptionsダウンロード要求のための任意の設定

FileInfo 構造

名称説明
fileIdstringダウンロード対象のファイルを識別する一意のID
localestringダウンロードする翻訳の対象となるlocale
versionId?stringファイルの任意のversionId

DownloadFileOptions

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

戻り値

Promise<string> - 翻訳後のファイル内容(UTF-8 文字列)。

返される文字列には、元のソースファイルと同一の形式で、翻訳対象のテキストがすべてターゲットの locale に変換されたファイル内容が含まれます。


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 Promise.all(
  translationIds.map(id => gt.downloadTranslatedFile(id))
);

注意事項

  • ダウンロードしたファイルを UTF-8 の文字列として取得します
  • 指定した locale の翻訳が完了しているファイルである必要があります
  • ファイルが見つからない場合はエラーになります

次のステップ

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

downloadTranslatedFile