# General Translation Python SDKs: TranslationsLoader URL: https://generaltranslation.com/ja/docs/python/reference/classes/translations-loader.mdx --- title: TranslationsLoader description: General Translation Python におけるカスタム翻訳ローダー用の callable 型。TranslationsLoader の API リファレンス。 --- 独自の翻訳を利用するために `load_translations` として渡す callback である、カスタム翻訳ローダーの型です。ロケールコードを翻訳 dict にマッピングします。 *注: `TranslationsLoader` は**型エイリアス**であり、クラス、protocol、ABC ではありません。これを継承するのではなく、そのシグネチャに一致する callable を渡します。* ## 概要 [#overview] `TranslationsLoader` は `Callable` のエイリアスとして定義されています: ```python from collections.abc import Awaitable, Callable TranslationsLoader = Callable[[str], dict[str, str] | Awaitable[dict[str, str]]] ``` ローダーはロケールコードを受け取り、メッセージハッシュを翻訳済み文字列に対応付けた `dict[str, str]` のマッピングを返します。同期的でも、awaitable な値を返す形でもかまいません。awaitable な場合は、マネージャーがその結果を await します。 `gt_i18n` から `TranslationsLoader` をインポートします。これはフレームワークパッケージからは再エクスポートされていません。 ## 仕組み [#how-it-works] * **入力。** 単一のロケールコード文字列 (例: `"es"`) 。 * **出力。** ハッシュ → 翻訳済み文字列の `dict[str, str]`、またはそれを返す awaitable。 * **使用箇所。** [`I18nManager`](/docs/python/reference/classes/i18n-manager) または [`initialize_gt`](/docs/python/reference/functions/initialize-gt) に `load_translations` として渡します。指定した場合、組み込みの CDN ローダーより優先されます。キーには [`hash_message`](/docs/python/reference/functions/hash-message) で生成されたハッシュを使用する必要があります。 ## 例 [#example] ```python import json from pathlib import Path from gt_i18n import I18nManager, TranslationsLoader # ロケールごとに1つのJSONファイルを読み込む同期ローダー def load_translations(locale: str) -> dict[str, str]: path = Path("translations") / f"{locale}.json" return json.loads(path.read_text()) if path.exists() else {} loader: TranslationsLoader = load_translations manager = I18nManager(default_locale="en", locales=["es"], load_translations=loader) ``` ```python # 非同期ローダーも有効です async def load_translations(locale: str) -> dict[str, str]: return await fetch_from_somewhere(locale) ```