@intlayer/docs 7.3.7 → 7.3.9
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.
- package/docs/ar/intlayer_with_tanstack.md +52 -1
- package/docs/ar/intlayer_with_vite+vue.md +16 -34
- package/docs/de/intlayer_with_tanstack.md +52 -1
- package/docs/de/intlayer_with_vite+vue.md +16 -34
- package/docs/en/intlayer_with_tanstack.md +52 -1
- package/docs/en/intlayer_with_vite+vue.md +16 -34
- package/docs/en-GB/intlayer_with_tanstack.md +52 -1
- package/docs/en-GB/intlayer_with_vite+vue.md +16 -34
- package/docs/es/intlayer_with_tanstack.md +52 -1
- package/docs/es/intlayer_with_vite+vue.md +16 -34
- package/docs/fr/intlayer_with_tanstack.md +52 -1
- package/docs/fr/intlayer_with_vite+vue.md +16 -34
- package/docs/hi/intlayer_with_tanstack.md +52 -1
- package/docs/hi/intlayer_with_vite+vue.md +16 -34
- package/docs/id/intlayer_with_tanstack.md +52 -1
- package/docs/id/intlayer_with_vite+vue.md +16 -34
- package/docs/it/intlayer_with_tanstack.md +52 -1
- package/docs/it/intlayer_with_vite+vue.md +16 -34
- package/docs/ja/intlayer_with_tanstack.md +52 -1
- package/docs/ja/intlayer_with_vite+vue.md +16 -34
- package/docs/ko/intlayer_with_tanstack.md +52 -1
- package/docs/ko/intlayer_with_vite+vue.md +16 -34
- package/docs/pl/intlayer_with_tanstack.md +52 -1
- package/docs/pl/intlayer_with_vite+vue.md +16 -34
- package/docs/pt/intlayer_with_tanstack.md +52 -1
- package/docs/pt/intlayer_with_vite+vue.md +16 -34
- package/docs/ru/intlayer_with_tanstack.md +52 -1
- package/docs/ru/intlayer_with_vite+vue.md +16 -34
- package/docs/tr/intlayer_with_tanstack.md +52 -1
- package/docs/tr/intlayer_with_vite+vue.md +16 -34
- package/docs/vi/intlayer_with_tanstack.md +52 -1
- package/docs/vi/intlayer_with_vite+vue.md +16 -34
- package/docs/zh/intlayer_with_tanstack.md +52 -1
- package/docs/zh/intlayer_with_vite+vue.md +16 -34
- package/package.json +6 -6
|
@@ -18,6 +18,9 @@ slugs:
|
|
|
18
18
|
- tanstack-start
|
|
19
19
|
applicationTemplate: https://github.com/aymericzip/intlayer-tanstack-start-template
|
|
20
20
|
history:
|
|
21
|
+
- version: 7.3.9
|
|
22
|
+
date: 2025-12-05
|
|
23
|
+
changes: Add step 13: Retrieve the locale in your server actions (Optional)
|
|
21
24
|
- version: 5.8.1
|
|
22
25
|
date: 2025-09-09
|
|
23
26
|
changes: 为 Tanstack Start 添加支持
|
|
@@ -529,7 +532,55 @@ export const Route = createFileRoute("/{-$locale}/")({
|
|
|
529
532
|
|
|
530
533
|
---
|
|
531
534
|
|
|
532
|
-
###
|
|
535
|
+
### Step 13: Retrieve the locale in your server actions (Optional)
|
|
536
|
+
|
|
537
|
+
You may want to access the current locale from inside your server actions or API endpoints.
|
|
538
|
+
You can do this using the `getLocale` helper from `intlayer`.
|
|
539
|
+
|
|
540
|
+
Here's an example using TanStack Start's server functions:
|
|
541
|
+
|
|
542
|
+
```tsx fileName="src/routes/{-$locale}/index.tsx"
|
|
543
|
+
import { createServerFn } from "@tanstack/react-start";
|
|
544
|
+
import {
|
|
545
|
+
getRequestHeader,
|
|
546
|
+
getRequestHeaders,
|
|
547
|
+
} from "@tanstack/react-start/server";
|
|
548
|
+
import { getCookie, getIntlayer, getLocale } from "intlayer";
|
|
549
|
+
|
|
550
|
+
export const getLocaleServer = createServerFn().handler(async () => {
|
|
551
|
+
const locale = await getLocale({
|
|
552
|
+
// Get the cookie from the request (default: 'INTLAYER_LOCALE')
|
|
553
|
+
getCookie: (name) => {
|
|
554
|
+
const cookieString = getRequestHeader("cookie");
|
|
555
|
+
|
|
556
|
+
return getCookie(name, cookieString);
|
|
557
|
+
},
|
|
558
|
+
// Get the header from the request (default: 'x-intlayer-locale')
|
|
559
|
+
getHeader: (name) => getRequestHeader(name),
|
|
560
|
+
// Fallback using Accept-Language negotiation
|
|
561
|
+
getAllHeaders: async () => {
|
|
562
|
+
const headers = getRequestHeaders();
|
|
563
|
+
const result: Record<string, string> = {};
|
|
564
|
+
|
|
565
|
+
// Convert the TypedHeaders into a plain Record<string, string>
|
|
566
|
+
for (const [key, value] of headers.entries()) {
|
|
567
|
+
result[key] = value;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
return result;
|
|
571
|
+
},
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
// Retrieve some content using getIntlayer()
|
|
575
|
+
const content = getIntlayer("app", locale);
|
|
576
|
+
|
|
577
|
+
return { locale, content };
|
|
578
|
+
});
|
|
579
|
+
```
|
|
580
|
+
|
|
581
|
+
---
|
|
582
|
+
|
|
583
|
+
### 第14步:配置 TypeScript(可选)
|
|
533
584
|
|
|
534
585
|
Intlayer 使用模块增强来利用 TypeScript 的优势,使您的代码库更健壮。
|
|
535
586
|
|
|
@@ -566,53 +566,47 @@ const content = useIntlayer("app"); // 创建相关的 intlayer 声明文件
|
|
|
566
566
|
首先,安装 Vue Router:
|
|
567
567
|
|
|
568
568
|
```bash packageManager="npm"
|
|
569
|
-
npm install
|
|
569
|
+
npm install vue-router
|
|
570
570
|
```
|
|
571
571
|
|
|
572
572
|
```bash packageManager="pnpm"
|
|
573
|
-
pnpm add
|
|
573
|
+
pnpm add vue-router
|
|
574
574
|
```
|
|
575
575
|
|
|
576
576
|
```bash packageManager="yarn"
|
|
577
|
-
yarn add
|
|
577
|
+
yarn add vue-router
|
|
578
578
|
```
|
|
579
579
|
|
|
580
580
|
然后,创建一个处理基于语言环境路由的路由配置:
|
|
581
581
|
|
|
582
582
|
```js fileName="src/router/index.ts"
|
|
583
583
|
import {
|
|
584
|
-
configuration,
|
|
585
|
-
getPathWithoutLocale,
|
|
586
584
|
localeFlatMap,
|
|
587
|
-
type
|
|
585
|
+
type Locale,
|
|
588
586
|
} from 'intlayer';
|
|
589
587
|
import { createIntlayerClient } from 'vue-intlayer';
|
|
590
588
|
import { createRouter, createWebHistory } from 'vue-router';
|
|
591
589
|
import HomeView from './views/home/HomeView.vue';
|
|
592
590
|
import RootView from './views/root/Root.vue';
|
|
593
591
|
|
|
594
|
-
// 获取国际化配置
|
|
595
|
-
const { internationalization, middleware } = configuration;
|
|
596
|
-
const { defaultLocale } = internationalization;
|
|
597
|
-
|
|
598
592
|
/**
|
|
599
593
|
* 声明带有语言环境特定路径和元数据的路由。
|
|
600
594
|
*/
|
|
601
|
-
const routes = localeFlatMap((
|
|
595
|
+
const routes = localeFlatMap(({ urlPrefix, locale }) => [
|
|
602
596
|
{
|
|
603
|
-
path: `${
|
|
604
|
-
name: `Root-${
|
|
597
|
+
path: `${urlPrefix}/`,
|
|
598
|
+
name: `Root-${locale}`,
|
|
605
599
|
component: RootView,
|
|
606
600
|
meta: {
|
|
607
|
-
locale
|
|
601
|
+
locale,
|
|
608
602
|
},
|
|
609
603
|
},
|
|
610
604
|
{
|
|
611
|
-
path: `${
|
|
612
|
-
name: `Home-${
|
|
605
|
+
path: `${urlPrefix}/home`,
|
|
606
|
+
name: `Home-${locale}`,
|
|
613
607
|
component: HomeView,
|
|
614
608
|
meta: {
|
|
615
|
-
locale
|
|
609
|
+
locale,
|
|
616
610
|
},
|
|
617
611
|
},
|
|
618
612
|
]);
|
|
@@ -627,23 +621,11 @@ export const router = createRouter({
|
|
|
627
621
|
router.beforeEach((to, _from, next) => {
|
|
628
622
|
const client = createIntlayerClient();
|
|
629
623
|
|
|
630
|
-
const metaLocale = to.meta.locale as
|
|
624
|
+
const metaLocale = to.meta.locale as Locale;
|
|
631
625
|
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
next();
|
|
636
|
-
} else {
|
|
637
|
-
// 回退:元信息中没有语言环境,可能是未匹配的路由
|
|
638
|
-
// 可选:处理 404 或重定向到默认语言环境
|
|
639
|
-
client.setLocale(defaultLocale);
|
|
640
|
-
|
|
641
|
-
if (middleware.prefixDefault) {
|
|
642
|
-
next(`/${defaultLocale}${getPathWithoutLocale(to.path)}`);
|
|
643
|
-
} else {
|
|
644
|
-
next(getPathWithoutLocale(to.path));
|
|
645
|
-
}
|
|
646
|
-
}
|
|
626
|
+
// 重用路由元信息中定义的语言环境
|
|
627
|
+
client.setLocale(metaLocale);
|
|
628
|
+
next();
|
|
647
629
|
});
|
|
648
630
|
```
|
|
649
631
|
|
|
@@ -773,7 +755,7 @@ watch(
|
|
|
773
755
|
提示:为了更好的 SEO 和可访问性,使用如 `<a href="/fr/home" hreflang="fr">` 这样的标签来链接到本地化页面,如步骤10所示。这允许搜索引擎正确发现和索引特定语言的 URL。为了保持 SPA 行为,可以通过 @click.prevent 阻止默认导航,使用 useLocale 更改语言环境,并通过 Vue Router 编程式导航。
|
|
774
756
|
|
|
775
757
|
```html
|
|
776
|
-
<ol
|
|
758
|
+
<ol>
|
|
777
759
|
<li>
|
|
778
760
|
<a
|
|
779
761
|
hreflang="x-default"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intlayer/docs",
|
|
3
|
-
"version": "7.3.
|
|
3
|
+
"version": "7.3.9",
|
|
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.
|
|
77
|
-
"@intlayer/core": "7.3.
|
|
78
|
-
"@intlayer/types": "7.3.
|
|
76
|
+
"@intlayer/config": "7.3.9",
|
|
77
|
+
"@intlayer/core": "7.3.9",
|
|
78
|
+
"@intlayer/types": "7.3.9"
|
|
79
79
|
},
|
|
80
80
|
"devDependencies": {
|
|
81
|
-
"@intlayer/api": "7.3.
|
|
82
|
-
"@intlayer/cli": "7.3.
|
|
81
|
+
"@intlayer/api": "7.3.9",
|
|
82
|
+
"@intlayer/cli": "7.3.9",
|
|
83
83
|
"@types/node": "24.10.1",
|
|
84
84
|
"@utils/ts-config": "1.0.4",
|
|
85
85
|
"@utils/ts-config-types": "1.0.4",
|