@@ -13,9 +13,7 @@ import { deleteBundleNested } from "./helper/deleteBundleNested.js"
1313import { handleUpdateBundle } from "./helper/handleBundleUpdate.js"
1414import { createMessage } from "./helper/createMessage.js"
1515import { saveProject } from "../../main.js"
16- import { createFileSystemMapper } from "../fs/createFileSystemMapper.js"
17- import path from "path"
18- import fs from "node:fs/promises"
16+ import { rpc } from "@inlang/rpc"
1917
2018// Same interface as before
2119export interface UpdateBundleMessage {
@@ -148,6 +146,90 @@ export function editorView(args: { context: vscode.ExtensionContext; initialBund
148146
149147 updateView ( )
150148 return
149+ case "translate-bundle" :
150+ // Handle the translated bundle
151+ try {
152+ // Update the bundle in the database
153+ await handleUpdateBundle ( {
154+ db : state ( ) . project ?. db ,
155+ message : {
156+ command : "translate-bundle" ,
157+ change : message . translatedBundle ,
158+ } ,
159+ } )
160+ updateView ( )
161+ } catch ( error ) {
162+ console . error ( "Failed to update translated bundle" , error )
163+ vscode . window . showErrorMessage ( `Failed to update translations: ${ String ( error ) } ` )
164+ }
165+ return
166+ case "machine-translate-bundle" :
167+ // Handle machine translation directly in extension host
168+ try {
169+ const sourceBundle = message . bundle
170+ const sourceLocale = message . sourceLocale
171+ const targetLocale = message . targetLocale
172+
173+ // Show info message
174+ msg ( "Translating..." , "info" , "statusBar" , vscode . StatusBarAlignment . Right , 3000 )
175+
176+ // Use the RPC translation service
177+ const result = await rpc . machineTranslateBundle ( {
178+ bundle : sourceBundle ,
179+ sourceLocale,
180+ targetLocales : [ targetLocale ] ,
181+ } )
182+
183+ if ( result . error ) {
184+ throw new Error ( result . error )
185+ }
186+
187+ // Check if result.data is undefined
188+ if ( ! result . data ) {
189+ throw new Error ( "Translation result is empty" )
190+ }
191+
192+ // We need to ensure the result.data has the expected structure
193+ // Create a properly formatted bundle with required fields to match expected type
194+ const typedBundle = {
195+ ...result . data ,
196+ id : result . data . id || sourceBundle . id , // Ensure id is never undefined
197+ declarations : result . data . declarations || [ ] , // Ensure declarations is always an array
198+ // Add any other required fields that might be missing
199+ }
200+
201+ // Update the bundle in the database with the properly typed data
202+ await handleUpdateBundle ( {
203+ db : state ( ) . project ?. db ,
204+ message : {
205+ command : "translate-bundle" ,
206+ change : {
207+ entityId : sourceBundle . id ,
208+ entity : "bundle" ,
209+ newData : typedBundle ,
210+ } ,
211+ } ,
212+ } )
213+
214+ updateView ( )
215+ msg ( "Translation complete" , "info" , "statusBar" , vscode . StatusBarAlignment . Right , 3000 )
216+ } catch ( error ) {
217+ console . error ( "Failed to translate bundle" , error )
218+ msg (
219+ `Translation error: ${ String ( error ) } ` ,
220+ "error" ,
221+ "statusBar" ,
222+ vscode . StatusBarAlignment . Right ,
223+ 3000
224+ )
225+ }
226+ return
227+ case "show-info-message" :
228+ msg ( message . message , "info" , "statusBar" , vscode . StatusBarAlignment . Right , 3000 )
229+ return
230+ case "show-error-message" :
231+ msg ( message . message , "error" , "statusBar" , vscode . StatusBarAlignment . Right , 3000 )
232+ return
151233
152234 default :
153235 console . error ( "Unknown command from webview:" , command )
@@ -245,8 +327,8 @@ export function editorView(args: { context: vscode.ExtensionContext; initialBund
245327 // Allow media from safe sources (if needed)
246328 `media-src ${ webview . cspSource } https://* data:;` ,
247329
248- // Allow connections to APIs, WebSockets, and data URIs
249- `connect-src https://* data: ${
330+ // Allow connections to APIs, WebSockets, and data URIs - include http://localhost:3000 for RPC
331+ `connect-src https://* http://localhost:3000 data: ${
250332 isProd
251333 ? ``
252334 : `ws://${ localServerUrl } ws://0.0.0.0:${ localPort } http://${ localServerUrl } http://0.0.0.0:${ localPort } `
0 commit comments