Paraglide JS

テナントごとに異なるデフォルトロケールと URL 構造を設定できます:

https://customer1.com/about      → de (デフォルトはドイツ語)
https://customer2.de/en/about    → en (パスで英語を指定)
https://customer3.com/fr/about   → fr (パスでフランス語を指定)

[!TIP]このガイドは URL パターンの概念を前提としています。基本については i18n ルーティング を参照してください。

i18n におけるマルチテナンシーとは、異なるローカリゼーション設定を使い分けて提供することです。特に次のような場合に役立ちます。

  • デフォルト言語がそれぞれ異なる複数の顧客ドメインがある
  • 顧客ごとに独自のサブドメインを持つ SaaS プラットフォームを運営している
  • アプリのセクションごとに異なるローカリゼーション戦略が必要である

たとえば、次のような構成が考えられます。

  • customer1.fr はフランス語をデフォルトにし、英語も選択できるようにする
  • customer2.com は英語をデフォルトにし、フランス語も選択できるようにする
  • その他のドメインはすべてパスベースのローカリゼーション (/en/、/fr/) を使う

マルチテナンシーを使えば、これらすべてのケースを 1 つの設定で扱えます。

ユースケース

  • 地域ビジネス: 市場ごとに異なるドメインを使用(例: フランス向けに .fr、ドイツ向けに .de)

  • ホワイトラベルソリューション: クライアントごとに独自のドメインと固有の言語設定を割り当てる

  • エンタープライズアプリケーション: 部門や子会社ごとに異なるデフォルト言語を設定する必要がある

設定

urlPatterns を使うと、テナントのドメインごとに異なるルーティングルールを定義できます。

compile({
  project: "./project.inlang",
  outdir: "./src/paraglide",
  strategy: ["url", "baseLocale"],
  urlPatterns: [
    // 開発用の localhost(パスプレフィックスを使用)
    {
      pattern: "http://localhost::port?/:path(.*)?",
      localized: [
        ["fr", "http://localhost::port?/fr/:path(.*)?"],
        ["en", "http://localhost::port?/:path(.*)?"],
      ],
    },
    // customer1.fr => デフォルトはフランス語、英語は /en/ で配信
    {
      pattern: "https://customer1.fr/:path(.*)?",
      localized: [
        ["fr", "https://customer1.fr/:path(.*)?"],
        ["en", "https://customer1.fr/en/:path(.*)?"],
      ],
    },
    // customer2.com => デフォルトは英語、フランス語は /fr/ で配信
    {
      pattern: "https://customer2.com/:path(.*)?",
      localized: [
        ["en", "https://customer2.com/:path(.*)?"],
        ["fr", "https://customer2.com/fr/:path(.*)?"],
      ],
    },
  ],
});

[!NOTE]strategy 配列に baseLocale を追加しておくと、どのパターンにも一致しない場合でもロケールが必ず解決されます。

顧客 1

  • フランス語からフランス語へのローカライズ:

    localizeHref("https://customer1.fr/about", { locale: "fr" })
    // 出力: "https://customer1.fr/about"
    
  • フランス語から英語へのローカライズ:

    localizeHref("https://customer1.fr/about", { locale: "en" })
    // 出力: "https://customer1.fr/en/about"
    
  • 英語のロケール情報の除去:

    deLocalizeHref("https://customer1.fr/en/about");
    // 出力: "https://customer1.fr/about"
    

Customer 2

  • 英語から英語へのローカライズ:

    localizeHref("https://customer2.com/about", { locale: "en" })
    // 出力: "https://customer2.com/about"
    
  • 英語からフランス語へのローカライズ:

    localizeHref("https://customer2.com/about", { locale: "fr" })
    // 出力: "https://customer2.com/fr/about"
    
  • フランス語URLからのロケール情報の除去:

    deLocalizeHref("https://customer2.com/fr/about");
    // 出力: "https://customer2.com/about"
    

テナントごとに特定のロケールを無効化する

テナントによっては、一部のロケールを制限したい場合があります。

  • 顧客 A は英語とドイツ語のみに対応
  • 顧客 B はフランス語とスペイン語のみに対応
  • 顧客 C はすべてのロケールに対応

サポート対象外のロケールは 404 ページにリダイレクトします。

compile({
  project: "./project.inlang",
  outdir: "./src/paraglide",
  strategy: ["url", "baseLocale"],
  urlPatterns: [
    // Customer1 - en と de のみ対応
    {
      pattern: "https://customer1.com/:path(.*)?",
      localized: [
        ["en", "https://customer1.com/:path(.*)?"],
        ["de", "https://customer1.com/de/:path(.*)?"],
        ["fr", "https://customer1.com/404"],  // 非対応 → 404
        ["es", "https://customer1.com/404"],
      ],
    },
    // Customer2 - fr と es のみ対応
    {
      pattern: "https://customer2.com/:path(.*)?",
      localized: [
        ["fr", "https://customer2.com/:path(.*)?"],
        ["es", "https://customer2.com/es/:path(.*)?"],
        ["en", "https://customer2.com/404"],  // 非対応 → 404
        ["de", "https://customer2.com/404"],
      ],
    },
    // Customer3 - すべてのロケールに対応
    {
      pattern: "https://customer3.com/:path(.*)?",
      localized: [
        ["en", "https://customer3.com/:path(.*)?"],
        ["de", "https://customer3.com/de/:path(.*)?"],
        ["fr", "https://customer3.com/fr/:path(.*)?"],
        ["es", "https://customer3.com/es/:path(.*)?"],
      ],
    },
  ],
});

特定のテナントでサポートされていないロケールのURLにユーザーがアクセスしようとすると、404ページにリダイレクトされます。

// Customer1 はフランス語に非対応
localizeHref("https://customer1.com/about", { locale: "fr" })
// 出力: "https://customer1.com/404"

// Customer2 はドイツ語に非対応
localizeHref("https://customer2.com/about", { locale: "de" })
// 出力: "https://customer2.com/404"

// Customer3 はすべてのロケールに対応
localizeHref("https://customer3.com/about", { locale: "fr" })
// 出力: "https://customer3.com/fr/about"

この方法により、次のことが可能になります。

  1. テナントごとにサポートするロケールを定義する
  2. サポートされていないロケールへのリクエストを適切に処理する
  3. マルチテナントアプリケーション全体で一貫したユーザー体験を維持する

テナントごとのロケールスイッチャー

現在のテナントがサポートしているロケールだけを表示します。これにより、ユーザーが404ページに遷移するリンクをクリックしてしまうのを防げます。

スイッチャーをレンダリングする前に、現在のテナントに応じて利用可能なロケールを絞り込みます。本番環境では通常、このマッピングは値をハードコードせず、設定ファイルやAPIから取得します。

React

import { localizeHref, getLocale } from "./paraglide/runtime.js";

const TENANT_LOCALES = {
  "customer1.com": ["en", "de"],
  "customer2.com": ["fr", "es"],
  "customer3.com": ["en", "de", "fr", "es"],
};

function LanguageSwitcher() {
  const hostname = window.location.hostname;
  const currentLocale = getLocale();
  const supportedLocales = TENANT_LOCALES[hostname] ?? ["en"];

  return (
    <div className="language-switcher">
      {supportedLocales.map(locale => (
        <a
          key={locale}
          href={localizeHref(window.location.href, { locale })}
          aria-current={locale === currentLocale ? "page" : undefined}
        >
          {locale.toUpperCase()}
        </a>
      ))}
    </div>
  );
}