Skip to content

Commit 7b4f1f6

Browse files
committed
Added checkLanguage() function and updated README
1 parent cc29cf3 commit 7b4f1f6

3 files changed

Lines changed: 59 additions & 1 deletion

File tree

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,28 @@ export default async function loadTranslations(locale: string, translationKeys:
6363

6464
`app/[locale]/layout.tsx`
6565
```typescript
66+
67+
export default async function RootLayout({ params, children }: LocaleLayoutProps){
68+
const { locale } = await params;
69+
70+
// if locale is not supported redirect to a supported one
71+
const validLocale = await checkLanguage(locale, getDefaultLocale());
72+
if(validLocale && locale !== validLocale){
73+
redirect('/'+validLocale+'/');
74+
}
75+
76+
// load translations for given translation keys (note this loadTranslations function is defined in translations.ts, see below)
77+
const TEXT = await loadTranslations(locale, [
78+
'HelloWorld',
79+
]);
80+
81+
return <html lang={locale}>
82+
<body>
83+
{TEXT['HelloWorld']}
84+
</body>
85+
</html>;
86+
}
87+
6688
export async function generateStaticParams(context: StaticParamsContext){
6789
const locales = await getLocales();
6890
return locales.map((locale) => ({ locale: locale }));
@@ -86,6 +108,19 @@ export default async function Home({ params }: StaticParamsContext) {
86108
}
87109
```
88110

111+
`app/[locale]/translations.ts`
112+
Server action that also allows client components to load translations.
113+
```typescript
114+
'use server';
115+
import 'server-only';
116+
117+
import { getTranslations } from "lup-language";
118+
119+
export default async function loadTranslations(locale: string, translationKeys: string[]): Promise<{[key: string]: string}> {
120+
return await getTranslations(locale, getDefaultLocale(), translationKeys);
121+
};
122+
```
123+
89124
Optional if unsupported languages or root should be redirected:
90125
`middleware.ts`
91126
```typescript

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lup-language",
3-
"version": "2.1.4",
3+
"version": "2.2.1",
44
"description": "Node express middleware for detecting requested language",
55
"files": [
66
"lib/**/*"

src/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,29 @@ export const getLanguages = async (translationsDir: string = DEFAULTS.TRANSLATIO
379379
return [...LANGUAGES[translationsDir]];
380380
};
381381

382+
383+
/**
384+
* Checks if a given language is supported and returns the supported language code.
385+
* If the given language is not supported the default language will be returned.
386+
*
387+
* @param lang Language code that should be checked.
388+
* @param defaultLang Default language code if given 'lang' is not supported.
389+
* @param translationsDir Relative path to directory containing JSON files with translations to lookup supported translations (optional).
390+
* @return Promise that resolves to the supported language code or undefined if no language is supported and also no default language is given.
391+
*/
392+
export const checkLanguage = async (lang: string | null | undefined, defaultLang?: string, translationsDir: string = DEFAULTS.TRANSLATIONS_DIR): Promise<string | undefined> => {
393+
if(!lang) return defaultLang;
394+
lang = lang.trim().toLowerCase();
395+
const langs = await getLanguages(translationsDir);
396+
for(let i=0; i < langs.length; i++){
397+
if(langs[i].toLowerCase() === lang){
398+
return langs[i];
399+
}
400+
}
401+
return defaultLang;
402+
}
403+
404+
382405
/**
383406
* Returns a map of all found languages and their native name.
384407
* Looksup following keys in the translations 'LANGUAGE_NAME_<lang>'.

0 commit comments

Comments
 (0)