関数の説明
はじめに
Frenglish SDKは、開発者がアプリケーションに自動翻訳機能を組み込むための強力なツールです。このSDKは、翻訳依頼から翻訳結果の取得まで、翻訳プロセス全体を管理します。このドキュメントでは、SDK内の各メソッドの使い方を詳しく解説します。
インストール方法
インストール方法についてはクイックスタートガイドを参照してください。
SDKメソッド一覧
translate
translate(contents: string[], isFullTranslation: boolean, filenames: string[], partialConfig: PartialConfiguration): Promise<RequestTranslationResponse>
コンテンツを翻訳に送信します。このメソッドは自動的にポーリング処理を行い、翻訳が完了したら結果を返します。
パラメータ:
- content: string[] - 翻訳したいテキストの配列。各要素が個別のコンテンツを表します。
- fullTranslation: boolean(オプション、デフォルトはfalse)- 翻訳の挙動を制御します:
- false(デフォルト)の場合:データベース内の既存の翻訳をチェックして、新規または変更されたコンテンツのみを翻訳します。これにより、翻訳時間とコストを削減できます。
- trueの場合:既存の翻訳を無視して、すべてのコンテンツを強制的に再翻訳します。
- filenames: string[](オプション)- 各コンテンツに対応するファイル名の配列。プロジェクト内で翻訳を追跡・識別するために使います。指定する場合はcontent配列と同じ長さにしてください。ファイル名には拡張子(例:.json)を含めてください。
- partialConfig: PartialConfiguration(オプション)- この翻訳のデフォルト設定を上書きします。含められる内容:
{
originLanguage?: string, // Source language code
languages?: string[], // Target language codes
rules?: string, // General translation rules
autoMergeToBaseBranch?: boolean, // Auto-merge setting
implicitRules?: ImplicitRule[], // Array of implicit translation rules
rulesPerLanguage?: Rule[], // Language-specific rules
useThisConfig?: boolean, // Whether to use this config
keyFilters?: { // Filters for translation keys
includeFilters: string[],
excludeFilters: string[]
} | null
}
戻り値:
Promiseで、RequestTranslationResponseオブジェクト(以下を含む)を返します:
- translationId: number - 翻訳リクエストの一意なID。
- content?: TranslationResponse[] - 各言語ごとの翻訳結果を表すTranslationResponseオブジェクトの配列。
例:
const contents = [
'{"hello": "Hello, world!"}',
'{"goodbye": "Goodbye, world!"}'
];
const filenames = ['greetings.json', 'farewells.json'];
const partialConfig = {
languages: ['fr', 'es'],
rules: 'use an informal tone'
};
try {
const translation = await frenglish.translate(contents, false, filenames, partialConfig);
if (translation && translation.content) {
console.log('Translation completed:', translation.content);
} else {
console.log('Translation in progress or failed.');
}
} catch (error) {
console.error('Translation error:', error.message);
}