next-intl-codemod 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/dist/apply.js +23 -0
- package/dist/cli.js +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -56,6 +56,14 @@ t.rich('Please accept the <link>terms</link>.', {link: (chunks) => <a>{chunks}</
|
|
|
56
56
|
| `--dry-run` | | Report only |
|
|
57
57
|
| `--report <file>` | | JSON report: skipped bindings with reasons, conflicts, old keys never referenced, and `keyMap` (old key → new id) |
|
|
58
58
|
|
|
59
|
+
## Requirements
|
|
60
|
+
|
|
61
|
+
The `po` format needs **next-intl 4.14.0 or newer**. That release changed the PO layout, and older
|
|
62
|
+
extractors erase the translations of a catalog in the new layout on the next `next dev` or
|
|
63
|
+
`next build`, so the codemod stops if it finds an older version. In a monorepo, upgrade every
|
|
64
|
+
workspace package to the same version and migrate any other PO catalog that next-intl loads, such as
|
|
65
|
+
a shared package's. The codemod only checks the catalogs in `--out`.
|
|
66
|
+
|
|
59
67
|
## Choosing the source locale
|
|
60
68
|
|
|
61
69
|
Inline messages are written in one locale and that locale must be the `messages.sourceLocale` of
|
package/dist/apply.js
CHANGED
|
@@ -24,6 +24,29 @@ export function applyEdits(edits, textByFile) {
|
|
|
24
24
|
fs.writeFileSync(file, bom + text);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* next-intl 4.14.0 moved PO catalogs to the source text in `msgid` and the key in `msgctxt`. An older
|
|
29
|
+
* extractor reads that layout as namespace + key and rewrites the catalogs with every translation empty.
|
|
30
|
+
*/
|
|
31
|
+
export function assertNextIntlSupportsPo(options) {
|
|
32
|
+
if (options.format !== 'po')
|
|
33
|
+
return;
|
|
34
|
+
for (let dir = options.projectRoot;; dir = path.dirname(dir)) {
|
|
35
|
+
const file = path.join(dir, 'node_modules/next-intl/package.json');
|
|
36
|
+
if (fs.existsSync(file)) {
|
|
37
|
+
const { version } = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
38
|
+
const [major, minor] = version.split('.').map(Number);
|
|
39
|
+
if (major < 4 || (major === 4 && minor < 14)) {
|
|
40
|
+
throw new Error(`next-intl ${version} is installed, but PO catalogs written by this codemod need next-intl 4.14.0 or newer: ` +
|
|
41
|
+
'older extractors erase their translations on the next `next dev` or `next build`. ' +
|
|
42
|
+
'Upgrade next-intl (including workspace packages) or use --format json. Nothing was changed.');
|
|
43
|
+
}
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (dir === path.dirname(dir))
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
27
50
|
/** Merging into the previous layout would write a catalog next-intl refuses to read. */
|
|
28
51
|
export function assertCatalogsSupported(options) {
|
|
29
52
|
if (options.format !== 'po')
|
package/dist/cli.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import fs from 'node:fs';
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import { parseArgs } from 'node:util';
|
|
5
|
-
import { applyEdits, assertCatalogsSupported, writeCatalogs } from './apply.js';
|
|
5
|
+
import { applyEdits, assertCatalogsSupported, assertNextIntlSupportsPo, writeCatalogs } from './apply.js';
|
|
6
6
|
import { loadCatalogs } from './catalog.js';
|
|
7
7
|
import { collectSourceFiles } from './files.js';
|
|
8
8
|
import { buildPlan } from './plan.js';
|
|
@@ -47,6 +47,7 @@ if (!['po', 'json'].includes(format))
|
|
|
47
47
|
throw new Error(`Unknown format: ${format}`);
|
|
48
48
|
if (!['none', 'first-segment', 'keep'].includes(namespace))
|
|
49
49
|
throw new Error(`Unknown namespace strategy: ${namespace}`);
|
|
50
|
+
assertNextIntlSupportsPo({ projectRoot, format });
|
|
50
51
|
const catalogs = loadCatalogs(values.messages, projectRoot, values['source-locale']);
|
|
51
52
|
assertCatalogsSupported({ outDir: path.resolve(projectRoot, values.out), format, locales: catalogs.locales });
|
|
52
53
|
const files = [];
|