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())
@sterlingdcs-damian
Copy link

I guess i need to install more packages to get this working @MarttiR , lingui/format-json etc?

@sterlingdcs-damian
Copy link

@thekip the imports dont seem to exist on the latest

 "@lingui/core": "4.11.1",
  "@lingui/macro": "4.11.1",
  "@lingui/react": "4.11.1",

image

@thekip
Copy link
Author

thekip commented Jun 13, 2024

@sterlingdcs-damian this imports points to the typings, simply drop them and also references in the code to these types. They probably renamed and i don't remember what version was actual when i wrote this migration.

@MarttiR
Copy link

MarttiR commented Jun 13, 2024

@sterlingdcs-damian You could also consider installing an older version, doing the migration, and upgrading after - the migration code is not useful after that point anyway.

@sterlingdcs-damian
Copy link

ok thanks both, @MarttiR , do you know what version you ran it on? im currently on 3.9.0 pre-upgrade

@thekip
Copy link
Author

thekip commented Jun 14, 2024

try 4.5 or +/- one minor

@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