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 */
250271const 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
279370main ( ) ;
0 commit comments