1 import {
2 LoaderOptions,
3 Logger,
4 ModulesSdkTypes,
5 } from "@medusajs/framework/types"
6 import {
7 ContainerRegistrationKeys,
8 normalizeLocale,
9 } from "@medusajs/framework/utils"
10 import Locale from "@models/locale"
11
12 /**
13 * BCP 47 Language Tags
14 * Common language-region codes following the IETF BCP 47 standard.
15 * Format: language[-script][-region]
16 * Examples: "en-US" (English, United States), "zh-Hans-CN" (Chinese Simplified, China)
17 */
18 const defaultLocales = [
19 { code: "en-US", name: "English (United States)" },
20 { code: "en-GB", name: "English (United Kingdom)" },
21 { code: "en-AU", name: "English (Australia)" },
22 { code: "en-CA", name: "English (Canada)" },
23 { code: "es-ES", name: "Spanish (Spain)" },
24 { code: "es-MX", name: "Spanish (Mexico)" },
25 { code: "es-AR", name: "Spanish (Argentina)" },
26 { code: "fr-FR", name: "French (France)" },
27 { code: "fr-CA", name: "French (Canada)" },
28 { code: "fr-BE", name: "French (Belgium)" },
29 { code: "de-DE", name: "German (Germany)" },
30 { code: "de-AT", name: "German (Austria)" },
31 { code: "de-CH", name: "German (Switzerland)" },
32 { code: "it-IT", name: "Italian (Italy)" },
33 { code: "pt-BR", name: "Portuguese (Brazil)" },
34 { code: "pt-PT", name: "Portuguese (Portugal)" },
35 { code: "nl-NL", name: "Dutch (Netherlands)" },
36 { code: "nl-BE", name: "Dutch (Belgium)" },
37 { code: "da-DK", name: "Danish (Denmark)" },
38 { code: "sv-SE", name: "Swedish (Sweden)" },
39 { code: "nb-NO", name: "Norwegian Bokmål (Norway)" },
40 { code: "fi-FI", name: "Finnish (Finland)" },
41 { code: "pl-PL", name: "Polish (Poland)" },
42 { code: "lt-LT", name: "Lithuanian (Lithuania)" },
43 { code: "cs-CZ", name: "Czech (Czech Republic)" },
44 { code: "sk-SK", name: "Slovak (Slovakia)" },
45 { code: "hu-HU", name: "Hungarian (Hungary)" },
46 { code: "ro-RO", name: "Romanian (Romania)" },
47 { code: "bg-BG", name: "Bulgarian (Bulgaria)" },
48 { code: "el-GR", name: "Greek (Greece)" },
49 { code: "tr-TR", name: "Turkish (Turkey)" },
50 { code: "ru-RU", name: "Russian (Russia)" },
51 { code: "uk-UA", name: "Ukrainian (Ukraine)" },
52 { code: "ar-SA", name: "Arabic (Saudi Arabia)" },
53 { code: "ar-AE", name: "Arabic (United Arab Emirates)" },
54 { code: "ar-EG", name: "Arabic (Egypt)" },
55 { code: "he-IL", name: "Hebrew (Israel)" },
56 { code: "hi-IN", name: "Hindi (India)" },
57 { code: "bn-BD", name: "Bengali (Bangladesh)" },
58 { code: "th-TH", name: "Thai (Thailand)" },
59 { code: "vi-VN", name: "Vietnamese (Vietnam)" },
60 { code: "id-ID", name: "Indonesian (Indonesia)" },
61 { code: "ms-MY", name: "Malay (Malaysia)" },
62 { code: "tl-PH", name: "Tagalog (Philippines)" },
63 { code: "zh-CN", name: "Chinese Simplified (China)" },
64 { code: "zh-TW", name: "Chinese Traditional (Taiwan)" },
65 { code: "zh-HK", name: "Chinese Traditional (Hong Kong)" },
66 { code: "ja-JP", name: "Japanese (Japan)" },
67 { code: "ko-KR", name: "Korean (South Korea)" },
68 { code: "ka-GE", name: "Georgian (Georgia)" },
69 { code: "mn-MN", name: "Mongolian (Mongolia)" },
70 ]
71
72 export default async ({ container }: LoaderOptions): Promise<void> => {
73 const logger =
74 container.resolve<Logger>(ContainerRegistrationKeys.LOGGER) ?? console
75 const localeService_: ModulesSdkTypes.IMedusaInternalService<typeof Locale> =
76 container.resolve("localeService")
77
78 try {
79 // Fetch existing locales to map their IDs for upsert
80 // The upsert method uses `id` as the key, so we need to include IDs for existing locales
81 const existingLocales = await localeService_.list(
82 {},
83 { select: ["id", "code"] }
84 )
85 const existingByCode = new Map(
86 existingLocales.map((l) => [l.code, l.id])
87 )
88
89 // Map default locales to include IDs for existing ones
90 const localesToUpsert = defaultLocales.map((locale) => {
91 const normalizedCode = normalizeLocale(locale.code)
92 const existingId = existingByCode.get(normalizedCode)
93 return existingId ? { ...locale, id: existingId } : locale
94 })
95
96 const resp = await localeService_.upsert(localesToUpsert)
97 logger.debug(`Loaded ${resp.length} locales`)
98 } catch (error) {
99 logger.warn(
100 `Failed to load locales, skipping loader. Original error: ${error.message}`
101 )
102 }
103 }