Skip to content

Commit bafe64d

Browse files
authored
Merge pull request #50 from CreoWis/issue-42-add-language-support
Implement Multi Language Support with Next.js and Next-Intl Plugin
2 parents ea04354 + 814ba92 commit bafe64d

File tree

16 files changed

+463
-148
lines changed

16 files changed

+463
-148
lines changed

README.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,63 @@ Say goodbye to setup headaches and hello to consistent code quality. Elevate you
3636
- **Prettier Plugin Tailwindcss**: Automatic sorting of tailwind classnames using the official prettier plugin.
3737
- **Prettier Plugin Sort Imports**: Organize import declarations alphabetically within groups, which can help improve readability when working on larger projects.
3838
- **Husky**: Ensure code quality and prevent bad commits with pre-commit hooks powered by Husky.
39+
- **Internationalization (i18n)**: Built-in support for multiple languages using next-intl, making it easy to create multilingual applications with locale-specific routing and translations.
40+
41+
## Internationalization (i18n)
42+
NextJsLaunchpad comes with built-in internationalization support using next-intl. This integration provides:
3943

40-
## Getting Started
44+
- Route-based locale handling with `/[locale]/` directory structure
45+
- Easy-to-use translation hooks with `useTranslations` in server and client components.
46+
47+
Translation files are located in:
48+
```bash
49+
content/
50+
├── en.json
51+
├── fr.json
52+
└── [other-locales].json
53+
```
54+
#### How to add a new language support:
55+
56+
To add a new language, we have to add the language JSON file to the content directory, which is in the root directory, that is our first step.
57+
58+
After that, we have to add the newly added language to the locales array in the navigation.ts file. Below is the content of the navigation.ts file, where we need to add the newly added language to the locales array:
59+
```bash
60+
import {defineRouting} from 'next-intl/routing';
61+
import {createNavigation} from 'next-intl/navigation';
62+
63+
export const routing = defineRouting({
64+
// A list of all locales that are supported
65+
locales: ['en', 'fr', 'newLanguage'], // Add the new language code here
66+
67+
// Used when no locale matches
68+
defaultLocale: 'en'
69+
});
70+
71+
export const {Link, redirect, usePathname, useRouter, getPathname} =
72+
createNavigation(routing);
73+
```
74+
#### Using Strings from Language Files
75+
76+
To use strings from a language file in both **client and server** components, use the `useTranslations` hook.
77+
78+
#### Steps:
4179

