-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2391 from opral/introduce-machine-translate-vscode
- Loading branch information
Showing
16 changed files
with
243 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"vs-code-extension": minor | ||
--- | ||
|
||
add machine translate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
inlang/source-code/ide-extension/src/commands/machineTranslate.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import { describe, it, expect, vi, beforeEach } from "vitest" | ||
import { rpc } from "@inlang/rpc" | ||
import { CONFIGURATION } from "../configuration.js" | ||
import { machineTranslateMessageCommand } from "./machineTranslate.js" | ||
import { msg } from "../utilities/messages/msg.js" | ||
|
||
vi.mock("vscode", () => ({ | ||
commands: { | ||
registerCommand: vi.fn(), | ||
}, | ||
StatusBarAlignment: { | ||
Left: 1, | ||
Right: 2, | ||
}, | ||
window: { | ||
createStatusBarItem: vi.fn(() => ({ | ||
show: vi.fn(), | ||
text: "", | ||
})), | ||
}, | ||
})) | ||
|
||
vi.mock("@inlang/rpc", () => ({ | ||
rpc: { | ||
machineTranslateMessage: vi.fn(), | ||
}, | ||
})) | ||
|
||
vi.mock("../configuration", () => ({ | ||
CONFIGURATION: { | ||
EVENTS: { | ||
ON_DID_EDIT_MESSAGE: { | ||
fire: vi.fn(), | ||
}, | ||
}, | ||
}, | ||
})) | ||
|
||
vi.mock("../utilities/messages/msg", () => ({ | ||
msg: vi.fn(), | ||
})) | ||
|
||
vi.mock("../utilities/state", () => ({ | ||
state: () => ({ | ||
project: { | ||
query: { | ||
messages: { | ||
get: (args: any) => { | ||
if (args.where && args.where.id === "validId") { | ||
return mockMessage | ||
} | ||
return undefined | ||
}, | ||
upsert: vi.fn(), | ||
}, | ||
}, | ||
}, | ||
}), | ||
})) | ||
|
||
const mockMessage = { | ||
id: "validId", | ||
alias: {}, | ||
selectors: [], | ||
variants: [ | ||
{ | ||
languageTag: "en", | ||
match: [], | ||
pattern: [ | ||
{ | ||
type: "Text", | ||
value: "Original content", | ||
}, | ||
], | ||
}, | ||
], | ||
} | ||
|
||
describe("machineTranslateMessageCommand", () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks() | ||
}) | ||
|
||
it("should return a message if messageId is not found", async () => { | ||
await machineTranslateMessageCommand.callback({ | ||
messageId: "nonexistent", | ||
sourceLanguageTag: "en", | ||
targetLanguageTags: ["es"], | ||
}) | ||
|
||
expect(msg).toHaveBeenCalledWith("Message with id nonexistent not found.") | ||
}) | ||
|
||
it("should return an error message on RPC error", async () => { | ||
// @ts-expect-error | ||
rpc.machineTranslateMessage.mockResolvedValueOnce({ error: "RPC Error" }) | ||
|
||
await machineTranslateMessageCommand.callback({ | ||
messageId: "validId", | ||
sourceLanguageTag: "en", | ||
targetLanguageTags: ["es"], | ||
}) | ||
|
||
expect(msg).toHaveBeenCalledWith("Error translating message: RPC Error") | ||
}) | ||
|
||
it("should return a message if no translation is available", async () => { | ||
// @ts-expect-error | ||
rpc.machineTranslateMessage.mockResolvedValueOnce({ data: undefined }) | ||
|
||
await machineTranslateMessageCommand.callback({ | ||
messageId: "validId", | ||
sourceLanguageTag: "en", | ||
targetLanguageTags: ["es"], | ||
}) | ||
|
||
expect(msg).toHaveBeenCalledWith("No translation available.") | ||
}) | ||
|
||
it("should successfully translate and update a message", async () => { | ||
const mockTranslation = { translatedText: "Translated content" } | ||
// @ts-expect-error | ||
rpc.machineTranslateMessage.mockResolvedValueOnce({ data: mockTranslation }) | ||
|
||
await machineTranslateMessageCommand.callback({ | ||
messageId: "validId", | ||
sourceLanguageTag: "en", | ||
targetLanguageTags: ["es"], | ||
}) | ||
|
||
expect(msg).toHaveBeenCalledWith("Message translated.") | ||
}) | ||
|
||
it("should emit ON_DID_EDIT_MESSAGE event after successful translation", async () => { | ||
const mockTranslation = { translatedText: "Translated content" } | ||
// @ts-expect-error | ||
rpc.machineTranslateMessage.mockResolvedValueOnce({ data: mockTranslation }) | ||
|
||
await machineTranslateMessageCommand.callback({ | ||
messageId: "validId", | ||
sourceLanguageTag: "en", | ||
targetLanguageTags: ["es"], | ||
}) | ||
|
||
expect(CONFIGURATION.EVENTS.ON_DID_EDIT_MESSAGE.fire).toHaveBeenCalled() | ||
}) | ||
}) |
55 changes: 55 additions & 0 deletions
55
inlang/source-code/ide-extension/src/commands/machineTranslate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { commands } from "vscode" | ||
import { LanguageTag, Message } from "@inlang/sdk" | ||
import { state } from "../utilities/state.js" | ||
import { msg } from "../utilities/messages/msg.js" | ||
import { rpc } from "@inlang/rpc" | ||
import { CONFIGURATION } from "../configuration.js" | ||
|
||
export const machineTranslateMessageCommand = { | ||
command: "sherlock.machineTranslateMessage", | ||
title: "Sherlock: Machine Translate Message", | ||
register: commands.registerCommand, | ||
callback: async function ({ | ||
messageId, | ||
sourceLanguageTag, | ||
targetLanguageTags, | ||
}: { | ||
messageId: Message["id"] | ||
sourceLanguageTag: LanguageTag | ||
targetLanguageTags: LanguageTag[] | ||
}) { | ||
// Get the message from the state | ||
const message = state().project.query.messages.get({ where: { id: messageId } }) | ||
if (!message) { | ||
return msg(`Message with id ${messageId} not found.`) | ||
} | ||
|
||
// Call machine translation RPC function | ||
const result = await rpc.machineTranslateMessage({ | ||
message, | ||
sourceLanguageTag, | ||
targetLanguageTags, | ||
}) | ||
|
||
if (result.error) { | ||
return msg(`Error translating message: ${result.error}`) | ||
} | ||
|
||
// Update the message with the translated content | ||
const updatedMessage = result.data | ||
if (!updatedMessage) { | ||
return msg("No translation available.") | ||
} | ||
|
||
state().project.query.messages.upsert({ | ||
where: { id: messageId }, | ||
data: updatedMessage, | ||
}) | ||
|
||
// Emit event to notify that a message was edited | ||
CONFIGURATION.EVENTS.ON_DID_EDIT_MESSAGE.fire() | ||
|
||
// Return success message | ||
return msg("Message translated.") | ||
}, | ||
} as const |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.