-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add locale detecting/switching logic
- Loading branch information
Showing
11 changed files
with
496 additions
and
223 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
[ | ||
{ | ||
"code": "ar-AR", | ||
"name": "العربية (الأرجنتين)" | ||
}, | ||
{ | ||
"code": "ast", | ||
"name": "asturianu" | ||
}, | ||
{ | ||
"code": "be-BY", | ||
"name": "беларуская (Беларусь)" | ||
}, | ||
{ | ||
"code": "ca-ES", | ||
"name": "català (Espanya)" | ||
}, | ||
{ | ||
"code": "cs-CZ", | ||
"name": "čeština (Česko)" | ||
}, | ||
{ | ||
"code": "de-DE", | ||
"name": "Deutsch (Deutschland)" | ||
}, | ||
{ | ||
"code": "el-GR", | ||
"name": "Ελληνικά (Ελλάδα)" | ||
}, | ||
{ | ||
"code": "en-GB", | ||
"name": "British English" | ||
}, | ||
{ | ||
"code": "en-US", | ||
"name": "American English" | ||
}, | ||
{ | ||
"code": "es-ES", | ||
"name": "español de España" | ||
}, | ||
{ | ||
"code": "fi-FI", | ||
"name": "suomi (Suomi)" | ||
}, | ||
{ | ||
"code": "fr-FR", | ||
"name": "français (France)" | ||
}, | ||
{ | ||
"code": "gd-GB", | ||
"name": "Gàidhlig (An Rìoghachd Aonaichte)" | ||
}, | ||
{ | ||
"code": "it-IT", | ||
"name": "italiano (Italia)" | ||
}, | ||
{ | ||
"code": "ja-JP", | ||
"name": "日本語 (日本)" | ||
}, | ||
{ | ||
"code": "kk-KZ", | ||
"name": "қазақ тілі (Қазақстан)" | ||
}, | ||
{ | ||
"code": "ko-KR", | ||
"name": "한국어(대한민국)" | ||
}, | ||
{ | ||
"code": "lt-LT", | ||
"name": "lietuvių (Lietuva)" | ||
}, | ||
{ | ||
"code": "nb-NO", | ||
"name": "norsk bokmål (Norge)" | ||
}, | ||
{ | ||
"code": "nl-NL", | ||
"name": "Nederlands (Nederland)" | ||
}, | ||
{ | ||
"code": "pl-PL", | ||
"name": "polski (Polska)" | ||
}, | ||
{ | ||
"code": "pt-BR", | ||
"name": "português (Brasil)" | ||
}, | ||
{ | ||
"code": "pt-PT", | ||
"name": "português europeu" | ||
}, | ||
{ | ||
"code": "ro-RO", | ||
"name": "română (România)" | ||
}, | ||
{ | ||
"code": "ru-RU", | ||
"name": "русский (Россия)" | ||
}, | ||
{ | ||
"code": "sr-RS", | ||
"name": "српски (Србија)" | ||
}, | ||
{ | ||
"code": "sv-SE", | ||
"name": "svenska (Sverige)" | ||
}, | ||
{ | ||
"code": "tr-TR", | ||
"name": "Türkçe (Türkiye)" | ||
}, | ||
{ | ||
"code": "uk-UA", | ||
"name": "українська (Україна)" | ||
}, | ||
{ | ||
"code": "zh-CN", | ||
"name": "中文(中国)" | ||
}, | ||
{ | ||
"code": "zh-Hant", | ||
"name": "繁體中文" | ||
}, | ||
{ | ||
"code": "en@uwu", | ||
"name": "English (lolcat)" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,51 @@ | ||
import { NextResponse } from 'next/server'; | ||
import type { NextRequest } from 'next/server'; | ||
|
||
import { match } from '@formatjs/intl-localematcher'; | ||
import Negotiator from 'negotiator'; | ||
|
||
import localeList from '../public/assets/json/localeList.json'; | ||
|
||
import { isLoggedIn } from './utils/auth'; | ||
|
||
export function middleware(request: NextRequest) { | ||
// selects locale code based on user accept-language header and saves it as cookie | ||
let availableLocales: string[] = []; | ||
localeList.forEach((l) => { | ||
availableLocales.push(l.code); | ||
}); | ||
|
||
availableLocales = availableLocales.filter((l) => l !== 'en@uwu'); | ||
|
||
const plainHeaders: Record<string, string> = {}; | ||
request.headers.forEach((v, k) => (plainHeaders[k] = v)); | ||
|
||
const userLocales = new Negotiator({ headers: plainHeaders }).languages(availableLocales); | ||
|
||
const locale = match(userLocales, availableLocales, 'en-US'); | ||
|
||
const response = NextResponse.next(); | ||
response.cookies.set({ | ||
name: 'autoLocale', | ||
value: locale, | ||
path: '/', | ||
}); | ||
|
||
if ( | ||
request.nextUrl.pathname.startsWith('/account/login') || | ||
request.nextUrl.pathname.startsWith('/account/register') | ||
) { | ||
return response; | ||
} | ||
|
||
if (request.nextUrl.pathname.startsWith('/account') && !isLoggedIn()) { | ||
|
||
const target = `/account/login?redirect=${encodeURIComponent(request.nextUrl.href)}`; | ||
return NextResponse.redirect(new URL(target, request.url)); | ||
} | ||
|
||
return response; | ||
} | ||
|
||
export const config = { | ||
matcher: '/account((?!/login|/register).*)', | ||
matcher: '/((?!api|_next/static|_next/image|favicon.ico).*)', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* this file runs at buildtime. it should not contain any functions actively used by the website. */ | ||
|
||
import fs from 'fs-extra'; | ||
import { getLocaleList } from './locale.ts'; | ||
|
||
const filename = './public/assets/json/localeList.json'; | ||
|
||
fs.ensureFileSync(filename); | ||
|
||
const localeList = getLocaleList(); | ||
|
||
fs.writeFileSync(filename, JSON.stringify(localeList, null, '\t')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters