# General Translation Platform: uploadTranslations
URL: https://generaltranslation.com/ja/docs/platform/core/reference/gt-class-methods/translation/upload-translations.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: ソースファイルに対応する既存の翻訳済みファイルをアップロードします。uploadTranslations の API リファレンス。

すでにプロジェクトにあるソースファイルの既存の翻訳をアップロードします。翻訳サービスで生成するのではなく、既存の翻訳を移行する場合や人手でレビュー済みの翻訳をアップロードする場合に使用します。

## 概要 [#overview]

翻訳アップロードの配列とオプションオブジェクトを指定して、`uploadTranslations` を呼び出します。各アップロードには、アップロード済みのソースファイルを特定するために使用される完全なソースコンテンツと、1 つ以上の翻訳済みファイルが含まれます。

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

const result = await gt.uploadTranslations(files, {
  sourceLocale: 'en',
});
```

シグネチャ:

```typescript
uploadTranslations(
  files: { source: FileUpload; translations: FileUpload[] }[],
  options: UploadFilesOptions
): Promise<UploadFilesResponse>
```

*注: `uploadTranslations` を使用するには、GT インスタンスに `apiKey` (または `devApiKey`) と `projectId` が設定されている必要があります。*

## 仕組み [#how-it-works]

* **既存のソース。** まずソースをアップロードします。`source`オブジェクトは、コンテンツとロケールを含む完全な`FileUpload`のままです。APIはそのデータから不足しているIDを導出し、得られたIDを使って既存のソースバージョンを検索します。
* **翻訳。** `translations`配列の各項目には、コンテンツと対象ロケールを含める必要があります。
* **ファイルエンコーディング。** テキストコンテンツは自動的にBase64エンコードされます。バイナリの`LOTTIE`翻訳には、あらかじめBase64エンコードされた`.lottie` ZIPデータが含まれている必要があります。
* **バージョニング。** 返されるファイル参照には、ブランチ対応のバージョニングに使用する`branchId`が含まれます。

## パラメーター [#parameters]

| パラメーター                | 説明                   | 型                                                      | 任意  | デフォルト |
| --------------------- | -------------------- | ------------------------------------------------------ | --- | ----- |
| [`files`](#files)     | 各ソースファイルとその翻訳からなる配列。 | `{ source: FileUpload; translations: FileUpload[] }[]` | いいえ | —     |
| [`options`](#options) | アップロードの設定オプション。      | `UploadFilesOptions`                                   | いいえ | —     |

### `files`

**Type** `{ source: FileUpload; translations: FileUpload[] }[]` · **必須**

各エントリは、完全なソースファイルとその翻訳済みファイルを対応付けます。

```typescript
{
  source: FileUpload; // ソースコンテンツとメタデータ
  translations: FileUpload[]; // コンテンツを含む翻訳済みファイル
}
```

`source` の値では次の `FileUpload` フィールドを使用します:

| フィールド                | 説明                                                              | 型                                                               | 任意  |
| -------------------- | --------------------------------------------------------------- | --------------------------------------------------------------- | --- |
| `content`            | ソーステキストの生データ、または `LOTTIE` の場合は base64 エンコードされたバイナリコンテンツ。        | `string`                                                        | いいえ |
| `fileName`           | 以前にアップロードしたソースファイルと一致するファイル名。                                   | `string`                                                        | いいえ |
| `fileFormat`         | ファイルの形式。                                                        | [`FileFormat`](/docs/platform/core/reference/types/file-format) | いいえ |
| `locale`             | ソースコンテンツのロケール。                                                  | `string`                                                        | いいえ |
| `dataFormat`         | ソースファイル内のデータ形式 (`ICU`、`I18NEXT`、`JSX`、`STRING`) 。               | [`DataFormat`](/docs/platform/core/reference/types/data-format) | はい  |
| `formatMetadata`     | ソース記述子とともに受け付けられる形式固有のメタデータ。既存のソースレコードは変更されません。                 | `GTJsonFormatMetadata \| FormatMetadata`                        | はい  |
| `branchId`           | 以前にアップロードしたソースを含むブランチ。省略した場合はデフォルトのブランチが使用されます。                 | `string`                                                        | はい  |
| `fileId`             | ソースファイルのファイル ID。                                                | `string`                                                        | はい  |
| `versionId`          | ソースファイルのバージョン ID。                                               | `string`                                                        | はい  |
| `transformFormat`    | `FileUpload` で受け付けられ、ローカルで検証されますが、このアップロード用 endpoint では使用されません。 | [`FileFormat`](/docs/platform/core/reference/types/file-format) | はい  |
| `incomingBranchId`   | `FileUpload` で受け付けられますが、このメソッドでは送信されません。                        | `string`                                                        | はい  |
| `checkedOutBranchId` | `FileUpload` で受け付けられますが、このメソッドでは送信されません。                        | `string`                                                        | はい  |

各翻訳 (`FileUpload`) では次のフィールドを使用します:

| フィールド                | 説明                                                                | 型                                                               | 任意  |
| -------------------- | ----------------------------------------------------------------- | --------------------------------------------------------------- | --- |
| `content`            | 翻訳テキストの生データ、または `LOTTIE` の場合は base64 エンコードされたバイナリコンテンツ。           | `string`                                                        | いいえ |
| `fileName`           | `FileUpload` では必須ですが、保存される翻訳にはソースファイルのファイル名が使用されます。               | `string`                                                        | いいえ |
| `fileFormat`         | ファイルの形式。                                                          | [`FileFormat`](/docs/platform/core/reference/types/file-format) | いいえ |
| `locale`             | 翻訳の対象ロケール。                                                        | `string`                                                        | いいえ |
| `dataFormat`         | 翻訳データの形式 (`ICU`、`I18NEXT`、`JSX`、`STRING`) 。                       | [`DataFormat`](/docs/platform/core/reference/types/data-format) | はい  |
| `fileId`             | クライアントで受け付けられ送信されますが、endpoint では無視されます。翻訳はソースファイルのファイル ID を継承します。 | `string`                                                        | はい  |
| `versionId`          | クライアントで受け付けられ送信されますが、endpoint では無視されます。翻訳はソースのバージョン ID を継承します。    | `string`                                                        | はい  |
| `branchId`           | クライアントで受け付けられ送信されますが、endpoint では無視されます。翻訳はソースのブランチを継承します。         | `string`                                                        | はい  |
| `transformFormat`    | `FileUpload` で受け付けられますが、このメソッドでは送信されません。                          | [`FileFormat`](/docs/platform/core/reference/types/file-format) | はい  |
| `formatMetadata`     | `FileUpload` で受け付けられますが、このメソッドでは送信されません。                          | `GTJsonFormatMetadata \| FormatMetadata`                        | はい  |
| `incomingBranchId`   | `FileUpload` で受け付けられますが、このメソッドでは送信されません。                          | `string`                                                        | はい  |
| `checkedOutBranchId` | `FileUpload` で受け付けられますが、このメソッドでは送信されません。                          | `string`                                                        | はい  |

### `options`

**型** `UploadFilesOptions` · **必須**

アップロードの設定:

| フィールド           | 説明                                                                                                                                                            | 型        | 任意  |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | --- |
| `sourceLocale`  | リクエストのソースロケール。プロジェクトのデフォルトロケールと異なる場合は、そちらも更新されます。                                                                                                             | `string` | いいえ |
| `modelProvider` | 共有オプション型では受け付けられますが、このアップロードメソッドからは送信されません。プロバイダの指定は代わりに [`enqueueFiles`](/docs/platform/core/reference/gt-class-methods/translation/enqueue-files) で行ってください。 | `string` | はい  |
| `timeout`       | ミリ秒単位のリクエストタイムアウト。                                                                                                                                            | `number` | はい  |

*注: `branchId` はアップロードオプションではありません。各ファイルオブジェクトに含まれるファイルごとのフィールドであり、`UploadFilesOptions` の一部ではありません。*

## 戻り値 [#returns]

**型** `Promise<UploadFilesResponse>`

アップロードされたファイル参照と要約を含む `UploadFilesResponse` が返されます：

```typescript
type UploadFilesResponse = {
  uploadedFiles: FileReference[]; // アップロードされたファイル参照
  count: number; // 正常にアップロードされたファイル数
  message: string; // APIからのステータスメッセージ
};
```

## 例 [#examples]

```typescript
// 基本的な使い方：以前にアップロードしたソースファイルの翻訳をアップロードする
import { GT } from 'generaltranslation';
import fs from 'fs';

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

