コンテンツにスキップ

辞書を使う

辞書は、従来のキー・バリュー形式の翻訳ファイルです。次のような用途で役立ちます。

  • 多くのコンポーネントで共有する文字列(ボタンラベル、エラーメッセージ)
  • コードベースの外部で管理されるコンテンツ
  • 既存の i18n セットアップからの移行
{
"dictionaries": {
"include": ["src/dictionaries/*.json"],
"format": "key-value"
}
}
src/dictionaries/common.json
{
"save": "Save",
"cancel": "Cancel",
"delete": "Delete",
"loading": "Loading..."
}

useDictionary(filenameKey) は 1 つの辞書ファイルを参照し、Record<string, string> を返します。ファイル名キーは、辞書パスから .json 拡張子を除いたものです。

  • src/dictionaries/common.jsoncommon
  • src/dictionaries/pages/home.jsonpages/home
import { useDictionary } from 'tyndale-react';
export function ActionButtons() {
const labels = useDictionary('common');
return (
<div>
<button>{labels.save ?? 'Save'}</button>
<button>{labels.cancel ?? 'Cancel'}</button>
</div>
);
}

ファイル名キーに一致するエントリがない場合、useDictionary(filenameKey) は空のオブジェクトを返します。