Skip to content

Commit

Permalink
fix: properly extracts fallbackLang (#6237)
Browse files Browse the repository at this point in the history
  • Loading branch information
JarrodMFlesch committed May 6, 2024
1 parent 9f37bf7 commit ac5d744
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 16 deletions.
13 changes: 6 additions & 7 deletions packages/next/src/utilities/getRequestLanguage.ts
Expand Up @@ -14,25 +14,24 @@ type GetRequestLanguageArgs = {
export const getRequestLanguage = ({
config,
cookies,
defaultLanguage = 'en',
headers,
}: GetRequestLanguageArgs): AcceptedLanguages => {
const supportedLanguageKeys = <AcceptedLanguages[]>Object.keys(config.i18n.supportedLanguages)
const langCookie = cookies.get(`${config.cookiePrefix || 'payload'}-lng`)
const languageFromCookie = typeof langCookie === 'string' ? langCookie : langCookie?.value
const languageFromCookie: AcceptedLanguages = (
typeof langCookie === 'string' ? langCookie : langCookie?.value
) as AcceptedLanguages
const languageFromHeader = headers.get('Accept-Language')
? extractHeaderLanguage(headers.get('Accept-Language'))
: undefined
const fallbackLang = config?.i18n?.fallbackLanguage || defaultLanguage

const supportedLanguageKeys = Object.keys(config?.i18n?.supportedLanguages || {})

if (languageFromCookie && supportedLanguageKeys.includes(languageFromCookie)) {
return languageFromCookie as AcceptedLanguages
return languageFromCookie
}

if (languageFromHeader && supportedLanguageKeys.includes(languageFromHeader)) {
return languageFromHeader
}

return supportedLanguageKeys.includes(fallbackLang) ? (fallbackLang as AcceptedLanguages) : 'en'
return config.i18n.fallbackLanguage
}
1 change: 1 addition & 0 deletions packages/payload/src/config/defaults.ts
Expand Up @@ -28,6 +28,7 @@ export const defaults: Omit<Config, 'db' | 'editor' | 'secret'> = {
maxComplexity: 1000,
},
hooks: {},
i18n: {},
localization: false,
maxDepth: 10,
routes: {
Expand Down
20 changes: 18 additions & 2 deletions packages/payload/src/config/sanitize.ts
@@ -1,3 +1,5 @@
import type { AcceptedLanguages } from '@payloadcms/translations'

import { en } from '@payloadcms/translations/languages/en'
import merge from 'deepmerge'

Expand Down Expand Up @@ -88,15 +90,29 @@ export const sanitizeConfig = async (incomingConfig: Config): Promise<SanitizedC
}
}

config.i18n = {
const i18nConfig: SanitizedConfig['i18n'] = {
fallbackLanguage: 'en',
supportedLanguages: {
en,
},
translations: {},
...(incomingConfig?.i18n ?? {}),
}

if (incomingConfig?.i18n) {
i18nConfig.supportedLanguages =
incomingConfig.i18n?.supportedLanguages || i18nConfig.supportedLanguages

const supportedLangKeys = <AcceptedLanguages[]>Object.keys(i18nConfig.supportedLanguages)
const fallbackLang = incomingConfig.i18n?.fallbackLanguage || i18nConfig.fallbackLanguage

i18nConfig.fallbackLanguage = supportedLangKeys.includes(fallbackLang)
? fallbackLang
: supportedLangKeys[0]
i18nConfig.translations = incomingConfig.i18n?.translations || i18nConfig.translations
}

config.i18n = i18nConfig

configWithDefaults.collections.push(getPreferencesCollection(config as unknown as Config))
configWithDefaults.collections.push(migrationsCollection)

Expand Down
4 changes: 2 additions & 2 deletions packages/payload/src/translations/getLocalI18n.ts
Expand Up @@ -6,10 +6,10 @@ import type { SanitizedConfig } from '../config/types.js'

export const getLocalI18n = async ({
config,
language = 'en',
language,
}: {
config: SanitizedConfig
language?: AcceptedLanguages
language: AcceptedLanguages
}) =>
initI18n({
config: config.i18n,
Expand Down
6 changes: 4 additions & 2 deletions packages/payload/src/utilities/createLocalReq.ts
Expand Up @@ -68,8 +68,6 @@ export const createLocalReq: CreateLocalReq = async (
{ context, fallbackLocale, locale: localeArg, req = {} as PayloadRequestWithData, user },
payload,
) => {
const i18n = req?.i18n || (await getLocalI18n({ config: payload.config }))

if (payload.config?.localization) {
const locale = localeArg === '*' ? 'all' : localeArg
const defaultLocale = payload.config.localization.defaultLocale
Expand All @@ -84,6 +82,10 @@ export const createLocalReq: CreateLocalReq = async (
}
}

const i18n =
req?.i18n ||
(await getLocalI18n({ config: payload.config, language: payload.config.i18n.fallbackLanguage }))

// @ts-expect-error
if (!req.headers) req.headers = new Headers()
req.context = getRequestContext(req, context)
Expand Down
4 changes: 2 additions & 2 deletions packages/translations/src/types.ts
Expand Up @@ -63,7 +63,7 @@ export type I18n = {
}

export type I18nOptions = {
fallbackLanguage?: string
fallbackLanguage?: AcceptedLanguages
supportedLanguages?: SupportedLanguages
translations?: Partial<{
[key in AcceptedLanguages]?: Language['translations']
Expand All @@ -82,7 +82,7 @@ export type InitTFunction = (args: {
export type InitI18n = (args: {
config: I18nOptions
context: 'api' | 'client'
language?: AcceptedLanguages
language: AcceptedLanguages
}) => Promise<I18n>

export type LanguagePreference = {
Expand Down
2 changes: 1 addition & 1 deletion packages/translations/src/utilities/init.ts
Expand Up @@ -164,7 +164,7 @@ function memoize(fn: (args: unknown) => Promise<I18n>, keys: string[]) {
}

export const initI18n: InitI18n = memoize(
async ({ config, context, language = 'en' }: Parameters<InitI18n>[0]) => {
async ({ config, context, language = config.fallbackLanguage }: Parameters<InitI18n>[0]) => {
const translations = getTranslationsByContext(config.supportedLanguages[language], context)

const { t, translations: mergedTranslations } = initTFunction({
Expand Down

0 comments on commit ac5d744

Please sign in to comment.