-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: language detectors accessible from outside
- Loading branch information
Showing
10 changed files
with
113 additions
and
82 deletions.
There are no files selected for viewing
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
10 changes: 10 additions & 0 deletions
10
packages/web/src/package/tools/detectLanguageFromHeaders.ts
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,10 @@ | ||
import { detectLanguage } from '../LanguageDetector'; | ||
import { getHeaderLanguages } from './getHeaderLanguages'; | ||
|
||
export const detectLanguageFromHeaders = ( | ||
headers: Headers, | ||
availableLanguages: string[] | ||
) => { | ||
const languages = getHeaderLanguages(headers); | ||
return languages[0] && detectLanguage(languages[0], availableLanguages); | ||
}; |
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,36 @@ | ||
import { getHeaderLanguages } from './getHeaderLanguages'; | ||
|
||
describe('locale from headers', () => { | ||
it('parses correctly single locale', () => { | ||
const headers = new Headers(); | ||
headers.set('Accept-Language', 'en-US'); | ||
expect(getHeaderLanguages(headers)).toEqual(['en-US']); | ||
}); | ||
|
||
it('parses correctly multiple locales', () => { | ||
const headers = new Headers(); | ||
headers.set('Accept-Language', 'en-US, en'); | ||
expect(getHeaderLanguages(headers)).toEqual(['en-US', 'en']); | ||
}); | ||
|
||
it('parses correctly star', () => { | ||
const headers = new Headers(); | ||
headers.set('Accept-Language', '*'); | ||
expect(getHeaderLanguages(headers)).toEqual([]); | ||
}); | ||
|
||
it('parses correctly weighted locales', () => { | ||
const headers = new Headers(); | ||
headers.set('Accept-Language', 'fr-CH, fr;q=0.9'); | ||
expect(getHeaderLanguages(headers)).toEqual(['fr-CH', 'fr']); | ||
}); | ||
|
||
it('parses correctly weighted locales with star', () => { | ||
const headers = new Headers(); | ||
headers.set( | ||
'Accept-Language', | ||
'fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5' | ||
); | ||
expect(getHeaderLanguages(headers)).toEqual(['fr-CH', 'fr', 'en', 'de']); | ||
}); | ||
}); |
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,15 @@ | ||
export function getHeaderLanguages(headers: Headers) { | ||
const acceptLanguageHeader = headers.get('Accept-Language'); | ||
if (!acceptLanguageHeader) { | ||
return []; | ||
} | ||
// Split the header into locales based on commas | ||
const locales = acceptLanguageHeader.split(',').map((locale) => { | ||
// Remove whitespace and split by ';' to get only the locale part | ||
const [localePart] = locale.trim().split(';'); | ||
return localePart; | ||
}); | ||
|
||
// Filter out any empty strings and return the unique locales | ||
return [...new Set(locales.filter((locale) => locale && locale !== '*'))]; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use server'; | ||
|
||
import { detectLanguageFromHeaders } from '@tolgee/react/server'; | ||
import { cookies, headers } from 'next/headers'; | ||
import { ALL_LOCALES, DEFAULT_LOCALE } from './shared'; | ||
|
||
const LOCALE_COOKIE = 'NEXT_LOCALE'; | ||
|
||
export async function setLocale(locale: string) { | ||
const cookieStore = cookies(); | ||
cookieStore.set({ | ||
name: LOCALE_COOKIE, | ||
value: locale, | ||
}); | ||
} | ||
|
||
export async function getLocale() { | ||
const cookieStore = cookies(); | ||
const locale = cookieStore.get(LOCALE_COOKIE)?.value; | ||
if (locale && ALL_LOCALES.includes(locale)) { | ||
return locale; | ||
} else { | ||
return ( | ||
detectLanguageFromHeaders(await headers(), ALL_LOCALES) ?? DEFAULT_LOCALE | ||
); | ||
} | ||
} |
This file was deleted.
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