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/)

借助多租户,只需一份配置即可处理以上所有情况。

使用场景

  • 区域性业务:不同市场使用不同域名(例如法国用 .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"
    

客户 2

  • 将英语本地化为英语:

    localizeHref("https://customer2.com/about", { locale: "en" })
    // 输出:"https://customer2.com/about"
    
  • 将英语本地化为法语:

    localizeHref("https://customer2.com/about", { locale: "fr" })
    // 输出:"https://customer2.com/fr/about"
    
  • 对法语进行去本地化:

    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>
  );
}