@intlayer/docs 7.3.15 → 7.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,9 +19,12 @@ slugs:
19
19
  applicationTemplate: https://github.com/aymericzip/intlayer-tanstack-start-template
20
20
  youtubeVideo: https://www.youtube.com/watch?v=_XTdKVWaeqg
21
21
  history:
22
+ - version: 7.4.0
23
+ date: 2025-12-11
24
+ changes: 引入 validatePrefix 并添加步骤 14: 处理带有本地化路由的 404 页面。
22
25
  - version: 7.3.9
23
26
  date: 2025-12-05
24
- changes: Add step 13: Retrieve the locale in your server actions (Optional)
27
+ changes: 添加步骤 13: 在您的 server actions 中获取 locale (可选)
25
28
  - version: 5.8.1
26
29
  date: 2025-09-09
27
30
  changes: 为 Tanstack Start 添加支持
@@ -591,7 +594,134 @@ export const getLocaleServer = createServerFn().handler(async () => {
591
594
 
592
595
  ---
593
596
 
594
- ### 第14步:配置 TypeScript(可选)
597
+ ### 第14步:管理未找到的页面(可选)
598
+
599
+ 当用户访问不存在的页面时,您可以显示自定义的未找到页面,并且区域设置前缀可能会影响未找到页面的触发方式。
600
+
601
+ #### 了解 TanStack Router 使用区域设置前缀的 404 处理
602
+
603
+ 在 TanStack Router 中,使用本地化路由处理 404 页面需要采用多层方法:
604
+
605
+ 1. **专用 404 路由**:用于显示 404 UI 的特定路由
606
+ 2. **路由级验证**:验证区域设置前缀并将无效的前缀重定向到 404
607
+ 3. **捕获所有路由**:捕获区域设置段内任何不匹配的路径
608
+
609
+ ```tsx fileName="src/routes/{-$locale}/404.tsx"
610
+ import { createFileRoute } from "@tanstack/react-router";
611
+
612
+ // 这将创建一个专用的 /[locale]/404 路由
613
+ // 它既作为直接路由使用,也可以在其他文件中作为组件导入
614
+ export const Route = createFileRoute("/{-$locale}/404")({
615
+ component: NotFoundComponent,
616
+ });
617
+
618
+ // 单独导出,以便可以在 notFoundComponent 和 catch-all 路由中重用
619
+ export function NotFoundComponent() {
620
+ return (
621
+ <div>
622
+ <h1>404</h1>
623
+ </div>
624
+ );
625
+ }
626
+ ```
627
+
628
+ ```tsx fileName="src/routes/__root.tsx"
629
+ import { createRootRoute } from "@tanstack/react-router";
630
+
631
+ // 根路由作为顶级布局
632
+ // 它不直接处理 404 - 这被委托给子路由
633
+ // 这使根路由保持简单,并让区域设置感知的路由管理自己的 404 逻辑
634
+ export const Route = createRootRoute({
635
+ component: Outlet,
636
+ });
637
+ ```
638
+
639
+ ```tsx fileName="src/routes/{-$locale}/route.tsx"
640
+ import { createFileRoute, Outlet, redirect } from "@tanstack/react-router";
641
+ import { validatePrefix } from "intlayer";
642
+ import { IntlayerProvider, useLocale } from "react-intlayer";
643
+
644
+ import { LocaleSwitcher } from "@/components/locale-switcher";
645
+ import { NotFoundComponent } from "./404";
646
+
647
+ export const Route = createFileRoute("/{-$locale}")({
648
+ // beforeLoad 在路由渲染之前运行(在服务器和客户端上)
649
+ // 这是验证区域设置前缀的理想位置
650
+ beforeLoad: ({ params }) => {
651
+ // 从路由参数获取区域设置(不是从服务器标头,因为 beforeLoad 在客户端和服务器上都会运行)
652
+ const localeParam = params.locale;
653
+
654
+ // validatePrefix 检查区域设置是否根据您的 intlayer 配置有效
655
+ // 返回: { isValid: boolean, localePrefix: string }
656
+ // - isValid: 如果前缀匹配配置的区域设置(或当前缀可选时为空),则为 true
657
+ // - localePrefix: 已验证的前缀或用于重定向的默认区域设置前缀
658
+ const { isValid, localePrefix } = validatePrefix(localeParam);
659
+
660
+ if (isValid) {
661
+ // 区域设置有效,允许路由正常渲染
662
+ return;
663
+ }
664
+
665
+ // 无效的区域设置前缀(例如,/xyz/about 其中 "xyz" 不是有效的区域设置)
666
+ // 重定向到具有有效区域设置前缀的 404 页面
667
+ // 这确保 404 页面仍然正确本地化
668
+ throw redirect({
669
+ to: "/{-$locale}/404",
670
+ params: { locale: localePrefix },
671
+ });
672
+ },
673
+ component: RouteComponent,
674
+ // notFoundComponent 在子路由不存在时被调用
675
+ // 例如,/en/不存在的页面 在 /en 布局内触发此操作
676
+ notFoundComponent: NotFoundLayout,
677
+ });
678
+
679
+ function RouteComponent() {
680
+ const { defaultLocale } = useLocale();
681
+ const { locale } = Route.useParams();
682
+
683
+ return (
684
+ // 用 IntlayerProvider 包装整个区域设置段
685
+ // 当区域设置参数为 undefined 时回退到 defaultLocale(可选前缀模式)
686
+ <IntlayerProvider locale={locale ?? defaultLocale}>
687
+ <Outlet />
688
+ </IntlayerProvider>
689
+ );
690
+ }
691
+
692
+ // NotFoundLayout 用 IntlayerProvider 包装 404 组件
693
+ // 这确保翻译在 404 页面上仍然有效
694
+ function NotFoundLayout() {
695
+ const { defaultLocale } = useLocale();
696
+ const { locale } = Route.useParams();
697
+
698
+ return (
699
+ <IntlayerProvider locale={locale ?? defaultLocale}>
700
+ <NotFoundComponent />
701
+ {/* 包含 LocaleSwitcher,以便用户即使在 404 页面上也可以更改语言 */}
702
+ <LocaleSwitcher />
703
+ </IntlayerProvider>
704
+ );
705
+ }
706
+ ```
707
+
708
+ ```tsx fileName="src/routes/{-$locale}/$.tsx"
709
+ import { createFileRoute } from "@tanstack/react-router";
710
+
711
+ import { NotFoundComponent } from "./404";
712
+
713
+ // $ (splat/catch-all) 路由匹配任何与其他路由不匹配的路径
714
+ // 例如,/en/某个/深度/嵌套/无效/路径
715
+ // 这确保区域设置内所有不匹配的路径都显示 404 页面
716
+ // 没有这个,不匹配的深层路径可能会显示空白页面或错误
717
+ export const Route = createFileRoute("/{-$locale}/$")({
718
+ component: NotFoundComponent,
719
+ });
720
+ ```
721
+
722
+ ---
723
+
724
+ ### 第15步:配置 TypeScript(可选)
595
725
 
596
726
  Intlayer 使用模块增强来利用 TypeScript 的优势,使您的代码库更健壮。
597
727
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intlayer/docs",
3
- "version": "7.3.15",
3
+ "version": "7.4.0",
4
4
  "private": false,
5
5
  "description": "Intlayer documentation",
6
6
  "keywords": [
@@ -73,13 +73,13 @@
73
73
  "watch": "webpack --config ./webpack.config.ts --watch"
74
74
  },
75
75
  "dependencies": {
76
- "@intlayer/config": "7.3.15",
77
- "@intlayer/core": "7.3.15",
78
- "@intlayer/types": "7.3.15"
76
+ "@intlayer/config": "7.4.0",
77
+ "@intlayer/core": "7.4.0",
78
+ "@intlayer/types": "7.4.0"
79
79
  },
80
80
  "devDependencies": {
81
- "@intlayer/api": "7.3.15",
82
- "@intlayer/cli": "7.3.15",
81
+ "@intlayer/api": "7.4.0",
82
+ "@intlayer/cli": "7.4.0",
83
83
  "@types/node": "24.10.1",
84
84
  "@utils/ts-config": "1.0.4",
85
85
  "@utils/ts-config-types": "1.0.4",