22
33/**
44 * @fileoverview
5- * Extract i18n content from plugin.
5+ * Extract i18n content from a single plugin resource .
66 *
77 * Extraction sources:
8- * 1. package.json - openblock.l10n fields
9- * 2. main - formatMessage() calls
10- * 3. msg- Block message definitions
8+ * 1. package.json - openblock.name and openblock.description (formatMessage fields)
9+ * 2. main - formatMessage() calls in extension/device source files
10+ * 3. msg - Block message definitions
1111 *
1212 * Output files:
1313 * - .translations/interface/en.json
1414 * - .translations/extensions/en.json
1515 * - .translations/blocks/en.json
1616 *
1717 * Usage:
18- * node extract-translations .js [--dir=path/to/resources ]
18+ * node extract-format-message .js [--dir=path/to/plugin ]
1919 */
2020
2121const fs = require ( 'fs-extra' ) ;
@@ -183,88 +183,10 @@ const scanPlugin = pluginDir => {
183183 return result ;
184184} ;
185185
186- /**
187- * Scan all plugins in extensions/ and devices/ directories
188- * @param {string } workDir - Working directory
189- * @returns {{interface: object, extensions: object, blocks: object} } All extracted translations
190- */
191- const scanAllPlugins = workDir => {
192- const allResults = {
193- interface : { } ,
194- extensions : { } ,
195- blocks : { }
196- } ;
197-
198- const resourceTypes = [ 'extensions' , 'devices' ] ;
199-
200- for ( const type of resourceTypes ) {
201- const typeDir = path . join ( workDir , type ) ;
202- if ( ! fs . existsSync ( typeDir ) ) {
203- console . log ( `Skipping ${ type } / (not found)` ) ;
204- continue ;
205- }
206-
207- const plugins = fs . readdirSync ( typeDir , { withFileTypes : true } )
208- . filter ( dirent => dirent . isDirectory ( ) )
209- . map ( dirent => dirent . name ) ;
210-
211- console . log ( `Scanning ${ plugins . length } resouces in ${ type } /` ) ;
212-
213- for ( const pluginName of plugins ) {
214- const pluginDir = path . join ( typeDir , pluginName ) ;
215- const result = scanPlugin ( pluginDir ) ;
216-
217- // Merge interface messages
218- result . interface . forEach ( msg => {
219- allResults . interface [ msg . id ] = {
220- message : msg . default ,
221- description : msg . default
222- } ;
223- } ) ;
224-
225- // Merge extension messages
226- result . extensions . forEach ( msg => {
227- allResults . extensions [ msg . id ] = {
228- message : msg . default ,
229- description : msg . default
230- } ;
231- } ) ;
232-
233- // Merge block messages
234- Object . assign ( allResults . blocks , result . blocks ) ;
235- }
236- }
237-
238- return allResults ;
239- } ;
240-
241186// ============================================================================
242187// Main
243188// ============================================================================
244189
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-
268190/**
269191 * Main function to extract translations and write to files
270192 */
@@ -273,98 +195,61 @@ const main = () => {
273195 const workDir = path . resolve ( dir || './' ) ;
274196
275197 console . log ( `Working directory: ${ workDir } \n` ) ;
198+ console . log ( `Extracting translations from: ${ workDir } \n` ) ;
276199
277- // Check if workDir is a single plugin directory
278- const isSinglePlugin = isPluginDirectory ( workDir ) ;
200+ const result = scanPlugin ( workDir ) ;
201+ const totalKeys = result . interface . length + result . extensions . length + Object . keys ( result . blocks ) . length ;
279202
280- if ( isSinglePlugin ) {
281- // Single plugin mode
282- console . log ( 'Running in SINGLE RESOUCE mode\n' ) ;
283- console . log ( `Extracting translations from: ${ workDir } \n` ) ;
203+ if ( totalKeys === 0 ) {
204+ console . log ( '⚠ No translation keys found\n' ) ;
205+ return ;
206+ }
284207
285- const result = scanPlugin ( workDir ) ;
286- const totalKeys = result . interface . length + result . extensions . length + Object . keys ( result . blocks ) . length ;
208+ // Convert arrays to objects
209+ const results = {
210+ interface : { } ,
211+ extensions : { } ,
212+ blocks : result . blocks
213+ } ;
287214
288- if ( totalKeys === 0 ) {
289- console . log ( '⚠ No translation keys found\n' ) ;
290- return ;
291- }
215+ // Convert interface array to object
216+ result . interface . forEach ( msg => {
217+ results . interface [ msg . id ] = {
218+ message : msg . default ,
219+ description : msg . default
220+ } ;
221+ } ) ;
292222
293- // Convert arrays to objects for single plugin mode
294- const results = {
295- interface : { } ,
296- extensions : { } ,
297- blocks : result . blocks
223+ // Convert extensions array to object
224+ result . extensions . forEach ( msg => {
225+ results . extensions [ msg . id ] = {
226+ message : msg . default ,
227+ description : msg . default
298228 } ;
229+ } ) ;
299230
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- }
231+ // Write interface translations
232+ const interfacePath = path . join ( workDir , '.translations/interface/en.json' ) ;
233+ fs . ensureDirSync ( path . dirname ( interfacePath ) ) ;
234+ fs . writeJsonSync ( interfacePath , results . interface , { spaces : 4 } ) ;
235+ console . log ( `\n✓ Interface translations: ${ interfacePath } ` ) ;
236+ console . log ( ` ${ Object . keys ( results . interface ) . length } messages` ) ;
237+
238+ // Write extension translations
239+ const extensionsPath = path . join ( workDir , '.translations/extensions/en.json' ) ;
240+ fs . ensureDirSync ( path . dirname ( extensionsPath ) ) ;
241+ fs . writeJsonSync ( extensionsPath , results . extensions , { spaces : 4 } ) ;
242+ console . log ( `\n✓ Extension translations: ${ extensionsPath } ` ) ;
243+ console . log ( ` ${ Object . keys ( results . extensions ) . length } messages` ) ;
244+
245+ // Write block translations
246+ const blocksPath = path . join ( workDir , '.translations/blocks/en.json' ) ;
247+ fs . ensureDirSync ( path . dirname ( blocksPath ) ) ;
248+ fs . writeJsonSync ( blocksPath , results . blocks , { spaces : 4 } ) ;
249+ console . log ( `\n✓ Block translations: ${ blocksPath } ` ) ;
250+ console . log ( ` ${ Object . keys ( results . blocks ) . length } messages` ) ;
251+
252+ console . log ( '\n✓ Extraction complete!' ) ;
368253} ;
369254
370255main ( ) ;
0 commit comments