Skip to content

Commit

Permalink
(WIP) adapt cli translate to shim v2 MessageBundles
Browse files Browse the repository at this point in the history
  • Loading branch information
jldec committed May 24, 2024
1 parent b8b7861 commit df2b2e1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
28 changes: 19 additions & 9 deletions inlang/source-code/cli/src/commands/machine/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { projectOption } from "../../utilities/globalFlags.js"
import progessBar from "cli-progress"
import plimit from "p-limit"
import type { Result } from "@inlang/result"
import { toV1Message, fromV1Message } from "@inlang/sdk/v2"

const rpcTranslateAction = process.env.MOCK_TRANSLATE_LOCAL
? mockMachineTranslateMessage
Expand Down Expand Up @@ -60,6 +61,7 @@ export async function translateCommandAction(args: { project: InlangProject }) {
return
}
const experimentalAliases = args.project.settings().experimental?.aliases
const v2Persistence = args.project.settings().experimental?.persistence

const allLanguageTags = [...projectConfig.languageTags, projectConfig.sourceLanguageTag]

Expand Down Expand Up @@ -97,9 +99,13 @@ export async function translateCommandAction(args: { project: InlangProject }) {
return
}

const messages = args.project.query.messages
.getAll()
.filter((message) => hasMissingTranslations(message, sourceLanguageTag, targetLanguageTags))
const allMessages = v2Persistence
? (await args.project.store!.messageBundles.getAll()).map(toV1Message)
: args.project.query.messages.getAll()

const filteredMessages = allMessages.filter((message) =>
hasMissingTranslations(message, sourceLanguageTag, targetLanguageTags)
)

const bar = options.nobar
? undefined
Expand All @@ -111,7 +117,7 @@ export async function translateCommandAction(args: { project: InlangProject }) {
progessBar.Presets.shades_grey
)

bar?.start(messages.length, 0)
bar?.start(filteredMessages.length, 0)

const logs: Array<() => void> = []

Expand All @@ -132,17 +138,21 @@ export async function translateCommandAction(args: { project: InlangProject }) {
translatedMessage &&
translatedMessage?.variants.length > toBeTranslatedMessage.variants.length
) {
args.project.query.messages.update({
where: { id: translatedMessage.id },
data: translatedMessage!,
})
if (v2Persistence) {
await args.project.store!.messageBundles.set({ data: fromV1Message(translatedMessage) })
} else {
args.project.query.messages.update({
where: { id: translatedMessage.id },
data: translatedMessage!,
})
}
logs.push(() => log.info(`Machine translated message ${logId}`))
}
bar?.increment()
}
// parallelize rpcTranslate calls with a limit of 100 concurrent calls
const limit = plimit(100)
const promises = messages.map((message) => limit(() => rpcTranslate(message)))
const promises = filteredMessages.map((message) => limit(() => rpcTranslate(message)))
await Promise.all(promises)

bar?.stop()
Expand Down
10 changes: 6 additions & 4 deletions inlang/source-code/sdk/multi-project-test/multi-project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { fileURLToPath } from "node:url"
import { dirname, join } from "node:path"
import childProcess from "node:child_process"
import fs from "node:fs/promises"
import { fromV1Message } from "../src/v2/shim.js"

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
Expand Down Expand Up @@ -79,16 +80,17 @@ describe.concurrent(
)

// skip pending new v2 persistence with translation.
it.skip(
it(
"project4 in project4-dir",
async () => {
const before = await fs.readFile(
join(__dirname, "project4-dir", "messages.json.bak"),
"utf8"
)
const messageBundles = JSON.parse(before).map(fromV1Message)
await fs.writeFile(
join(__dirname, "project4-dir", "project.inlang", "messages.json"),
before
JSON.stringify(messageBundles, undefined, "2")
)
await run("pnpm translate4")
const expected = await fs.readFile(
Expand All @@ -101,10 +103,10 @@ describe.concurrent(
)
expect(result).toEqual(expected)
},
{ timeout: 20000 }
{ timeout: 2000000 }
)
},
{ timeout: 40000 }
{ timeout: 4000000 }
)

// run command in __dirname
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import { deepmerge } from "deepmerge-ts"
import { TypeCompiler } from "@sinclair/typebox/compiler"
import { tryCatch } from "@inlang/result"

import _debug from "debug"
const debug = _debug("sdk:resolvePlugins")

// @ts-ignore - type mismatch error
const PluginCompiler = TypeCompiler.Compile(Plugin)

Expand All @@ -26,6 +29,11 @@ export const resolvePlugins: ResolvePluginsFunction = async (args) => {
errors: [],
}

const experimentalPersistence = !!args.settings.experimental?.persistence
if (experimentalPersistence) {
debug("Using experimental persistence")
}

for (const plugin of args.plugins) {
const errors = [...PluginCompiler.Errors(plugin)]

Expand Down Expand Up @@ -110,8 +118,9 @@ export const resolvePlugins: ResolvePluginsFunction = async (args) => {
// --- LOADMESSAGE / SAVEMESSAGE NOT DEFINED ---

if (
typeof result.data.loadMessages !== "function" ||
typeof result.data.saveMessages !== "function"
!experimentalPersistence &&
(typeof result.data.loadMessages !== "function" ||
typeof result.data.saveMessages !== "function")
) {
result.errors.push(new PluginsDoNotProvideLoadOrSaveMessagesError())
}
Expand Down
1 change: 1 addition & 0 deletions inlang/source-code/sdk/src/v2/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export type * from "./types.js"
export * from "./shim.js"

0 comments on commit df2b2e1

Please sign in to comment.