Skip to content

Instantly share code, notes, and snippets.

@thekip
Created June 23, 2023 09:27
Show Gist options
  • Save thekip/7ef13a5f6bae83f5550166a51a1e426e to your computer and use it in GitHub Desktop.
Save thekip/7ef13a5f6bae83f5550166a51a1e426e to your computer and use it in GitHub Desktop.
Convert explicit ids from v3 catalogs to v4
# Run it in the place where is your `lingui.config` is located
# ts-node ./migrate.ts
import {
CatalogType,
ExtractedMessageType,
getConfig,
LinguiConfigNormalized,
MessageType,
} from "@lingui/conf"
import { getCatalogs } from "@lingui/cli/api"
import { formatter as createFormatter } from "@lingui/format-po"
import fs from "fs/promises"
import { generateMessageId } from "@lingui/message-utils/generateMessageId"
function _isGeneratedId(id: string, message: ExtractedMessageType): boolean {
return id === generateMessageId(message.message, message.context)
}
async function command(
config: LinguiConfigNormalized,
) {
const catalogs = await getCatalogs(config)
const formatter = createFormatter({
explicitIdAsDefault: true,
})
await Promise.all(
catalogs.map(async (catalog) => {
const extractedCatalog = await catalog.collect()
for (const locale of config.locales) {
const translationFileName = catalog.getFilename(locale)
const translationCatalog = await formatter.parse(
await fs.readFile(translationFileName, "utf-8"),
{
locale,
sourceLocale: config.sourceLocale,
filename: translationFileName,
}
)
for (const messageId in extractedCatalog) {
const extractedMessage = extractedCatalog[messageId]
const isGeneratedId = _isGeneratedId(messageId, extractedMessage)
const oldId = isGeneratedId ? extractedMessage.message : messageId
if (translationCatalog[oldId]) {
;(extractedMessage as MessageType).translation =
translationCatalog[oldId].translation
}
}
await catalog.write(locale, extractedCatalog as CatalogType)
}
})
)
}
command(getConfig())
@MarttiR
Copy link

MarttiR commented Jun 14, 2024

@sterlingdcs-damian The versions were 4.4.0 for all relevant packages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment