Skip to content

Commit e92365d

Browse files
committed
nextjs plugin
1 parent 64165ec commit e92365d

File tree

15 files changed

+3662
-26
lines changed

15 files changed

+3662
-26
lines changed

.DS_Store

-2 KB
Binary file not shown.

package-lock.json

Lines changed: 963 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"workspaces": [
3737
"packages/config",
3838
"packages/core",
39+
"packages/nextjs-dev-translate-plugin",
3940
"packages/cli",
4041
"packages/testground"
4142
]
42-
}
43+
}

packages/cli/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@aexol/dev-translate",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"private": false,
55
"main": "./lib/index.js",
66
"author": "Aexol, Artur Czemiel",
@@ -17,10 +17,10 @@
1717
"lib"
1818
],
1919
"dependencies": {
20-
"@aexol/dev-translate-config": "^0.0.3",
21-
"@aexol/dev-translate-core": "^0.0.3",
20+
"@aexol/dev-translate-config": "^0.0.4",
21+
"@aexol/dev-translate-core": "^0.0.4",
2222
"chalk": "^5.3.0",
2323
"chokidar": "^3.6.0",
2424
"commander": "^11.0.0"
2525
}
26-
}
26+
}

packages/config/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@aexol/dev-translate-config",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"private": false,
55
"main": "./lib/index.js",
66
"author": "Aexol, Artur Czemiel",

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@aexol/dev-translate-core",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"private": false,
55
"main": "./lib/index.js",
66
"author": "Aexol, Artur Czemiel",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": [
3+
"../../.eslintrc.json"
4+
],
5+
"rules":{
6+
"@typescript-eslint/no-explicit-any":"off"
7+
}
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.env
2+
/lib
3+
/commonjs
4+
node_modules
5+
.graphql-editor-auth.json
6+
tsconfig.tsbuildinfo
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import chokidar from 'chokidar';
2+
import path from 'path';
3+
import { NextConfig } from 'next';
4+
import { LangPair, translateLocaleFolder } from '@aexol/dev-translate-core';
5+
6+
export type DevTranslateOptions = {
7+
apiKey: string;
8+
folderName: string;
9+
lang: LangPair['lang'];
10+
localeDir: string;
11+
};
12+
13+
const setupFileWatcher = async (opts: {
14+
apiKey: string;
15+
folderName: string;
16+
lang: LangPair['lang'];
17+
localeDir: string;
18+
}) => {
19+
const { apiKey, folderName, lang, localeDir } = opts;
20+
const directoryToWatch = path.join(process.cwd(), localeDir, opts.folderName);
21+
const translate = async () => {
22+
const result = await translateLocaleFolder({
23+
srcLang: {
24+
folderName,
25+
lang,
26+
},
27+
apiKey,
28+
cwd: process.cwd(),
29+
localeDir,
30+
});
31+
console.log(JSON.stringify(result, null, 2));
32+
};
33+
const watcher = chokidar.watch(directoryToWatch, {
34+
persistent: true,
35+
});
36+
37+
watcher.on('change', (filePath: string) => {
38+
console.log(`File changed: ${filePath}`);
39+
translate();
40+
});
41+
42+
watcher.on('add', (filePath: string) => {
43+
console.log(`File added: ${filePath}`);
44+
translate();
45+
});
46+
47+
watcher.on('unlink', (filePath: string) => {
48+
console.log(`File removed: ${filePath}`);
49+
translate();
50+
});
51+
52+
console.log(`Watching for file changes in ${directoryToWatch}`);
53+
};
54+
55+
// Plugin function to be used in next.config.js
56+
export function withDevTranslate(nextConfig: NextConfig = {}, options: DevTranslateOptions): NextConfig {
57+
setupFileWatcher(options);
58+
return {
59+
...nextConfig,
60+
webpack(config, options) {
61+
// Optionally, customize the webpack configuration
62+
63+
// Don't forget to include other plugins' webpack modification
64+
if (typeof nextConfig.webpack === 'function') {
65+
return nextConfig.webpack(config, options);
66+
}
67+
68+
return config;
69+
},
70+
};
71+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "@aexol/nextjs-dev-translate-plugin",
3+
"version": "0.0.4",
4+
"private": false,
5+
"main": "./lib/index.js",
6+
"author": "Aexol, Artur Czemiel",
7+
"type": "module",
8+
"scripts": {
9+
"build": "tspc && tspc -p tsconfig.commonjs.json",
10+
"start": "tspc --watch",
11+
"lint": "tspc && eslint \"./src/**/*.{ts,js}\" --quiet --fix"
12+
},
13+
"files": [
14+
"lib",
15+
"commonjs"
16+
],
17+
"dependencies": {
18+
"@aexol/dev-translate-core": "^0.0.4",
19+
"chokidar": "^3.6.0"
20+
},
21+
"peerDependencies": {
22+
"next": ">=13"
23+
},
24+
"devDependencies": {
25+
"next": "^15.1.4"
26+
},
27+
"exports": {
28+
"import": "./lib/index.js",
29+
"require": "./commonjs/index.js"
30+
}
31+
}

0 commit comments

Comments
 (0)