diff --git a/.changeset/frank-days-start.md b/.changeset/frank-days-start.md new file mode 100644 index 0000000000..02ec1ebd41 --- /dev/null +++ b/.changeset/frank-days-start.md @@ -0,0 +1,5 @@ +--- +"@medusajs/translation": patch +--- + +fix(translation): prevent duplicate locale error on server restart diff --git a/packages/modules/translation/src/loaders/defaults.ts b/packages/modules/translation/src/loaders/defaults.ts index ab05f516f6..1509127908 100644 --- a/packages/modules/translation/src/loaders/defaults.ts +++ b/packages/modules/translation/src/loaders/defaults.ts @@ -3,7 +3,10 @@ import { Logger, ModulesSdkTypes, } from "@medusajs/framework/types" -import { ContainerRegistrationKeys } from "@medusajs/framework/utils" +import { + ContainerRegistrationKeys, + normalizeLocale, +} from "@medusajs/framework/utils" import Locale from "@models/locale" /** @@ -70,7 +73,24 @@ export default async ({ container }: LoaderOptions): Promise => { container.resolve("localeService") try { - const resp = await localeService_.upsert(defaultLocales) + // Fetch existing locales to map their IDs for upsert + // The upsert method uses `id` as the key, so we need to include IDs for existing locales + const existingLocales = await localeService_.list( + {}, + { select: ["id", "code"] } + ) + const existingByCode = new Map( + existingLocales.map((l) => [l.code, l.id]) + ) + + // Map default locales to include IDs for existing ones + const localesToUpsert = defaultLocales.map((locale) => { + const normalizedCode = normalizeLocale(locale.code) + const existingId = existingByCode.get(normalizedCode) + return existingId ? { ...locale, id: existingId } : locale + }) + + const resp = await localeService_.upsert(localesToUpsert) logger.debug(`Loaded ${resp.length} locales`) } catch (error) { logger.warn(