Created
June 23, 2023 09:27
-
-
Save thekip/7ef13a5f6bae83f5550166a51a1e426e to your computer and use it in GitHub Desktop.
Convert explicit ids from v3 catalogs to v4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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()) |
@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",
@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.
@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.
ok thanks both, @MarttiR , do you know what version you ran it on? im currently on 3.9.0 pre-upgrade
try 4.5 or +/- one minor
@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
I guess i need to install more packages to get this working @MarttiR , lingui/format-json etc?