@@ -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";
@@ -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" ;
@@ -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" ;
0 commit comments