feat(SW-706): add Lokalise tooling and codemod

This commit is contained in:
Michael Zetterberg
2025-03-12 07:02:49 +01:00
parent 1c5b116ed8
commit e22fc1f3c8
26 changed files with 1478 additions and 130 deletions

View File

@@ -0,0 +1,99 @@
// https://docs.lokalise.com/en/articles/3229161-structured-json
import type { LokaliseMessageDescriptor } from "@/types/intl"
type TranslationEntry = {
translation: string
notes?: string
context?: string
limit?: number
tags?: string[]
}
type CompiledEntries = Record<string, string>
type LokaliseStructuredJson = Record<string, TranslationEntry>
export function format(
msgs: LokaliseMessageDescriptor[]
): LokaliseStructuredJson {
const results: LokaliseStructuredJson = {}
for (const [id, msg] of Object.entries(msgs)) {
const { defaultMessage, description } = msg
if (typeof defaultMessage === "string") {
const entry: TranslationEntry = {
translation: defaultMessage,
}
if (description) {
if (typeof description === "string") {
console.warn(
`Unsupported type for description, expected 'object', got ${typeof context}. Skipping!`,
msg
)
} else {
const { context, limit, tags } = description
if (context) {
if (typeof context === "string") {
entry.context = context
} else {
console.warn(
`Unsupported type for context, expected 'string', got ${typeof context}`,
msg
)
}
}
if (limit) {
if (limit && typeof limit === "number") {
entry.limit = limit
} else {
console.warn(
`Unsupported type for limit, expected 'number', got ${typeof limit}`,
msg
)
}
}
if (tags) {
if (tags && typeof tags === "string") {
const tagArray = tags.split(",").map((s) => s.trim())
if (tagArray.length) {
entry.tags = tagArray
}
} else {
console.warn(
`Unsupported type for tags, expected Array, got ${typeof tags}`,
msg
)
}
}
}
}
results[id] = entry
} else {
console.warn(
`Skipping message, unsupported type for defaultMessage, expected string, got ${typeof defaultMessage}`,
{
id,
msg,
}
)
}
}
return results
}
export function compile(msgs: LokaliseStructuredJson): CompiledEntries {
const results: CompiledEntries = {}
for (const [id, msg] of Object.entries(msgs)) {
results[id] = msg.translation
}
return results
}