Files
web/apps/scandic-web/i18n/tooling/formatter.ts
Joakim Jäderberg daf765f3d5 Merged in feature/wrap-logging (pull request #2511)
Feature/wrap logging

* feat: change all logging to go through our own logger function so that we can control log levels

* move packages/trpc to using our own logger

* merge


Approved-by: Linus Flood
2025-07-03 12:37:04 +00:00

102 lines
2.6 KiB
TypeScript

// https://docs.lokalise.com/en/articles/3229161-structured-json
import { logger } from "@scandic-hotels/common/logger"
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") {
logger.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 {
logger.warn(
`Unsupported type for context, expected 'string', got ${typeof context}`,
msg
)
}
}
if (limit) {
if (limit && typeof limit === "number") {
entry.limit = limit
} else {
logger.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 {
logger.warn(
`Unsupported type for tags, expected Array, got ${typeof tags}`,
msg
)
}
}
}
}
results[id] = entry
} else {
logger.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
}