const files = [
  {
    // これとまったく同じソースが、すでにアップロード済みである必要がある。
    source: {
      content: fs.readFileSync('./locales/en/common.json', 'utf8'),
      fileName: 'common.json',
      fileFormat: 'JSON' as const,
      locale: 'en',
    },
    translations: [
      {
        content: fs.readFileSync('./locales/es/common.json', 'utf8'),
        fileName: 'common.json',
        fileFormat: 'JSON' as const,
        locale: 'es',
      },
      {
        content: fs.readFileSync('./locales/fr/common.json', 'utf8'),
        fileName: 'common.json',
        fileFormat: 'JSON' as const,
        locale: 'fr',
      },
    ],
  },
];

const result = await gt.uploadTranslations(files, {
  sourceLocale: 'en',
});

console.log(`Uploaded ${result.count} translation files`);
```

```typescript
// 完全なワークフロー: ソースファイルをアップロードし、次にその翻訳をアップロードする
import { GT } from 'generaltranslation';
import fs from 'fs';

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

// ステップ1: ソースファイルをアップロードする
const sourceFiles = [
  {
    source: {
      content: fs.readFileSync('./locales/en/messages.json', 'utf8'),
      fileName: 'messages.json',
      fileFormat: 'JSON' as const,
      locale: 'en',
    },
  },
];

