-
-
Save timofei-iatsenko/7ef13a5f6bae83f5550166a51a1e426e to your computer and use it in GitHub Desktop.
| # 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()) |
Thanks for this, saved my day! 🥇
The following is an adaptation to
- run on plain node instead of ts-node, and
- convert legacy
linguiformat JSON files.
const {
getConfig,
} = require("@lingui/conf");
const { getCatalogs } = require("@lingui/cli/api");
const { formatter: createFormatter } = require("@lingui/format-json");
const fs = require("fs/promises");
const { generateMessageId } = require("@lingui/message-utils/generateMessageId");
function _isGeneratedId(id, message) {
return id === generateMessageId(message.message, message.context);
}
async function command(config) {
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.translation =
translationCatalog[oldId].translation;
}
}
await catalog.write(locale, extractedCatalog);
}
})
);
}
command(getConfig());I guess i need to install more packages to get this working @MarttiR , lingui/format-json etc?
@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.

Please leave your feedback here. I will try to keep this gist updated.