|
1 | 1 | import type { NextConfig } from "next"; |
2 | 2 |
|
3 | | -import path from "path"; |
4 | | -import fs from "fs/promises"; |
5 | | -import { |
6 | | - getCacheDir, |
7 | | - getCachePath, |
8 | | - getConfigPath, |
9 | | -} from "../utils/path-helpers"; |
| 3 | +import { getConfigPath } from "../utils/path-helpers"; |
10 | 4 | import { createLingoConfig } from "../utils/config-factory"; |
11 | | -import { logger } from "../utils/logger"; |
12 | | -import { startTranslationServer } from "../translation-server"; |
| 5 | +import { logger, loggerRegistry } from "../utils/logger"; |
13 | 6 | import type { PartialLingoConfig } from "../types"; |
14 | 7 | import type { TurbopackOptions } from "next/dist/server/config-shared"; |
15 | 8 |
|
@@ -71,6 +64,12 @@ function buildLingoConfig( |
71 | 64 | ): NextConfig { |
72 | 65 | const lingoConfig = createLingoConfig(lingoOptions); |
73 | 66 |
|
| 67 | + loggerRegistry.create("plugin", { |
| 68 | + enableConsole: true, |
| 69 | + // TODO (AleksandrSl 04/12/2025): Make configurable |
| 70 | + enableDebug: true, |
| 71 | + }); |
| 72 | + |
74 | 73 | // Prepare Turbopack loader configuration |
75 | 74 | const loaderConfig = { |
76 | 75 | loader: "@lingo.dev/_compiler/turbopack-loader", |
@@ -156,63 +155,17 @@ function buildLingoConfig( |
156 | 155 | } |
157 | 156 |
|
158 | 157 | logger.info("Running post-build translation generation..."); |
159 | | - let translationServer: |
160 | | - | Awaited<ReturnType<typeof startTranslationServer>> |
161 | | - | undefined; |
162 | | - try { |
163 | | - translationServer = await startTranslationServer({ |
164 | | - startPort: lingoConfig.dev?.serverStartPort, |
165 | | - onError: (err) => { |
166 | | - logger.error("Translation server error:", err); |
167 | | - }, |
168 | | - config: lingoConfig, |
169 | | - }); |
170 | | - |
171 | | - logger.info( |
172 | | - `Processing translations for ${lingoConfig.targetLocales.length} locale(s)...`, |
173 | | - ); |
174 | 158 |
|
175 | | - // Translate all locales in parallel |
176 | | - // Each translator handles its own batching strategy internally |
177 | | - const localePromises = lingoConfig.targetLocales.map(async (locale) => { |
178 | | - logger.info(`Translating to ${locale}...`); |
179 | | - |
180 | | - const result = await translationServer!.translateAll(locale); |
| 159 | + try { |
| 160 | + const { processBuildTranslations } = await import("./build-translator"); |
181 | 161 |
|
182 | | - if (result.errors.length > 0) { |
183 | | - logger.warn( |
184 | | - `Translation completed with ${result.errors.length} errors for ${locale}`, |
185 | | - ); |
186 | | - } |
187 | | - logger.info(`${locale} completed`); |
| 162 | + await processBuildTranslations({ |
| 163 | + config: lingoConfig, |
| 164 | + publicOutputPath: distDir, |
188 | 165 | }); |
189 | | - |
190 | | - await Promise.all(localePromises); |
191 | | - |
192 | | - const publicPath = distDir; |
193 | | - logger.info(`Generating static translation files in ${distDir}`); |
194 | | - |
195 | | - await fs.mkdir(publicPath, { recursive: true }); |
196 | | - |
197 | | - for (const locale of lingoOptions.targetLocales) { |
198 | | - const cacheFilePath = getCachePath(lingoConfig, locale); |
199 | | - const publicFilePath = path.join(publicPath, `${locale}.json`); |
200 | | - |
201 | | - try { |
202 | | - await fs.copyFile(cacheFilePath, publicFilePath); |
203 | | - logger.info(`✓ Generated ${locale}.json`); |
204 | | - } catch (error) { |
205 | | - logger.error(`Failed to copy ${locale}.json:`, error); |
206 | | - } |
207 | | - } |
208 | | - |
209 | | - logger.info("Translation generation completed successfully"); |
210 | 166 | } catch (error) { |
211 | 167 | logger.error("Translation generation failed:", error); |
212 | 168 | throw error; |
213 | | - } finally { |
214 | | - translationServer?.stop(); |
215 | | - logger.info("Shutting down translation server..."); |
216 | 169 | } |
217 | 170 | }; |
218 | 171 |
|
|
0 commit comments