コンテンツにスキップ
i18n ルーティングと自動エントリーページ

i18n ルーティングと自動エントリーページ

Tralume は Hugo にすべての正式なコンテンツ URL を通常通り生成させた後、ビルド後のステップとして「スマート言語エントリーページ」を追加します。

ルーティングモード

  • defaultContentLanguageInSubdir = true: 正式なコンテンツ URL は /en-us/.../zh-hans/... の下に置かれ、ニュートラルなエントリーページは //posts/test/ を使用します。
  • defaultContentLanguageInSubdir = false: デフォルト言語はルート URL を維持し、ニュートラルなエントリーページは /auto//auto/posts/test/ に移動します。
  • 明示的な言語 URL は自動的に書き換えられることはありません。エントリーページのみが言語ベースのリダイレクトを実行します。
  • post または page が1つの言語のみに存在する場合、エントリーページは localStoragenavigator.languages をチェックせずにその単一のターゲットに直接リダイレクトします。

基本設定

hugo.toml に以下を設定します:

defaultContentLanguage = 'en-US'
defaultContentLanguageInSubdir = true
# Note: Disable Hugo's built-in default-language redirect and let the theme's entry-page script handle it.
disableDefaultLanguageRedirect = true

[params.i18nRouting]
  # Note: Enable smart language entry pages generated after the build.
  enableAutoEntry = true

実際のビルドパイプラインに python3 tools/gen_auto_entries.py を組み込んだ後にのみ有効にしてください。それ以外の場合は無効のままにし、Hugo の組み込みデフォルト言語リダイレクトを使用してください。

ビルドフロー

テーマはまず Hugo に正式なページと route-manifest.json のレンダリングを依頼し、その後 Python ジェネレーターを実行します:

# Note: First build Hugo's formal content pages and route manifest.
# Note: This example writes verification output to public_test/ to avoid polluting the default public/ directory.
hugo --destination public_test/hugo-auto-entry

# Note: Read route-manifest.json and generate smart entry pages inside the publish directory.
# Note: The script checks for path conflicts and removes stale entry pages generated by previous runs.
python3 tools/gen_auto_entries.py --publish-dir public_test/hugo-auto-entry

注意事項

  • エントリーページは固定の localStorage['tralume-language'] キーを最初に読み取ります。
  • 保存された言語が利用できない場合、navigator.languages にフォールバックします。
  • 何も一致しない場合、デフォルト言語の正式なページにリダイレクトします。
  • エントリーページは常に noindex,follow を使用し、canonical を正式なコンテンツページに向けます。これらは Hugo のサイトマップや RSS 出力には含まれません。
  • この機能が有効でない場合、テーマは Hugo のネイティブ動作を維持し、Hugo にルートパスのデフォルト言語リダイレクトを処理させます。
最終更新日 • alexma233