{{ description }}
60 |7 | {{ $t("error.description") }} 8 |
9 |404
17 |25 | {{ $t("errorPage.message") }} 26 |
27 |{{ description }}
60 |71 | {{ data?.description }} 72 |
73 |96 | {{ $t("homePage.sitemap.sitemap") }} 97 |
98 |
16 |
17 |
18 |
21 | An opionionated Nuxt.js Template with Nuxt Content!
22 |
23 | View Demo »
24 |
25 |
26 | Explore the docs
27 | ·
28 | Report Bug
29 | ·
30 | Request Feature
31 |
{{ $t("welcome") }}
400 | 401 | ``` 402 | 403 | With this in place, Nuxt is smart enough to render out "Welcome" or "Bienvenue" correctly when the language context changed. 404 | 405 | ### Locales File Definition 406 | 407 | While the above solution of adding new definition of words in the `nuxt.config.ts` file works, it can pose a real problem when the **vocabulary grows** as the file become cumbersome to maintain. 408 | 409 | Fortunately, there is another preferred way to store the language definitions in their own, separate JSON file. With this approach, not only it achieves the Single Responsibility Principle, it also drastically improve the maintainability of the files. 410 | 411 | Here is how it is configured in `nuxt.config.ts`. 412 | 413 | ```ts [nuxt.config.ts] 414 | export default defineNuxtConfig({ 415 | i18n: { 416 | langDir: "locales", 417 | locales: [ 418 | { 419 | code: "en", 420 | file: "en.json", 421 | }, 422 | { 423 | code: "fr", 424 | file: "fr.json", 425 | }, 426 | ], 427 | }, 428 | }); 429 | ``` 430 | 431 | The above code tells Nuxt to locate English definition in `en.json` file and French definition in `fr.json` file inside the `locales` folder. 432 | 433 | ### I18n in Nuxt Content 434 | 435 | To support internationalization for Markdown based contents from Nuxt Content, create a corresponding folder inside the `content` folder with the non-default locale's code and imitate the structure of the base folder. 436 | 437 | For example, given I have the following file structure that has English contents, the French contents can be housed in the following manner. 438 | 439 | From: 440 | 441 | ```[Directory Structure] 442 | ├─ content 443 | │ ├─ blogs 444 | │ │ ├─ blog1.md 445 | │ │ └─ blog2.md 446 | │ ├─ demo.md 447 | │ └─ guide.md 448 | ``` 449 | 450 | To: 451 | 452 | ```[Directory Structure] 453 | ├─ content 454 | │ ├─ blogs 455 | │ │ ├─ blog1.md (English) 456 | │ │ └─ blog2.md (English) 457 | │ ├─ fr 458 | │ │ ├─ blogs 459 | │ │ │ ├─ blog1.md (French) 460 | │ │ │ └─ blog2.md (French) 461 | │ │ ├─ demo.md (French) 462 | │ │ └─ guide.md (French) 463 | │ ├─ demo.md (English) 464 | │ └─ guide.md (English) 465 | ``` 466 | 467 | By doing this, we are utilizing the behaviour of the prefixed URL for non-default locale and it does the trick. Not the most elegant solution but it works for now. 468 | 469 | ### Internationalized Links 470 | 471 | To make sure that every links in the website corresponds to its language counterparts, we have to preprocess the links with the `useLocalePath` composable. Here is how it looks like in code. 472 | 473 | ```vue 474 | 477 | 478 | 479 |{{ $t("welcome") }}
402 | 403 | ``` 404 | 405 | Avec cela en place, Nuxt est assez intelligent pour restituer correctement "Bienvenue" ou "Bienvenue" lorsque le contexte de la langue a changé. 406 | 407 | ### Définition du fichier de paramètres régionaux 408 | 409 | Bien que la solution ci-dessus consistant à ajouter une nouvelle définition de mots dans le fichier `nuxt.config.ts` fonctionne, elle peut poser un réel problème lorsque le **vocabulaire s'agrandit** à mesure que le fichier devient lourd à maintenir. 410 | 411 | Heureusement, il existe un autre moyen préféré de stocker les définitions de langage dans leur propre fichier JSON séparé. Avec cette approche, non seulement il atteint le principe de responsabilité unique, mais il améliore également considérablement la maintenabilité des fichiers. 412 | 413 | Voici comment il est configuré dans `nuxt.config.ts`. 414 | 415 | ```ts [nuxt.config.ts] 416 | export default defineNuxtConfig({ 417 | i18n: { 418 | langDir: "locales", 419 | locales: [ 420 | { 421 | code: "en", 422 | file: "en.json", 423 | }, 424 | { 425 | code: "fr", 426 | file: "fr.json", 427 | }, 428 | ], 429 | }, 430 | }); 431 | ``` 432 | 433 | Le code ci-dessus indique à Nuxt de localiser la définition anglaise dans le fichier "en.json" et la définition française dans le fichier "fr.json" dans le dossier "locales". 434 | 435 | ### I18n dans le contenu Nuxt 436 | 437 | Pour prendre en charge l'internationalisation des contenus basés sur Markdown à partir de Nuxt Content, créez un dossier correspondant dans le dossier `content` avec le code de la locale non par défaut et imitez la structure du dossier de base. 438 | 439 | Par exemple, étant donné que j'ai la structure de fichiers suivante qui a un contenu en anglais, le contenu en français peut être hébergé de la manière suivante. 440 | 441 | De: 442 | 443 | ```[Structure du répertoire] 444 | ├─ content 445 | │ ├─ blogs 446 | │ │ ├─ blog1.md 447 | │ │ └─ blog2.md 448 | │ ├─ demo.md 449 | │ └─ guide.md 450 | ``` 451 | 452 | En: 453 | 454 | ```[Structure du répertoire] 455 | ├─ content 456 | │ ├─ blogs 457 | │ │ ├─ blog1.md (Anglais) 458 | │ │ └─ blog2.md (Anglais) 459 | │ ├─ fr 460 | │ │ ├─ blogs 461 | │ │ │ ├─ blog1.md (Français) 462 | │ │ │ └─ blog2.md (Français) 463 | │ │ ├─ demo.md (Français) 464 | │ │ └─ guide.md (Français) 465 | │ ├─ demo.md (Anglais) 466 | │ └─ guide.md (Anglais) 467 | ``` 468 | 469 | En faisant cela, nous utilisons le comportement de l'URL préfixée pour les paramètres régionaux non par défaut et cela fait l'affaire. Ce n'est pas la solution la plus élégante mais ça marche pour l'instant. 470 | 471 | ### Liens internationalisés 472 | 473 | Pour nous assurer que chaque lien du site Web correspond à ses homologues linguistiques, nous devons prétraiter les liens avec le composable `useLocalePath`. Voici à quoi cela ressemble dans le code. 474 | 475 | ```vue 476 | 479 | 480 | 481 |