@@ -358,7 +358,7 @@ app.mount("#app");
358
358
359
359
Access your content dictionaries throughout your application by creating a main Vue component and using the Intlayer composables:
360
360
361
- ``` vue fileName="src/App.vue" codeFormat="vue"
361
+ ``` vue fileName="src/App.vue"
362
362
<script setup>
363
363
import { useIntlayer } from "vue-intlayer";
364
364
import HelloWorld from "./components/HelloWorld.vue";
@@ -413,7 +413,7 @@ useI18nHTMLAttributes();
413
413
414
414
To use Intlayer in your Vue application, you need to register the plugin in your main file:
415
415
416
- ```js fileName="src/main.js" codeFormat="javascript "
416
+ ```js fileName="src/main.ts "
417
417
import { createApp } from "vue";
418
418
import App from "./App.vue";
419
419
import "./style.css";
@@ -430,7 +430,7 @@ To change the language of your content, you can use the `setLocale` function pro
430
430
431
431
Create a component to switch between languages:
432
432
433
- ``` vue fileName="src/components/LocaleSwitcher.vue" codeFormat="vue"
433
+ ``` vue fileName="src/components/LocaleSwitcher.vue"
434
434
<template>
435
435
<div class="locale-switcher">
436
436
<select v-model="selectedLocale" @change="changeLocale">
@@ -480,7 +480,7 @@ select {
480
480
481
481
Then, use this component in your App.vue:
482
482
483
- ``` vue fileName="src/App.vue" codeFormat="vue"
483
+ ``` vue fileName="src/App.vue"
484
484
<script setup>
485
485
import { useIntlayer } from "vue-intlayer";
486
486
import HelloWorld from "./components/HelloWorld.vue";
@@ -533,7 +533,7 @@ Adding localized routing in a Vue application typically involves using Vue Route
533
533
534
534
Example:
535
535
536
- ```
536
+ ``` plaintext
537
537
- https://example.com/about
538
538
- https://example.com/es/about
539
539
- https://example.com/fr/about
@@ -547,104 +547,74 @@ npm install vue-router
547
547
548
548
Then, create a router configuration that handles locale-based routing:
549
549
550
- ``` js fileName="src/router/index.js" codeFormat="javascript"
551
- import { createRouter , createWebHistory } from " vue-router" ;
552
- import { configuration , getPathWithoutLocale } from " intlayer" ;
553
- import HomeView from " ../views/HomeView.vue" ;
554
- import AboutView from " ../views/AboutView.vue" ;
550
+ ``` js fileName="src/router/index.ts"
551
+ import {
552
+ configuration ,
553
+ getPathWithoutLocale ,
554
+ localeMapper ,
555
+ Locales ,
556
+ } from ' intlayer' ;
557
+ import { createIntlayerClient } from ' vue-intlayer' ;
558
+ import { createRouter , createWebHistory } from ' vue-router' ;
559
+ import HomeView from ' ./views/home/HomeView.vue' ;
560
+ import RootView from ' ./views/root/Root.vue' ;
555
561
556
562
// Get internationalization configuration
557
563
const { internationalization , middleware } = configuration;
558
- const { locales , defaultLocale } = internationalization;
564
+ const { defaultLocale } = internationalization;
559
565
560
- // Define your routes without locale prefixes
561
- const routes = [
566
+ const routes = localeMapper ((localizedData ) => [
562
567
{
563
- path: " /" ,
564
- name: " Home" ,
565
- component: HomeView,
568
+ path: ` /${ localizedData .urlPrefix } /` ,
569
+ name: ` ${ localizedData .locale } -Root` ,
570
+ component: RootView,
571
+ meta: {
572
+ locale: localizedData .locale ,
573
+ },
566
574
},
567
575
{
568
- path: " /about" ,
569
- name: " About" ,
570
- component: AboutView,
576
+ path: ` /${ localizedData .urlPrefix } /home` ,
577
+ name: ` ${ localizedData .locale } -Home` ,
578
+ component: HomeView,
579
+ meta: {
580
+ locale: localizedData .locale ,
581
+ },
571
582
},
572
- ];
573
-
574
- // Create a function to generate localized routes
575
- const generateLocalizedRoutes = () => {
576
- let allRoutes = [];
577
-
578
- // Add locale prefix to all routes except for default locale when prefixDefault is false
579
- locales .forEach ((locale ) => {
580
- // Skip default locale if prefixDefault is false
581
- if (! middleware .prefixDefault && locale === defaultLocale) {
582
- return ;
583
- }
584
-
585
- const localizedRoutes = routes .map ((route ) => {
586
- return {
587
- ... route,
588
- path: ` /${ locale}${ route .path } ` ,
589
- meta: {
590
- ... route .meta ,
591
- locale,
592
- },
593
- };
594
- });
595
-
596
- allRoutes = [... allRoutes, ... localizedRoutes];
597
- });
598
-
599
- // If default locale is not prefixed, add routes without locale prefix
600
- if (! middleware .prefixDefault ) {
601
- allRoutes = [
602
- ... allRoutes,
603
- ... routes .map ((route ) => ({
604
- ... route,
605
- meta: {
606
- ... route .meta ,
607
- locale: defaultLocale,
608
- },
609
- })),
610
- ];
611
- }
612
-
613
- return allRoutes;
614
- };
583
+ ]);
615
584
616
585
// Create the router instance
617
- const router = createRouter ({
586
+ export const router = createRouter ({
618
587
history: createWebHistory (),
619
- routes: generateLocalizedRoutes (),
588
+ routes: routes . flat (),
620
589
});
621
590
622
591
// Add navigation guard for locale handling
623
- router .beforeEach ((to , from , next ) => {
624
- // Extract locale from the path or use default
625
- const pathSegments = to .path .split (" /" ).filter (Boolean );
626
- const localeFromPath = pathSegments[0 ];
627
-
628
- // Check if the path starts with a valid locale
629
- if (locales .includes (localeFromPath)) {
630
- // Valid locale in URL, proceed
631
- next ();
632
- } else if (! middleware .prefixDefault ) {
633
- // No locale in URL, and we don't prefix default - assume default locale
592
+ router .beforeEach ((to , _from , next ) => {
593
+ const client = createIntlayerClient ();
594
+
595
+ const metaLocale = to .meta .locale as Locales | undefined ;
596
+
597
+ if (metaLocale) {
598
+ // Reuse the locale defined in the route meta
599
+ client .setLocale (metaLocale);
634
600
next ();
635
601
} else {
636
- // No valid locale, redirect to the path with default locale
637
- const pathWithoutLocale = getPathWithoutLocale (to .path );
638
- next (` /${ defaultLocale}${ pathWithoutLocale}${ to .search } ` );
602
+ // Fallback: no locale in meta, possibly unmatched route
603
+ // Optional: handle 404 or redirect to default locale
604
+ client .setLocale (defaultLocale);
605
+
606
+ if (middleware .prefixDefault ) {
607
+ next (` /${ defaultLocale}${ getPathWithoutLocale (to .path )} ` );
608
+ } else {
609
+ next (getPathWithoutLocale (to .path ));
610
+ }
639
611
}
640
612
});
641
-
642
- export default router ;
643
613
```
644
614
645
615
Then, register the router in your main.js file:
646
616
647
- ``` js fileName="src/main.js" codeFormat="javascript "
617
+ ``` js fileName="src/main.ts "
648
618
import { createApp } from " vue" ;
649
619
import App from " ./App.vue" ;
650
620
import router from " ./router" ;
@@ -663,7 +633,7 @@ app.mount("#app");
663
633
664
634
To automatically update the URL when the user changes the language, you can modify the ` LocaleSwitcher ` component to use Vue Router:
665
635
666
- ``` vue fileName="src/components/LocaleSwitcher.vue" codeFormat="vue"
636
+ ``` vue fileName="src/components/LocaleSwitcher.vue"
667
637
<template>
668
638
<div class="locale-switcher">
669
639
<select v-model="selectedLocale" @change="changeLocale">
@@ -723,7 +693,7 @@ watch(
723
693
724
694
When your application supports multiple languages, it's important to update the ` <html> ` tag's ` lang ` and ` dir ` attributes to match the current locale:
725
695
726
- ``` js fileName="src/composables/useI18nHTMLAttributes.js" codeFormat="javascript "
696
+ ``` js fileName="src/composables/useI18nHTMLAttributes.ts "
727
697
import { watch } from " vue" ;
728
698
import { useLocale } from " vue-intlayer" ;
729
699
import { getHTMLTextDir } from " intlayer" ;
@@ -760,7 +730,7 @@ export function useI18nHTMLAttributes() {
760
730
761
731
Use this composable in your App.vue or a global component:
762
732
763
- ``` vue fileName="src/App.vue" codeFormat="vue"
733
+ ``` vue fileName="src/App.vue"
764
734
<script setup>
765
735
import { ref } from "vue";
766
736
import { useIntlayer } from "vue-intlayer";
@@ -783,7 +753,7 @@ const count = ref(0);
783
753
784
754
When working with internationalized applications, it's helpful to have a component that automatically creates links with the correct locale prefix:
785
755
786
- ``` vue fileName="src/components/LocalizedLink.vue" codeFormat="vue"
756
+ ``` vue fileName="src/components/LocalizedLink.vue"
787
757
<template>
788
758
<a :href="localizedHref" v-bind="$attrs">
789
759
<slot></slot>
@@ -816,7 +786,7 @@ const localizedHref = computed(() =>
816
786
817
787
For use with Vue Router, create a router-specific version:
818
788
819
- ``` vue fileName="src/components/LocalizedRouterLink.vue" codeFormat="vue"
789
+ ``` vue fileName="src/components/LocalizedRouterLink.vue"
820
790
<template>
821
791
<router-link :to="localizedTo" v-bind="$attrs">
822
792
<slot></slot>
0 commit comments