Skip to content

Commit fa340d2

Browse files
committed
The temporary translation data directory has been changed to .translation.
1 parent f07c37b commit fa340d2

4 files changed

Lines changed: 134 additions & 43 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openblock-registry-cli",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "CLI tool for publishing plugins to OpenBlock Registry",
55
"author": "OpenBlock Team",
66
"license": "MIT",

src/commands/i18n/extract-format-message.js

Lines changed: 124 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
* 3. msg- Block message definitions
1111
*
1212
* Output files:
13-
* - translations/interface/en.json
14-
* - translations/extensions/en.json
15-
* - translations/blocks/en.json
13+
* - .translations/interface/en.json
14+
* - .translations/extensions/en.json
15+
* - .translations/blocks/en.json
1616
*
1717
* Usage:
1818
* node extract-translations.js [--dir=path/to/resources]
@@ -36,9 +36,6 @@ const parseArgs = () => {
3636
return args;
3737
};
3838

39-
const {dir} = parseArgs();
40-
const workDir = dir || './';
41-
4239
// ============================================================================
4340
// Utilities
4441
// ============================================================================
@@ -188,9 +185,10 @@ const scanPlugin = pluginDir => {
188185

189186
/**
190187
* Scan all plugins in extensions/ and devices/ directories
188+
* @param {string} workDir - Working directory
191189
* @returns {{interface: object, extensions: object, blocks: object}} All extracted translations
192190
*/
193-
const scanAllPlugins = () => {
191+
const scanAllPlugins = workDir => {
194192
const allResults = {
195193
interface: {},
196194
extensions: {},
@@ -244,36 +242,129 @@ const scanAllPlugins = () => {
244242
// Main
245243
// ============================================================================
246244

245+
/**
246+
* Read package.json from plugin directory
247+
* @param {string} pluginDir - Plugin directory path
248+
* @returns {object|null} Package.json content or null
249+
*/
250+
const readPackageJson = pluginDir => {
251+
const packagePath = path.join(pluginDir, 'package.json');
252+
if (!fs.existsSync(packagePath)) {
253+
return null;
254+
}
255+
return fs.readJsonSync(packagePath);
256+
};
257+
258+
/**
259+
* Check if directory is a plugin directory (has package.json with openblock field)
260+
* @param {string} dir - Directory path
261+
* @returns {boolean} True if it's a plugin directory
262+
*/
263+
const isPluginDirectory = dir => {
264+
const pkg = readPackageJson(dir);
265+
return pkg && pkg.openblock && (pkg.openblock.extensionId || pkg.openblock.deviceId);
266+
};
267+
247268
/**
248269
* Main function to extract translations and write to files
249270
*/
250271
const main = () => {
251-
console.log(`Extracting translations from: ${path.resolve(workDir)}\n`);
252-
253-
const results = scanAllPlugins();
254-
255-
// Write interface translations
256-
const interfacePath = path.join(workDir, 'translations/interface/en.json');
257-
fs.ensureDirSync(path.dirname(interfacePath));
258-
fs.writeJsonSync(interfacePath, results.interface, {spaces: 4});
259-
console.log(`\n✓ Interface translations: ${interfacePath}`);
260-
console.log(` ${Object.keys(results.interface).length} messages`);
261-
262-
// Write extension translations
263-
const extensionsPath = path.join(workDir, 'translations/extensions/en.json');
264-
fs.ensureDirSync(path.dirname(extensionsPath));
265-
fs.writeJsonSync(extensionsPath, results.extensions, {spaces: 4});
266-
console.log(`\n✓ Extension translations: ${extensionsPath}`);
267-
console.log(` ${Object.keys(results.extensions).length} messages`);
268-
269-
// Write block translations
270-
const blocksPath = path.join(workDir, 'translations/blocks/en.json');
271-
fs.ensureDirSync(path.dirname(blocksPath));
272-
fs.writeJsonSync(blocksPath, results.blocks, {spaces: 4});
273-
console.log(`\n✓ Block translations: ${blocksPath}`);
274-
console.log(` ${Object.keys(results.blocks).length} messages`);
275-
276-
console.log('\n✓ Extraction complete!');
272+
const {dir} = parseArgs();
273+
const workDir = path.resolve(dir || './');
274+
275+
console.log(`Working directory: ${workDir}\n`);
276+
277+
// Check if workDir is a single plugin directory
278+
const isSinglePlugin = isPluginDirectory(workDir);
279+
280+
if (isSinglePlugin) {
281+
// Single plugin mode
282+
console.log('Running in SINGLE RESOUCE mode\n');
283+
console.log(`Extracting translations from: ${workDir}\n`);
284+
285+
const result = scanPlugin(workDir);
286+
const totalKeys = result.interface.length + result.extensions.length + Object.keys(result.blocks).length;
287+
288+
if (totalKeys === 0) {
289+
console.log('⚠ No translation keys found\n');
290+
return;
291+
}
292+
293+
// Convert arrays to objects for single plugin mode
294+
const results = {
295+
interface: {},
296+
extensions: {},
297+
blocks: result.blocks
298+
};
299+
300+
// Convert interface array to object
301+
result.interface.forEach(msg => {
302+
results.interface[msg.id] = {
303+
message: msg.default,
304+
description: msg.default
305+
};
306+
});
307+
308+
// Convert extensions array to object
309+
result.extensions.forEach(msg => {
310+
results.extensions[msg.id] = {
311+
message: msg.default,
312+
description: msg.default
313+
};
314+
});
315+
316+
// Write interface translations
317+
const interfacePath = path.join(workDir, '.translations/interface/en.json');
318+
fs.ensureDirSync(path.dirname(interfacePath));
319+
fs.writeJsonSync(interfacePath, results.interface, {spaces: 4});
320+
console.log(`\n✓ Interface translations: ${interfacePath}`);
321+
console.log(` ${Object.keys(results.interface).length} messages`);
322+
323+
// Write extension translations
324+
const extensionsPath = path.join(workDir, '.translations/extensions/en.json');
325+
fs.ensureDirSync(path.dirname(extensionsPath));
326+
fs.writeJsonSync(extensionsPath, results.extensions, {spaces: 4});
327+
console.log(`\n✓ Extension translations: ${extensionsPath}`);
328+
console.log(` ${Object.keys(results.extensions).length} messages`);
329+
330+
// Write block translations
331+
const blocksPath = path.join(workDir, '.translations/blocks/en.json');
332+
fs.ensureDirSync(path.dirname(blocksPath));
333+
fs.writeJsonSync(blocksPath, results.blocks, {spaces: 4});
334+
console.log(`\n✓ Block translations: ${blocksPath}`);
335+
console.log(` ${Object.keys(results.blocks).length} messages`);
336+
337+
console.log('\n✓ Extraction complete!');
338+
} else {
339+
// Multi-plugin mode
340+
console.log('Running in MULTI RESOUCE mode\n');
341+
console.log(`Extracting translations from: ${workDir}\n`);
342+
343+
const results = scanAllPlugins(workDir);
344+
345+
// Write interface translations
346+
const interfacePath = path.join(workDir, '.translations/interface/en.json');
347+
fs.ensureDirSync(path.dirname(interfacePath));
348+
fs.writeJsonSync(interfacePath, results.interface, {spaces: 4});
349+
console.log(`\n✓ Interface translations: ${interfacePath}`);
350+
console.log(` ${Object.keys(results.interface).length} messages`);
351+
352+
// Write extension translations
353+
const extensionsPath = path.join(workDir, '.translations/extensions/en.json');
354+
fs.ensureDirSync(path.dirname(extensionsPath));
355+
fs.writeJsonSync(extensionsPath, results.extensions, {spaces: 4});
356+
console.log(`\n✓ Extension translations: ${extensionsPath}`);
357+
console.log(` ${Object.keys(results.extensions).length} messages`);
358+
359+
// Write block translations
360+
const blocksPath = path.join(workDir, '.translations/blocks/en.json');
361+
fs.ensureDirSync(path.dirname(blocksPath));
362+
fs.writeJsonSync(blocksPath, results.blocks, {spaces: 4});
363+
console.log(`\n✓ Block translations: ${blocksPath}`);
364+
console.log(` ${Object.keys(results.blocks).length} messages`);
365+
366+
console.log('\n✓ Extraction complete!');
367+
}
277368
};
278369

279370
main();

src/commands/i18n/push-format-message.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
* Push extracted i18n content to Transifex.
66
*
77
* Pushes the following translation files to Transifex:
8-
* - translations/interface/en.json → openblock-resources/interface
9-
* - translations/extensions/en.json → openblock-resources/extensions
10-
* - translations/blocks/en.json → openblock-resources/blocks
8+
* - .translations/interface/en.json → openblock-resources/interface
9+
* - .translations/extensions/en.json → openblock-resources/extensions
10+
* - .translations/blocks/en.json → openblock-resources/blocks
1111
*
1212
* Usage:
1313
* node push-format-message.js [--dir=path/to/resources]
@@ -73,15 +73,15 @@ const main = () => {
7373
const resources = [
7474
{
7575
name: 'interface',
76-
file: path.join(workDirResolved, 'translations/interface/en.json')
76+
file: path.join(workDirResolved, '.translations/interface/en.json')
7777
},
7878
{
7979
name: 'extensions',
80-
file: path.join(workDirResolved, 'translations/extensions/en.json')
80+
file: path.join(workDirResolved, '.translations/extensions/en.json')
8181
},
8282
{
8383
name: 'blocks',
84-
file: path.join(workDirResolved, 'translations/blocks/en.json')
84+
file: path.join(workDirResolved, '.translations/blocks/en.json')
8585
}
8686
];
8787

src/commands/i18n/update-translations.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ const loadLocalTranslations = workDir => {
137137

138138
// Load en.json files
139139
const enFiles = {
140-
[RESOURCES.INTERFACE]: path.join(workDir, 'translations/interface/en.json'),
141-
[RESOURCES.EXTENSIONS]: path.join(workDir, 'translations/extensions/en.json'),
142-
[RESOURCES.BLOCKS]: path.join(workDir, 'translations/blocks/en.json')
140+
[RESOURCES.INTERFACE]: path.join(workDir, '.translations/interface/en.json'),
141+
[RESOURCES.EXTENSIONS]: path.join(workDir, '.translations/extensions/en.json'),
142+
[RESOURCES.BLOCKS]: path.join(workDir, '.translations/blocks/en.json')
143143
};
144144

145145
for (const [resource, filePath] of Object.entries(enFiles)) {

0 commit comments

Comments
 (0)