@intlayer/docs 7.3.8 → 7.3.10
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 +73 -1
- package/docs/ar/intlayer_with_vite+vue.md +37 -34
- package/docs/de/intlayer_with_tanstack.md +73 -1
- package/docs/de/intlayer_with_vite+vue.md +37 -34
- package/docs/en/intlayer_with_tanstack.md +63 -1
- package/docs/en/intlayer_with_vite+vue.md +27 -34
- package/docs/en-GB/intlayer_with_tanstack.md +73 -1
- package/docs/en-GB/intlayer_with_vite+vue.md +37 -34
- package/docs/es/intlayer_with_tanstack.md +73 -1
- package/docs/es/intlayer_with_vite+vue.md +37 -36
- package/docs/fr/intlayer_with_tanstack.md +73 -1
- package/docs/fr/intlayer_with_vite+vue.md +37 -36
- package/docs/hi/intlayer_with_tanstack.md +73 -1
- package/docs/hi/intlayer_with_vite+vue.md +37 -34
- package/docs/id/intlayer_with_tanstack.md +63 -1
- package/docs/id/intlayer_with_vite+vue.md +28 -35
- package/docs/it/intlayer_with_tanstack.md +73 -1
- package/docs/it/intlayer_with_vite+vue.md +37 -34
- package/docs/ja/intlayer_with_tanstack.md +73 -1
- package/docs/ja/intlayer_with_vite+vue.md +37 -34
- package/docs/ko/intlayer_with_tanstack.md +73 -1
- package/docs/ko/intlayer_with_vite+vue.md +37 -34
- package/docs/pl/intlayer_with_tanstack.md +63 -1
- package/docs/pl/intlayer_with_vite+vue.md +33 -34
- package/docs/pt/intlayer_with_tanstack.md +73 -1
- package/docs/pt/intlayer_with_vite+vue.md +37 -34
- package/docs/ru/intlayer_with_tanstack.md +73 -1
- package/docs/ru/intlayer_with_vite+vue.md +37 -34
- package/docs/tr/intlayer_with_tanstack.md +73 -1
- package/docs/tr/intlayer_with_vite+vue.md +37 -34
- package/docs/vi/intlayer_with_tanstack.md +63 -1
- package/docs/vi/intlayer_with_vite+vue.md +33 -34
- package/docs/zh/intlayer_with_tanstack.md +73 -1
- package/docs/zh/intlayer_with_vite+vue.md +37 -34
- package/package.json +6 -6
|
@@ -40,6 +40,27 @@ Intlayer ile şunları yapabilirsiniz:
|
|
|
40
40
|
|
|
41
41
|
## Vite ve Vue Uygulamasında Intlayer Kurulumu İçin Adım Adım Kılavuz
|
|
42
42
|
|
|
43
|
+
<Tab defaultTab="video">
|
|
44
|
+
<TabItem label="Video" value="video">
|
|
45
|
+
|
|
46
|
+
<iframe title="The best i18n solution for Vite and Vue? Discover Intlayer" class="m-auto aspect-[16/9] w-full overflow-hidden rounded-lg border-0" allow="autoplay; gyroscope;" loading="lazy" width="1080" height="auto" src="https://www.youtube.com/embed/IE3XWkZ6a5U?autoplay=0&origin=http://intlayer.org&controls=0&rel=1"/>
|
|
47
|
+
|
|
48
|
+
</TabItem>
|
|
49
|
+
<TabItem label="Kod" value="code">
|
|
50
|
+
|
|
51
|
+
<iframe
|
|
52
|
+
src="https://stackblitz.com/github/aymericzip/intlayer-vite-vue-template?embed=1&ctl=1&file=intlayer.config.ts"
|
|
53
|
+
className="m-auto overflow-hidden rounded-lg border-0 max-md:size-full max-md:h-[700px] md:aspect-16/9 md:w-full"
|
|
54
|
+
title="Demo CodeSandbox - How to Internationalize your application using Intlayer"
|
|
55
|
+
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
|
|
56
|
+
loading="lazy"
|
|
57
|
+
/>
|
|
58
|
+
|
|
59
|
+
</TabItem>
|
|
60
|
+
</Tab>
|
|
61
|
+
|
|
62
|
+
GitHub'da [Uygulama Şablonu](https://github.com/aymericzip/intlayer-vite-vue-template)'na bakın.
|
|
63
|
+
|
|
43
64
|
### Adım 1: Bağımlılıkları Kurma
|
|
44
65
|
|
|
45
66
|
Gerekli paketleri npm kullanarak kurun:
|
|
@@ -533,53 +554,47 @@ Vue uygulamasında yerelleştirilmiş yönlendirme eklemek genellikle yerel ayar
|
|
|
533
554
|
Öncelikle Vue Router'ı kurun:
|
|
534
555
|
|
|
535
556
|
```bash packageManager="npm"
|
|
536
|
-
npm install
|
|
557
|
+
npm install vue-router
|
|
537
558
|
```
|
|
538
559
|
|
|
539
560
|
```bash packageManager="pnpm"
|
|
540
|
-
pnpm add
|
|
561
|
+
pnpm add vue-router
|
|
541
562
|
```
|
|
542
563
|
|
|
543
564
|
```bash packageManager="yarn"
|
|
544
|
-
yarn add
|
|
565
|
+
yarn add vue-router
|
|
545
566
|
```
|
|
546
567
|
|
|
547
568
|
Ardından, yerel ayar tabanlı yönlendirmeyi yöneten bir yönlendirici yapılandırması oluşturun:
|
|
548
569
|
|
|
549
570
|
```js fileName="src/router/index.ts"
|
|
550
571
|
import {
|
|
551
|
-
configuration,
|
|
552
|
-
getPathWithoutLocale,
|
|
553
572
|
localeFlatMap,
|
|
554
|
-
type
|
|
573
|
+
type Locale,
|
|
555
574
|
} from 'intlayer';
|
|
556
575
|
import { createIntlayerClient } from 'vue-intlayer';
|
|
557
576
|
import { createRouter, createWebHistory } from 'vue-router';
|
|
558
577
|
import HomeView from './views/home/HomeView.vue';
|
|
559
578
|
import RootView from './views/root/Root.vue';
|
|
560
579
|
|
|
561
|
-
// Uluslararasılaştırma yapılandırmasını çıkar
|
|
562
|
-
const { internationalization, middleware } = configuration;
|
|
563
|
-
const { defaultLocale } = internationalization;
|
|
564
|
-
|
|
565
580
|
/**
|
|
566
581
|
* Yerel ayar özel yollarla ve meta verilerle rotaları bildirin.
|
|
567
582
|
*/
|
|
568
|
-
const routes = localeFlatMap((
|
|
583
|
+
const routes = localeFlatMap(({ urlPrefix, locale }) => [
|
|
569
584
|
{
|
|
570
|
-
path: `${
|
|
571
|
-
name: `Root-${
|
|
585
|
+
path: `${urlPrefix}/`,
|
|
586
|
+
name: `Root-${locale}`,
|
|
572
587
|
component: RootView,
|
|
573
588
|
meta: {
|
|
574
|
-
locale
|
|
589
|
+
locale,
|
|
575
590
|
},
|
|
576
591
|
},
|
|
577
592
|
{
|
|
578
|
-
path: `${
|
|
579
|
-
name: `Home-${
|
|
593
|
+
path: `${urlPrefix}/home`,
|
|
594
|
+
name: `Home-${locale}`,
|
|
580
595
|
component: HomeView,
|
|
581
596
|
meta: {
|
|
582
|
-
locale
|
|
597
|
+
locale,
|
|
583
598
|
},
|
|
584
599
|
},
|
|
585
600
|
]);
|
|
@@ -594,23 +609,11 @@ export const router = createRouter({
|
|
|
594
609
|
router.beforeEach((to, _from, next) => {
|
|
595
610
|
const client = createIntlayerClient();
|
|
596
611
|
|
|
597
|
-
const metaLocale = to.meta.locale as
|
|
612
|
+
const metaLocale = to.meta.locale as Locale;
|
|
598
613
|
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
next();
|
|
603
|
-
} else {
|
|
604
|
-
// Geri dönüş: meta'da yerel ayar yok, muhtemelen eşleşmeyen rota
|
|
605
|
-
// İsteğe bağlı: 404'ü yönetin veya varsayılan yerel ayara yönlendirin
|
|
606
|
-
client.setLocale(defaultLocale);
|
|
607
|
-
|
|
608
|
-
if (middleware.prefixDefault) {
|
|
609
|
-
next(`/${defaultLocale}${getPathWithoutLocale(to.path)}`);
|
|
610
|
-
} else {
|
|
611
|
-
next(getPathWithoutLocale(to.path));
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
+
// Rota meta'sında tanımlanan yerel ayarı yeniden kullan
|
|
615
|
+
client.setLocale(metaLocale);
|
|
616
|
+
next();
|
|
614
617
|
});
|
|
615
618
|
```
|
|
616
619
|
|
|
@@ -742,7 +745,7 @@ watch(
|
|
|
742
745
|
İpucu: Daha iyi SEO ve erişilebilirlik için, dil özel sayfalarına bağlanmak üzere `<a href="/fr/home" hreflang="fr">` gibi etiketleri kullanın. Bu, arama motorlarının dil özel URL'lerini keşfetmesine ve uygun şekilde indekslemesine olanak tanır. SPA davranışını korumak için varsayılan navigasyonu @click.prevent ile önleyin, useLocale ile yerel ayarı değiştirin ve Vue Router kullanarak programatik olarak gidin.
|
|
743
746
|
|
|
744
747
|
```html
|
|
745
|
-
<ol
|
|
748
|
+
<ol>
|
|
746
749
|
<li>
|
|
747
750
|
<a
|
|
748
751
|
hreflang="x-default"
|
|
@@ -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: 6.5.2
|
|
22
25
|
date: 2025-10-03
|
|
23
26
|
changes: Cập nhật tài liệu
|
|
@@ -50,6 +53,14 @@ Với Intlayer, bạn có thể:
|
|
|
50
53
|
|
|
51
54
|
## Hướng dẫn từng bước để thiết lập Intlayer trong ứng dụng Tanstack Start
|
|
52
55
|
|
|
56
|
+
<Tab defaultTab="video">
|
|
57
|
+
<TabItem label="Video" value="video">
|
|
58
|
+
|
|
59
|
+
<iframe title="Giải pháp i18n tốt nhất cho Tanstack Start? Khám phá Intlayer" class="m-auto aspect-[16/9] w-full overflow-hidden rounded-lg border-0" allow="autoplay; gyroscope;" loading="lazy" width="1080" height="auto" src="https://www.youtube.com/embed/_XTdKVWaeqg?autoplay=0&origin=http://intlayer.org&controls=0&rel=1"/>
|
|
60
|
+
|
|
61
|
+
</TabItem>
|
|
62
|
+
<TabItem label="Code" value="code">
|
|
63
|
+
|
|
53
64
|
<iframe
|
|
54
65
|
src="https://stackblitz.com/github/aymericzip/intlayer-tanstack-start-template?embed=1&ctl=1&file=intlayer.config.ts"
|
|
55
66
|
className="m-auto overflow-hidden rounded-lg border-0 max-md:size-full max-md:h-[700px] md:aspect-16/9 md:w-full"
|
|
@@ -58,6 +69,9 @@ Với Intlayer, bạn có thể:
|
|
|
58
69
|
loading="lazy"
|
|
59
70
|
/>
|
|
60
71
|
|
|
72
|
+
</TabItem>
|
|
73
|
+
</Tab>
|
|
74
|
+
|
|
61
75
|
Xem [Mẫu Ứng dụng](https://github.com/aymericzip/intlayer-tanstack-start-template) trên GitHub.
|
|
62
76
|
|
|
63
77
|
### Bước 1: Tạo Dự Án
|
|
@@ -548,7 +562,55 @@ export const Route = createFileRoute("/{-$locale}/")({
|
|
|
548
562
|
|
|
549
563
|
---
|
|
550
564
|
|
|
551
|
-
###
|
|
565
|
+
### Step 13: Retrieve the locale in your server actions (Optional)
|
|
566
|
+
|
|
567
|
+
You may want to access the current locale from inside your server actions or API endpoints.
|
|
568
|
+
You can do this using the `getLocale` helper from `intlayer`.
|
|
569
|
+
|
|
570
|
+
Here's an example using TanStack Start's server functions:
|
|
571
|
+
|
|
572
|
+
```tsx fileName="src/routes/{-$locale}/index.tsx"
|
|
573
|
+
import { createServerFn } from "@tanstack/react-start";
|
|
574
|
+
import {
|
|
575
|
+
getRequestHeader,
|
|
576
|
+
getRequestHeaders,
|
|
577
|
+
} from "@tanstack/react-start/server";
|
|
578
|
+
import { getCookie, getIntlayer, getLocale } from "intlayer";
|
|
579
|
+
|
|
580
|
+
export const getLocaleServer = createServerFn().handler(async () => {
|
|
581
|
+
const locale = await getLocale({
|
|
582
|
+
// Get the cookie from the request (default: 'INTLAYER_LOCALE')
|
|
583
|
+
getCookie: (name) => {
|
|
584
|
+
const cookieString = getRequestHeader("cookie");
|
|
585
|
+
|
|
586
|
+
return getCookie(name, cookieString);
|
|
587
|
+
},
|
|
588
|
+
// Get the header from the request (default: 'x-intlayer-locale')
|
|
589
|
+
getHeader: (name) => getRequestHeader(name),
|
|
590
|
+
// Fallback using Accept-Language negotiation
|
|
591
|
+
getAllHeaders: async () => {
|
|
592
|
+
const headers = getRequestHeaders();
|
|
593
|
+
const result: Record<string, string> = {};
|
|
594
|
+
|
|
595
|
+
// Convert the TypedHeaders into a plain Record<string, string>
|
|
596
|
+
for (const [key, value] of headers.entries()) {
|
|
597
|
+
result[key] = value;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
return result;
|
|
601
|
+
},
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
// Retrieve some content using getIntlayer()
|
|
605
|
+
const content = getIntlayer("app", locale);
|
|
606
|
+
|
|
607
|
+
return { locale, content };
|
|
608
|
+
});
|
|
609
|
+
```
|
|
610
|
+
|
|
611
|
+
---
|
|
612
|
+
|
|
613
|
+
### Bước 14: Cấu hình TypeScript (Tùy chọn)
|
|
552
614
|
|
|
553
615
|
Intlayer sử dụng module augmentation để tận dụng các lợi ích của TypeScript và làm cho codebase của bạn mạnh mẽ hơn.
|
|
554
616
|
|
|
@@ -42,8 +42,25 @@ Với Intlayer, bạn có thể:
|
|
|
42
42
|
|
|
43
43
|
## Hướng dẫn từng bước để thiết lập Intlayer trong ứng dụng Vite và Vue
|
|
44
44
|
|
|
45
|
+
<Tab defaultTab="video">
|
|
46
|
+
<TabItem label="Video" value="video">
|
|
47
|
+
|
|
48
|
+
<iframe title="The best i18n solution for Vite and Vue? Discover Intlayer" class="m-auto aspect-[16/9] w-full overflow-hidden rounded-lg border-0" allow="autoplay; gyroscope;" loading="lazy" width="1080" height="auto" src="https://www.youtube.com/embed/IE3XWkZ6a5U?autoplay=0&origin=http://intlayer.org&controls=0&rel=1"/>
|
|
49
|
+
|
|
50
|
+
</TabItem>
|
|
51
|
+
<TabItem label="Mã" value="code">
|
|
52
|
+
|
|
45
53
|
<iframe
|
|
46
54
|
src="https://stackblitz.com/github/aymericzip/intlayer-vite-vue-template?embed=1&ctl=1&file=intlayer.config.ts"
|
|
55
|
+
className="m-auto overflow-hidden rounded-lg border-0 max-md:size-full max-md:h-[700px] md:aspect-16/9 md:w-full"
|
|
56
|
+
title="Demo CodeSandbox - How to Internationalize your application using Intlayer"
|
|
57
|
+
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
|
|
58
|
+
loading="lazy"
|
|
59
|
+
/>
|
|
60
|
+
|
|
61
|
+
</TabItem>
|
|
62
|
+
</Tab>
|
|
63
|
+
|
|
47
64
|
Xem [Mẫu Ứng Dụng](https://github.com/aymericzip/intlayer-vite-vue-template) trên GitHub.
|
|
48
65
|
|
|
49
66
|
### Bước 1: Cài Đặt Các Phụ Thuộc
|
|
@@ -567,53 +584,47 @@ Ví dụ:
|
|
|
567
584
|
Trước tiên, cài đặt Vue Router:
|
|
568
585
|
|
|
569
586
|
```bash packageManager="npm"
|
|
570
|
-
npm install
|
|
587
|
+
npm install vue-router
|
|
571
588
|
```
|
|
572
589
|
|
|
573
590
|
```bash packageManager="pnpm"
|
|
574
|
-
pnpm add
|
|
591
|
+
pnpm add vue-router
|
|
575
592
|
```
|
|
576
593
|
|
|
577
594
|
```bash packageManager="yarn"
|
|
578
|
-
yarn add
|
|
595
|
+
yarn add vue-router
|
|
579
596
|
```
|
|
580
597
|
|
|
581
598
|
Sau đó, tạo một cấu hình router xử lý định tuyến dựa trên ngôn ngữ:
|
|
582
599
|
|
|
583
600
|
```js fileName="src/router/index.ts"
|
|
584
601
|
import {
|
|
585
|
-
configuration,
|
|
586
|
-
getPathWithoutLocale,
|
|
587
602
|
localeFlatMap,
|
|
588
|
-
type
|
|
603
|
+
type Locale,
|
|
589
604
|
} from 'intlayer';
|
|
590
605
|
import { createIntlayerClient } from 'vue-intlayer';
|
|
591
606
|
import { createRouter, createWebHistory } from 'vue-router';
|
|
592
607
|
import HomeView from './views/home/HomeView.vue';
|
|
593
608
|
import RootView from './views/root/Root.vue';
|
|
594
609
|
|
|
595
|
-
// Lấy cấu hình quốc tế hóa
|
|
596
|
-
const { internationalization, middleware } = configuration;
|
|
597
|
-
const { defaultLocale } = internationalization;
|
|
598
|
-
|
|
599
610
|
/**
|
|
600
611
|
* Khai báo các route với đường dẫn và metadata theo từng ngôn ngữ.
|
|
601
612
|
*/
|
|
602
|
-
const routes = localeFlatMap((
|
|
613
|
+
const routes = localeFlatMap(({ urlPrefix, locale }) => [
|
|
603
614
|
{
|
|
604
|
-
path: `${
|
|
605
|
-
name: `Root-${
|
|
615
|
+
path: `${urlPrefix}/`,
|
|
616
|
+
name: `Root-${locale}`,
|
|
606
617
|
component: RootView,
|
|
607
618
|
meta: {
|
|
608
|
-
locale
|
|
619
|
+
locale,
|
|
609
620
|
},
|
|
610
621
|
},
|
|
611
622
|
{
|
|
612
|
-
path: `${
|
|
613
|
-
name: `Home-${
|
|
623
|
+
path: `${urlPrefix}/home`,
|
|
624
|
+
name: `Home-${locale}`,
|
|
614
625
|
component: HomeView,
|
|
615
626
|
meta: {
|
|
616
|
-
locale
|
|
627
|
+
locale,
|
|
617
628
|
},
|
|
618
629
|
},
|
|
619
630
|
]);
|
|
@@ -628,23 +639,11 @@ export const router = createRouter({
|
|
|
628
639
|
router.beforeEach((to, _from, next) => {
|
|
629
640
|
const client = createIntlayerClient();
|
|
630
641
|
|
|
631
|
-
const metaLocale = to.meta.locale as
|
|
642
|
+
const metaLocale = to.meta.locale as Locale;
|
|
632
643
|
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
next();
|
|
637
|
-
} else {
|
|
638
|
-
// Trường hợp fallback: không có locale trong meta, có thể là route không khớp
|
|
639
|
-
// Tùy chọn: xử lý 404 hoặc chuyển hướng về locale mặc định
|
|
640
|
-
client.setLocale(defaultLocale);
|
|
641
|
-
|
|
642
|
-
if (middleware.prefixDefault) {
|
|
643
|
-
next(`/${defaultLocale}${getPathWithoutLocale(to.path)}`);
|
|
644
|
-
} else {
|
|
645
|
-
next(getPathWithoutLocale(to.path));
|
|
646
|
-
}
|
|
647
|
-
}
|
|
644
|
+
// Tái sử dụng locale đã định nghĩa trong meta của route
|
|
645
|
+
client.setLocale(metaLocale);
|
|
646
|
+
next();
|
|
648
647
|
});
|
|
649
648
|
```
|
|
650
649
|
|
|
@@ -776,7 +775,7 @@ watch(
|
|
|
776
775
|
Mẹo: Để cải thiện SEO và khả năng truy cập, hãy sử dụng thẻ như `<a href="/fr/home" hreflang="fr">` để liên kết đến các trang đã được địa phương hóa, như được trình bày trong Bước 10. Điều này cho phép các công cụ tìm kiếm phát hiện và lập chỉ mục các URL theo ngôn ngữ một cách chính xác. Để giữ nguyên hành vi SPA, bạn có thể ngăn chặn điều hướng mặc định với @click.prevent, thay đổi locale bằng useLocale, và điều hướng chương trình bằng Vue Router.
|
|
777
776
|
|
|
778
777
|
```html
|
|
779
|
-
<ol
|
|
778
|
+
<ol>
|
|
780
779
|
<li>
|
|
781
780
|
<a
|
|
782
781
|
hreflang="x-default"
|
|
@@ -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 添加支持
|
|
@@ -47,6 +50,27 @@ history:
|
|
|
47
50
|
|
|
48
51
|
## 在 Tanstack Start 应用中设置 Intlayer 的分步指南
|
|
49
52
|
|
|
53
|
+
<Tab defaultTab="video">
|
|
54
|
+
<TabItem label="Video" value="video">
|
|
55
|
+
|
|
56
|
+
<iframe title="Tanstack Start 的最佳 i18n 解决方案?发现 Intlayer" class="m-auto aspect-[16/9] w-full overflow-hidden rounded-lg border-0" allow="autoplay; gyroscope;" loading="lazy" width="1080" height="auto" src="https://www.youtube.com/embed/_XTdKVWaeqg?autoplay=0&origin=http://intlayer.org&controls=0&rel=1"/>
|
|
57
|
+
|
|
58
|
+
</TabItem>
|
|
59
|
+
<TabItem label="Code" value="code">
|
|
60
|
+
|
|
61
|
+
<iframe
|
|
62
|
+
src="https://stackblitz.com/github/aymericzip/intlayer-tanstack-start-template?embed=1&ctl=1&file=intlayer.config.ts"
|
|
63
|
+
className="m-auto overflow-hidden rounded-lg border-0 max-md:size-full max-md:h-[700px] md:aspect-16/9 md:w-full"
|
|
64
|
+
title="Demo CodeSandbox - 如何使用 Intlayer 国际化您的应用程序"
|
|
65
|
+
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
|
|
66
|
+
loading="lazy"
|
|
67
|
+
/>
|
|
68
|
+
|
|
69
|
+
</TabItem>
|
|
70
|
+
</Tab>
|
|
71
|
+
|
|
72
|
+
在 GitHub 上查看[应用程序模板](https://github.com/aymericzip/intlayer-tanstack-start-template)。
|
|
73
|
+
|
|
50
74
|
### 第一步:创建项目
|
|
51
75
|
|
|
52
76
|
首先,按照 TanStack Start 网站上的[新建项目](https://tanstack.com/start/latest/docs/framework/react/quick-start)指南创建一个新的 TanStack Start 项目。
|
|
@@ -529,7 +553,55 @@ export const Route = createFileRoute("/{-$locale}/")({
|
|
|
529
553
|
|
|
530
554
|
---
|
|
531
555
|
|
|
532
|
-
###
|
|
556
|
+
### Step 13: Retrieve the locale in your server actions (Optional)
|
|
557
|
+
|
|
558
|
+
You may want to access the current locale from inside your server actions or API endpoints.
|
|
559
|
+
You can do this using the `getLocale` helper from `intlayer`.
|
|
560
|
+
|
|
561
|
+
Here's an example using TanStack Start's server functions:
|
|
562
|
+
|
|
563
|
+
```tsx fileName="src/routes/{-$locale}/index.tsx"
|
|
564
|
+
import { createServerFn } from "@tanstack/react-start";
|
|
565
|
+
import {
|
|
566
|
+
getRequestHeader,
|
|
567
|
+
getRequestHeaders,
|
|
568
|
+
} from "@tanstack/react-start/server";
|
|
569
|
+
import { getCookie, getIntlayer, getLocale } from "intlayer";
|
|
570
|
+
|
|
571
|
+
export const getLocaleServer = createServerFn().handler(async () => {
|
|
572
|
+
const locale = await getLocale({
|
|
573
|
+
// Get the cookie from the request (default: 'INTLAYER_LOCALE')
|
|
574
|
+
getCookie: (name) => {
|
|
575
|
+
const cookieString = getRequestHeader("cookie");
|
|
576
|
+
|
|
577
|
+
return getCookie(name, cookieString);
|
|
578
|
+
},
|
|
579
|
+
// Get the header from the request (default: 'x-intlayer-locale')
|
|
580
|
+
getHeader: (name) => getRequestHeader(name),
|
|
581
|
+
// Fallback using Accept-Language negotiation
|
|
582
|
+
getAllHeaders: async () => {
|
|
583
|
+
const headers = getRequestHeaders();
|
|
584
|
+
const result: Record<string, string> = {};
|
|
585
|
+
|
|
586
|
+
// Convert the TypedHeaders into a plain Record<string, string>
|
|
587
|
+
for (const [key, value] of headers.entries()) {
|
|
588
|
+
result[key] = value;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
return result;
|
|
592
|
+
},
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
// Retrieve some content using getIntlayer()
|
|
596
|
+
const content = getIntlayer("app", locale);
|
|
597
|
+
|
|
598
|
+
return { locale, content };
|
|
599
|
+
});
|
|
600
|
+
```
|
|
601
|
+
|
|
602
|
+
---
|
|
603
|
+
|
|
604
|
+
### 第14步:配置 TypeScript(可选)
|
|
533
605
|
|
|
534
606
|
Intlayer 使用模块增强来利用 TypeScript 的优势,使您的代码库更健壮。
|
|
535
607
|
|
|
@@ -40,6 +40,27 @@ history:
|
|
|
40
40
|
|
|
41
41
|
## 在 Vite 和 Vue 应用中设置 Intlayer 的分步指南
|
|
42
42
|
|
|
43
|
+
<Tab defaultTab="video">
|
|
44
|
+
<TabItem label="视频" value="video">
|
|
45
|
+
|
|
46
|
+
<iframe title="The best i18n solution for Vite and Vue? Discover Intlayer" class="m-auto aspect-[16/9] w-full overflow-hidden rounded-lg border-0" allow="autoplay; gyroscope;" loading="lazy" width="1080" height="auto" src="https://www.youtube.com/embed/IE3XWkZ6a5U?autoplay=0&origin=http://intlayer.org&controls=0&rel=1"/>
|
|
47
|
+
|
|
48
|
+
</TabItem>
|
|
49
|
+
<TabItem label="代码" value="code">
|
|
50
|
+
|
|
51
|
+
<iframe
|
|
52
|
+
src="https://stackblitz.com/github/aymericzip/intlayer-vite-vue-template?embed=1&ctl=1&file=intlayer.config.ts"
|
|
53
|
+
className="m-auto overflow-hidden rounded-lg border-0 max-md:size-full max-md:h-[700px] md:aspect-16/9 md:w-full"
|
|
54
|
+
title="Demo CodeSandbox - How to Internationalize your application using Intlayer"
|
|
55
|
+
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
|
|
56
|
+
loading="lazy"
|
|
57
|
+
/>
|
|
58
|
+
|
|
59
|
+
</TabItem>
|
|
60
|
+
</Tab>
|
|
61
|
+
|
|
62
|
+
请参阅 GitHub 上的[应用模板](https://github.com/aymericzip/intlayer-vite-vue-template)。
|
|
63
|
+
|
|
43
64
|
### 第一步:安装依赖
|
|
44
65
|
|
|
45
66
|
使用 npm 安装所需的包:
|
|
@@ -566,53 +587,47 @@ const content = useIntlayer("app"); // 创建相关的 intlayer 声明文件
|
|
|
566
587
|
首先,安装 Vue Router:
|
|
567
588
|
|
|
568
589
|
```bash packageManager="npm"
|
|
569
|
-
npm install
|
|
590
|
+
npm install vue-router
|
|
570
591
|
```
|
|
571
592
|
|
|
572
593
|
```bash packageManager="pnpm"
|
|
573
|
-
pnpm add
|
|
594
|
+
pnpm add vue-router
|
|
574
595
|
```
|
|
575
596
|
|
|
576
597
|
```bash packageManager="yarn"
|
|
577
|
-
yarn add
|
|
598
|
+
yarn add vue-router
|
|
578
599
|
```
|
|
579
600
|
|
|
580
601
|
然后,创建一个处理基于语言环境路由的路由配置:
|
|
581
602
|
|
|
582
603
|
```js fileName="src/router/index.ts"
|
|
583
604
|
import {
|
|
584
|
-
configuration,
|
|
585
|
-
getPathWithoutLocale,
|
|
586
605
|
localeFlatMap,
|
|
587
|
-
type
|
|
606
|
+
type Locale,
|
|
588
607
|
} from 'intlayer';
|
|
589
608
|
import { createIntlayerClient } from 'vue-intlayer';
|
|
590
609
|
import { createRouter, createWebHistory } from 'vue-router';
|
|
591
610
|
import HomeView from './views/home/HomeView.vue';
|
|
592
611
|
import RootView from './views/root/Root.vue';
|
|
593
612
|
|
|
594
|
-
// 获取国际化配置
|
|
595
|
-
const { internationalization, middleware } = configuration;
|
|
596
|
-
const { defaultLocale } = internationalization;
|
|
597
|
-
|
|
598
613
|
/**
|
|
599
614
|
* 声明带有语言环境特定路径和元数据的路由。
|
|
600
615
|
*/
|
|
601
|
-
const routes = localeFlatMap((
|
|
616
|
+
const routes = localeFlatMap(({ urlPrefix, locale }) => [
|
|
602
617
|
{
|
|
603
|
-
path: `${
|
|
604
|
-
name: `Root-${
|
|
618
|
+
path: `${urlPrefix}/`,
|
|
619
|
+
name: `Root-${locale}`,
|
|
605
620
|
component: RootView,
|
|
606
621
|
meta: {
|
|
607
|
-
locale
|
|
622
|
+
locale,
|
|
608
623
|
},
|
|
609
624
|
},
|
|
610
625
|
{
|
|
611
|
-
path: `${
|
|
612
|
-
name: `Home-${
|
|
626
|
+
path: `${urlPrefix}/home`,
|
|
627
|
+
name: `Home-${locale}`,
|
|
613
628
|
component: HomeView,
|
|
614
629
|
meta: {
|
|
615
|
-
locale
|
|
630
|
+
locale,
|
|
616
631
|
},
|
|
617
632
|
},
|
|
618
633
|
]);
|
|
@@ -627,23 +642,11 @@ export const router = createRouter({
|
|
|
627
642
|
router.beforeEach((to, _from, next) => {
|
|
628
643
|
const client = createIntlayerClient();
|
|
629
644
|
|
|
630
|
-
const metaLocale = to.meta.locale as
|
|
645
|
+
const metaLocale = to.meta.locale as Locale;
|
|
631
646
|
|
|
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
|
-
}
|
|
647
|
+
// 重用路由元信息中定义的语言环境
|
|
648
|
+
client.setLocale(metaLocale);
|
|
649
|
+
next();
|
|
647
650
|
});
|
|
648
651
|
```
|
|
649
652
|
|
|
@@ -773,7 +776,7 @@ watch(
|
|
|
773
776
|
提示:为了更好的 SEO 和可访问性,使用如 `<a href="/fr/home" hreflang="fr">` 这样的标签来链接到本地化页面,如步骤10所示。这允许搜索引擎正确发现和索引特定语言的 URL。为了保持 SPA 行为,可以通过 @click.prevent 阻止默认导航,使用 useLocale 更改语言环境,并通过 Vue Router 编程式导航。
|
|
774
777
|
|
|
775
778
|
```html
|
|
776
|
-
<ol
|
|
779
|
+
<ol>
|
|
777
780
|
<li>
|
|
778
781
|
<a
|
|
779
782
|
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.10",
|
|
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.10",
|
|
77
|
+
"@intlayer/core": "7.3.10",
|
|
78
|
+
"@intlayer/types": "7.3.10"
|
|
79
79
|
},
|
|
80
80
|
"devDependencies": {
|
|
81
|
-
"@intlayer/api": "7.3.
|
|
82
|
-
"@intlayer/cli": "7.3.
|
|
81
|
+
"@intlayer/api": "7.3.10",
|
|
82
|
+
"@intlayer/cli": "7.3.10",
|
|
83
83
|
"@types/node": "24.10.1",
|
|
84
84
|
"@utils/ts-config": "1.0.4",
|
|
85
85
|
"@utils/ts-config-types": "1.0.4",
|