mirror of
https://github.com/spliit-app/spliit.git
synced 2026-03-05 12:16:13 +01:00
Internationalization + Finnish language (#181)
* I18n with next-intl * package-lock * Finnish translations * Development fix * Use locale for positioning currency symbol * Translations: Expenses.ActiveUserModal * Translations: group 404 * Better translation for ExpenseCard * Apply translations in CategorySelect search * Fix for Finnish translation * Translations for ExpenseDocumentsInput * Translations for CreateFromReceipt * Fix for Finnish translation * Translations for schema errors * Fix for Finnish translation * Fixes for Finnish translations * Prettier --------- Co-authored-by: Sebastien Castiel <sebastien@castiel.me>
This commit is contained in:
42
src/lib/locale.ts
Normal file
42
src/lib/locale.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
'use server'
|
||||
|
||||
import { Locale, Locales, defaultLocale, locales } from '@/i18n'
|
||||
import { match } from '@formatjs/intl-localematcher'
|
||||
import Negotiator from 'negotiator'
|
||||
import { cookies, headers } from 'next/headers'
|
||||
|
||||
const COOKIE_NAME = 'NEXT_LOCALE'
|
||||
|
||||
function getAcceptLanguageLocale(requestHeaders: Headers, locales: Locales) {
|
||||
let locale
|
||||
const languages = new Negotiator({
|
||||
headers: {
|
||||
'accept-language': requestHeaders.get('accept-language') || undefined,
|
||||
},
|
||||
}).languages()
|
||||
try {
|
||||
locale = match(languages, locales, defaultLocale)
|
||||
} catch (e) {
|
||||
// invalid language
|
||||
}
|
||||
return locale
|
||||
}
|
||||
|
||||
export async function getUserLocale() {
|
||||
let locale
|
||||
|
||||
// Prio 1: use existing cookie
|
||||
locale = cookies().get(COOKIE_NAME)?.value
|
||||
|
||||
// Prio 2: use `accept-language` header
|
||||
// Prio 3: use default locale
|
||||
if (!locale) {
|
||||
locale = getAcceptLanguageLocale(headers(), locales)
|
||||
}
|
||||
|
||||
return locale
|
||||
}
|
||||
|
||||
export async function setUserLocale(locale: Locale) {
|
||||
cookies().set(COOKIE_NAME, locale)
|
||||
}
|
||||
Reference in New Issue
Block a user