const { uploadedFiles } = await gt.uploadSourceFiles(sourceFiles, {
  sourceLocale: 'en',
});

// ステップ2: 既存の翻訳をアップロードする
const translationFiles = [
  {
    source: {
      content: sourceFiles[0].source.content,
      fileName: uploadedFiles[0].fileName,
      fileFormat: uploadedFiles[0].fileFormat,
      locale: sourceFiles[0].source.locale,
      fileId: uploadedFiles[0].fileId,
      versionId: uploadedFiles[0].versionId,
    },
    translations: [
      {
        content: fs.readFileSync('./locales/es/messages.json', 'utf8'),
        fileName: 'messages.json',
        fileFormat: 'JSON' as const,
        locale: 'es',
      },
      {
        content: fs.readFileSync('./locales/de/messages.json', 'utf8'),
        fileName: 'messages.json',
        fileFormat: 'JSON' as const,
        locale: 'de',
      },
    ],
  },
];

const translationResult = await gt.uploadTranslations(translationFiles, {
  sourceLocale: 'en',
});

console.log(`Uploaded ${translationResult.count} translations`);
```

```typescript
// 複数のソースファイルの翻訳をバッチアップロードする
import fs from 'node:fs';
import { GT } from 'generaltranslation';
import type { FileUpload } from 'generaltranslation/types';

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

async function uploadAllTranslations(
  sourceFiles: FileUpload[],
  targetLocales: string[]
) {
  const files = sourceFiles.map((source) => ({
    source,
    translations: targetLocales
      .map((locale) => {
        const translationPath = `./locales/${locale}/${source.fileName}`;
        try {
          return {
            content: fs.readFileSync(translationPath, 'utf8'),
            fileName: source.fileName,
            fileFormat: source.fileFormat,
            locale,
          };
        } catch {
          // このロケールの翻訳ファイルが存在しない
          return null;
        }
      })
      .filter((file): file is FileUpload => file !== null),
  }));

  const result = await gt.uploadTranslations(files, {
    sourceLocale: 'en',
    timeout: 60000,
  });

  return result;
}
```

## メモ [#notes]

* 各エントリの `source` オブジェクトには、コンテンツ、ファイル名、ファイル形式、ロケールを含める必要があります。
* そのオブジェクトが示すソースバージョンは、プロジェクト内に既に存在している必要があります。
* `translations` 配列内の各翻訳には、コンテンツと対象ロケールを含める必要があります。
* このメソッドは、既存の翻訳を移行する場合や、人手でレビューした翻訳をアップロードする場合に役立ちます。
* ファイル参照には、ブランチ対応のバージョニング用に `branchId` が含まれます。

## Sitemap

See the full [sitemap](https://generaltranslation.com/sitemap.md) for all pages.
