Skip to content

Commit 8debb68

Browse files
authored
fix: generate types when no en language is defined in i18n (#10181)
Previously, if you had for example only `de` in `i18n` - `generate:types` would fail Fixes #10145
1 parent 466f109 commit 8debb68

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

packages/payload/src/bin/generateTypes.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { AcceptedLanguages } from '@payloadcms/translations'
2+
13
import { initI18n } from '@payloadcms/translations'
24
import fs from 'fs'
35
import { compile } from 'json-schema-to-typescript'
@@ -20,7 +22,12 @@ export async function generateTypes(
2022
if (shouldLog) {
2123
logger.info('Compiling TS types for Collections and Globals...')
2224
}
23-
const i18n = await initI18n({ config: config.i18n, context: 'api', language: 'en' })
25+
26+
const languages = Object.keys(config.i18n.supportedLanguages) as AcceptedLanguages[]
27+
28+
const language = languages.includes('en') ? 'en' : config.i18n.fallbackLanguage
29+
30+
const i18n = await initI18n({ config: config.i18n, context: 'api', language })
2431

2532
const jsonSchema = configToJSONSchema(config, config.db.defaultIDType, i18n)
2633

packages/payload/src/utilities/configToJSONSchema.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,9 @@ export function withNullableJSONSchemaType(
200200
}
201201

202202
function entityOrFieldToJsDocs({
203-
config,
204203
entity,
205204
i18n,
206205
}: {
207-
config: SanitizedConfig
208206
entity: FlattenedField | SanitizedCollectionConfig | SanitizedGlobalConfig
209207
i18n?: I18n
210208
}): string | undefined {
@@ -213,11 +211,10 @@ function entityOrFieldToJsDocs({
213211
if (typeof entity?.admin?.description === 'string') {
214212
description = entity?.admin?.description
215213
} else if (typeof entity?.admin?.description === 'object') {
216-
const defaultLocale = config?.localization ? config?.localization?.defaultLocale : undefined
217214
if (entity?.admin?.description?.en) {
218215
description = entity?.admin?.description?.en
219-
} else if (entity?.admin?.description?.[defaultLocale]) {
220-
description = entity?.admin?.description?.[defaultLocale]
216+
} else if (entity?.admin?.description?.[i18n.language]) {
217+
description = entity?.admin?.description?.[i18n.language]
221218
}
222219
} else if (typeof entity?.admin?.description === 'function' && i18n) {
223220
description = entity?.admin?.description(i18n)
@@ -255,7 +252,7 @@ export function fieldsToJSONSchema(
255252
requiredFieldNames.add(field.name)
256253
}
257254

258-
const fieldDescription = entityOrFieldToJsDocs({ config, entity: field, i18n })
255+
const fieldDescription = entityOrFieldToJsDocs({ entity: field, i18n })
259256
const baseFieldSchema: JSONSchema4 = {}
260257
if (fieldDescription) {
261258
baseFieldSchema.description = fieldDescription
@@ -710,7 +707,7 @@ export function entityToJSONSchema(
710707
),
711708
}
712709

713-
const entityDescription = entityOrFieldToJsDocs({ config, entity, i18n })
710+
const entityDescription = entityOrFieldToJsDocs({ entity, i18n })
714711

715712
if (entityDescription) {
716713
jsonSchema.description = entityDescription

0 commit comments

Comments
 (0)