80+
1. **Import `useTranslations`:**
81+
82+
```javascript
83+
import { useTranslations } from 'next-intl';
84+
```
85+
2. **Initialize useTranslations with a section:**
86+
```javascript
87+
const t = useTranslations('Home');
88+
```
89+
3. **Fetch and use translations:**
90+
91+
```javascript
92+
<h1>{t('welcomeMessage')}</h1>
93+
```
94+
95+
## Getting Started
4296
> Usage
4397
4498
```bash

content/en.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"Home": {
3+
"title": "NextJS Launchpad",
4+
"description": "We created a boilerplate with essential tools and best practices so you can focus on building your NextJS project.",
5+
"edit_prompt": "Edit",
6+
"edit_file": "app/page.tsx",
7+
"edit_to_start": "to start",
8+
"tech_stack": {
9+
"nextjs": {
10+
"name": "Next JS",
11+
"description": "Utilize the power of Next.js for server-rendered React applications"
12+
},
13+
"typescript": {
14+
"name": "TypeScript",
15+
"description": "Enhance your development experience with TypeScript's static typing"
16+
},
17+
"tailwindcss": {
18+
"name": "Tailwind CSS",
19+
"description": "Rapidly build custom designs with Tailwind CSS's utility-first approach"
20+
},
21+
"eslint": {
22+
"name": "ESLint",
23+
"description": "Enforce code quality standards and catch errors early with ESLint"
24+
},
25+
"prettier": {
26+
"name": "Prettier",
27+
"description": "Keep your codebase clean and consistent with automatic code formatting using Prettier."
28+
},
29+
"prettier_plugin_tailwindcss": {
30+
"name": "Prettier Plugin Tailwindcss",
31+
"description": "Automatic sorting of tailwind classnames using the official prettier plugin"
32+
},
33+
"prettier_plugin_sort_imports": {
34+
"name": "Prettier Plugin Sort Imports",
35+
"description": "Sort imports alphabetically for better readability, especially in larger projects"
36+
},
37+
"husky": {
38+
"name": "Husky",
39+
"description": "Ensure code quality and prevent bad commits with pre-commit hooks powered by Husky"
40+
}
41+
},
42+
"footer_text": "An Open Source initiative from CreoWis Technologies."
43+
},
44+
"NotFound": {
45+
"404": "404",
46+
"dosen't_exist_text": "Oops! The page you're looking for doesn't exist.",
47+
"go_to_home": "Go back to Home"
48+
}
49+
}

content/fr.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"Home": {
3+
"title": "NextJS Launchpad",
4+
"description": "Nous avons créé un modèle avec les outils essentiels et les meilleures pratiques pour que vous puissiez vous concentrer sur le développement de votre projet NextJS.",
5+
"edit_prompt": "Modifier",
6+
"edit_file": "app/page.tsx",
7+
"edit_to_start": "pour commencer",
8+
"tech_stack": {
9+
"nextjs": {
10+
"name": "Next JS",
11+
"description": "Exploitez la puissance de Next.js pour des applications React rendues côté serveur"
12+
},
13+
"typescript": {
14+
"name": "TypeScript",
15+
"description": "Améliorez votre expérience de développement grâce au typage statique de TypeScript"
16+
},
17+
"tailwindcss": {
18+
"name": "Tailwind CSS",
19+
"description": "Créez rapidement des designs personnalisés avec l'approche utilitaire de Tailwind CSS"
20+
},
21+
"eslint": {
22+
"name": "ESLint",
23+
"description": "Appliquez des normes de qualité de code et détectez les erreurs tôt avec ESLint"
24+
},
25+
"prettier": {
26+
"name": "Prettier",
27+
"description": "Gardez votre code propre et cohérent avec le formatage automatique de Prettier"
28+
},
29+
"prettier_plugin_tailwindcss": {
30+
"name": "Plugin Prettier Tailwindcss",
31+
"description": "Tri automatique des classes Tailwind avec le plugin officiel Prettier"
32+
},
33+
"prettier_plugin_sort_imports": {
34+
"name": "Plugin Prettier Tri des Imports",
35+
"description": "Triez les imports par ordre alphabétique pour une meilleure lisibilité, en particulier dans les grands projets"
36+
},
37+
"husky": {
38+
"name": "Husky",
39+
"description": "Assurez la qualité du code et empêchez les mauvais commits avec les hooks pré-commit de Husky"
40+
}
41+
},
42+
"footer_text": "Une initiative Open Source de CreoWis Technologies."
43+
},
44+
"NotFound": {
45+
"404": "404",
46+
"dosent_exist_text": "Oups! La page que vous cherchez n'existe pas.",
47+
"go_to_home": "Retour à l'accueil"
48+
}
49+
}

next.config.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
/** @type {import('next').NextConfig} */
2+
import createNextIntlPlugin from 'next-intl/plugin';
3+
4+
const withNextIntl = createNextIntlPlugin();
5+
26
const nextConfig = {
37
reactStrictMode: true,
48
swcMinify: true,
59
productionBrowserSourceMaps: true,
610
};
711

8-
export default nextConfig;
12+
export default withNextIntl(nextConfig);

package-lock.json

Lines changed: 113 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
},
1919
"dependencies": {
2020
"next": "15.1.4",
21+
"next-intl": "^3.26.3",
2122
"react": "19.0.0",
2223
"react-dom": "19.0.0"
2324
},
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from 'react';
2+
import Link from 'next/link';
3+
import { useTranslations } from 'next-intl';
4+
5+
const NotFound = () => {
6+
const t = useTranslations('NotFound');
7+
8+
return (
9+
<div className="flex min-h-screen flex-col items-center justify-center bg-gray-100 text-gray-800">
10+
<h1 className="mb-4 text-6xl font-bold">{t('404')} </h1>
11+
<p className="mb-4 text-xl"> {t(`dosen't_exist_text`)} </p>
12+
<Link
13+
href="/"
14+
className="mt-4 rounded-lg bg-blue-700 px-6 py-3 text-white transition duration-300 hover:bg-blue-800"
15+
>
16+
{t('go_to_home')}
17+
</Link>
18+
</div>
19+
);
20+
};
21+
22+
export default NotFound;

0 commit comments

Comments
 (0)