Skip to content

Commit

Permalink
Merge pull request #2108 from opral/1844-sdk-persistence-of-messages-…
Browse files Browse the repository at this point in the history
…in-project-direcory

WIP 1844 Part 1: auto-generated human-IDs and aliases
  • Loading branch information
jldec authored Mar 12, 2024
2 parents 19d6815 + c5fc396 commit be81a4e
Show file tree
Hide file tree
Showing 82 changed files with 3,198 additions and 2,813 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-chairs-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inlang/paraglide-js": minor
---

paraglide deprecate aliases
23 changes: 23 additions & 0 deletions .changeset/twenty-terms-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
"@inlang/project-settings": minor
"@inlang/message": minor
"@inlang/paraglide-js": minor
"vs-code-extension": minor
"@inlang/cli": minor
"@inlang/rpc": minor
"@inlang/sdk": minor
"@lix-js/client": minor
"@inlang/message-lint-rule-without-source": patch
"@inlang/message-lint-rule-missing-translation": patch
"@inlang/message-lint-rule-identical-pattern": patch
"@inlang/message-lint-rule-empty-pattern": patch
"@inlang/message-lint-rule-snake-case-id": patch
"@inlang/plugin-message-format": patch
"@inlang/plugin-next-intl": patch
"@inlang/plugin-i18next": patch
"@inlang/plugin-json": patch
"@inlang/badge": patch
---

File locking for concurrent message updates through the load/store plugin api
Auto-generated human-IDs and aliases - only with experimental: { aliases: true }
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ it("should work with multiple resources", () => {
export const createMessage = (id: string, patterns: Record<string, Pattern | string>) =>
({
id,
alias: {},
selectors: [],
variants: Object.entries(patterns).map(([languageTag, patterns]) => ({
languageTag,
Expand Down
2 changes: 2 additions & 0 deletions inlang/source-code/cli/src/commands/lint/lint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { mockRepo } from "@lix-js/client"
const exampleMessages: Message[] = [
{
id: "a",
alias: {},
selectors: [],
variants: [
{
Expand All @@ -29,6 +30,7 @@ const exampleMessages: Message[] = [
},
{
id: "b",
alias: {},
selectors: [],
variants: [
{
Expand Down
26 changes: 18 additions & 8 deletions inlang/source-code/cli/src/commands/machine/translate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ test.runIf(process.env.GOOGLE_TRANSLATE_API_KEY)(
}
}
}
}
},
{ timeout: 10000 }
)

test.runIf(process.env.GOOGLE_TRANSLATE_API_KEY)(
Expand All @@ -82,6 +83,7 @@ test.runIf(process.env.GOOGLE_TRANSLATE_API_KEY)(
const exampleMessages: Message[] = [
{
id: "a",
alias: {},
selectors: [],
variants: [
{
Expand Down Expand Up @@ -144,11 +146,19 @@ test.runIf(process.env.GOOGLE_TRANSLATE_API_KEY)(
const messages = project.query.messages.getAll()

expect(messages[0]?.variants.length).toBe(2)
expect(messages[0]?.variants[1]?.languageTag).toBe("de")
expect(
messages[0]?.variants[1]?.pattern.some(
(value) => value.type === "VariableReference" && value.name === "username"
)
).toBeTruthy()
}
expect(messages[0]?.variants.map((variant) => variant.languageTag).sort()).toStrictEqual([
"de",
"en",
])

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- we expected the message to exist earlier
for (const variant of messages[0]!.variants) {
expect(
variant.pattern.some(
(value) => value.type === "VariableReference" && value.name === "username"
)
).toBeTruthy()
}
},
{ timeout: 10000 }
)
18 changes: 10 additions & 8 deletions inlang/source-code/cli/src/commands/machine/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Command } from "commander"
import { rpc } from "@inlang/rpc"
import { getInlangProject } from "../../utilities/getInlangProject.js"
import { log } from "../../utilities/log.js"
import { log, logError } from "../../utilities/log.js"
import { type InlangProject, ProjectSettings, Message } from "@inlang/sdk"
import prompts from "prompts"
import { projectOption } from "../../utilities/globalFlags.js"
Expand Down Expand Up @@ -40,7 +40,7 @@ export const translate = new Command()
const project = await getInlangProject({ projectPath: args.project })
await translateCommandAction({ project })
} catch (error) {
log.error(error)
logError(error)
}
})

Expand All @@ -53,6 +53,7 @@ export async function translateCommandAction(args: { project: InlangProject }) {
log.error(`No inlang project found`)
return
}
const experimentalAliases = args.project.settings().experimental?.aliases

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

Expand Down Expand Up @@ -106,20 +107,24 @@ export async function translateCommandAction(args: { project: InlangProject }) {

const rpcTranslate = async (id: Message["id"]) => {
const toBeTranslatedMessage = args.project.query.messages.get({ where: { id } })!
const logId =
`"${id}"` +
(experimentalAliases ? ` (alias "${toBeTranslatedMessage.alias.default ?? ""}")` : "")

const { data: translatedMessage, error } = await rpc.machineTranslateMessage({
message: toBeTranslatedMessage,
sourceLanguageTag,
targetLanguageTags,
})
if (error) {
logs.push(() => log.error(`Couldn't translate message "${id}": ${error}`))
logs.push(() => log.error(`Couldn't translate message ${logId}: ${error}`))
return
} else if (
translatedMessage &&
translatedMessage?.variants.length > toBeTranslatedMessage.variants.length
) {
args.project.query.messages.update({ where: { id: id }, data: translatedMessage! })
logs.push(() => log.info(`Machine translated message "${id}"`))
logs.push(() => log.info(`Machine translated message ${logId}`))
}
bar.increment()
}
Expand All @@ -133,12 +138,9 @@ export async function translateCommandAction(args: { project: InlangProject }) {
log()
}

// https://github.com/opral/monorepo/issues/1846
// https://github.com/opral/monorepo/issues/1968
await new Promise((resolve) => setTimeout(resolve, 8002))
// Log the message counts
log.success("Machine translate complete.")
} catch (error) {
log.error(error)
logError(error)
}
}
3 changes: 1 addition & 2 deletions inlang/source-code/cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,5 @@ export const cli = new Command()
version,
},
})
// https://github.com/tj/commander.js/issues/1745
process.exit(0)
// process should exit by itself once promises are resolved
})
14 changes: 14 additions & 0 deletions inlang/source-code/cli/src/utilities/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,17 @@ import consola from "consola"
* log.success("Success")
*/
export const log = consola

export function logError(error: any) {
log.error(causeString(error), error)
}

// Convert error.cause into a string for logging
export function causeString(error: any) {
if (typeof error === "object" && error.cause) {
if (error.cause.errors?.length) return error.cause.errors.join(", ")
if (error.cause.code) return "" + error.cause.code
return JSON.stringify(error.cause)
}
return ""
}
Loading

0 comments on commit be81a4e

Please sign in to comment.