@@ -11,7 +11,16 @@ Node express middleware for detecting requested language based on:
11114 . Default language code
1212Request will be modified such that URI does not start with language code and new attribute ` lang ` gets added to the request object.
1313
14- ## Express Example
14+ ## Request object after LanguageRouter
15+ * ` req.lang ` - requested language code e.g. 'en'
16+ * ` req.langs ` - list of all supported language codes (based on found translation files in ` translations ` dir or ` next.config.js ` ).
17+ * ` req.TEXT ` - object with all translations for the requested language code (Disabled by default!).
18+ * ` req.PATH ` - update URI path without language code prefix.
19+
20+
21+ ## Examples
22+
23+ ### Express
1524` server.js `
1625``` javascript
1726const express = require (' express' );
@@ -24,7 +33,7 @@ const app = express();
2433// Here parameter 'languages' is a list of language codes that
2534// your app will accept.
2635app .use (LanguageRouter ({
27- languages : [ ' en' , ' de ' ]
36+ defaultLanguage : ' en' ,
2837}));
2938
3039// Create your custom routes and retriev request language code
@@ -40,7 +49,76 @@ app.listen(80, function(){
4049```
4150
4251
43- ## Next.js with Express Example
52+ ### Next.js ≥13 Page
53+ ` app/[locale]/translations.ts `
54+ ``` typescript
55+ ' use server' ;
56+ import " server-only" ;
57+ import { getTranslations } from " lup-language" ;
58+
59+ export default async function loadTranslations(locale : string , translationKeys : string []): Promise <{[key : string ]: string }> {
60+ return await getTranslations (locale , ' en' , translationKeys ); // second argument is default locale
61+ };
62+ ```
63+
64+ ` app/[locale]/layout.tsx `
65+ ``` typescript
66+ export async function generateStaticParams(context : StaticParamsContext ){
67+ const locales = await getLocales ();
68+ return locales .map ((locale ) => ({ locale: locale }));
69+ }
70+ ```
71+
72+ ` app/[locale]/page.tsx `
73+ ``` typescript
74+ import React from " react" ;
75+ import styles from ' ./page.module.css' ;
76+ import loadTranslations from " ../translations" ;
77+ import { StaticParamsContext } from " ../../../services/Types.service" ;
78+
79+ export default async function Home({ params }: StaticParamsContext ) {
80+ const locale = params .locale ;
81+ const TEXT = await loadTranslations (locale , [' HelloWorld' ]);
82+
83+ return <>
84+ <b >{TEXT[' HelloWorld' ]}</b>
85+ </>
86+ }
87+ ```
88+
89+ Optional if unsupported languages or root should be redirected:
90+ ` middleware.ts `
91+ ` ` ` typescript
92+ import { NextResponse } from "next/server";
93+ import type { NextRequest } from "next/server";
94+ import { LanguageRouter } from "lup-language";
95+
96+ // Settings
97+ const lupLang = LanguageRouter({
98+ defaultLang: 'en',
99+ });
100+
101+ export function middleware(request: NextRequest): NextResponse {
102+
103+ // Redirect to correct language
104+ const langInfo = lupLang.nextJsMiddlewareHandler(request);
105+ if(langInfo.redirect || langInfo.cookie){
106+ const langResponse = langInfo.redirect ? NextResponse.redirect(langInfo.redirect, { status: langInfo.redirectResponseCode }) : NextResponse.next();
107+ if(langInfo.cookie){
108+ langResponse.cookies.set(langInfo.name, langInfo.cookie.value, langInfo.cookie.options);
109+ }
110+ return langResponse;
111+ }
112+
113+
114+ // Other middleware logic
115+
116+ return NextResponse.next();
117+ }
118+ ` ` `
119+
120+
121+ ### Next .js with Express
44122` server.ts `
45123` ` ` typescript
46124import express from 'express';
@@ -83,7 +161,7 @@ nextApp.prepare().then(async () => {
83161});
84162` ` `
85163
86- ## Next.js ≤12 Page Example
164+ ### Next .js ≤12 Page
87165` index.tsx `
88166` ` ` typescript
89167// Next.js page
@@ -116,42 +194,4 @@ export async function getStaticProps(context){
116194 }
117195 };
118196}
119- ` ` `
120-
121- ## Next.js ≥13 Page Example
122- ` app / [locale ]/ translations .ts `
123- ` ` ` typescript
124- ' use server' ;
125- import " server-only" ;
126- import { getTranslations } from " lup-language" ;
127-
128- export default async function loadTranslations(locale : string , translationKeys : string []): Promise <{[key : string ]: string }> {
129- return await getTranslations (locale , ' en' , translationKeys ); // second argument is default locale
130- };
131- ` ` `
132-
133- ` app / [locale ]/ layout .tsx `
134- ` ` ` typescript
135-
136- ` ` `
137-
138- ` app / [locale ]/ page .tsx `
139- ` ` ` typescript
140- import React from " react" ;
141- import styles from ' ./page.module.css' ;
142- import loadTranslations from " ../translations" ;
143- import { StaticParamsContext } from " ../../../services/Types.service" ;
144-
145- export default async function Home({ params }: StaticParamsContext ) {
146- const locale = params .locale ;
147- const TEXT = await loadTranslations (locale , [' HelloWorld' ]);
148-
149- return <>
150- <b >{TEXT[' HelloWorld' ]}</b>
151- </>
152- }
153-
154- export async function generateStaticParams(context: StaticParamsContext ) {
155- return [];
156- }
157197` ` `
0 commit comments