11import { readFile } from 'node:fs/promises' ;
22import jsonc from 'comment-json' ;
33import { defineConfig , Plugin } from 'i18next-cli' ;
4+ import { consolePlugin as consolePluginMetadata } from './package.json' ;
5+
6+ const EXPECTED_NAMESPACE = `plugin__${ consolePluginMetadata . name } ` ;
47
58/**
69 * Custom JSON parser for localizing keys matching format: /%.+%/
@@ -32,14 +35,27 @@ const consoleExtensionsPlugin = (): Plugin => ({
3235 }
3336
3437 for ( const { key : fullKey } of extracted ) {
35- const [ ns , key ] = fullKey . split ( '~' , 2 ) ;
36-
37- if ( ns && key ) {
38+ const sep = fullKey . indexOf ( '~' ) ;
39+ if ( sep > 0 && sep < fullKey . length - 1 ) {
40+ const ns = fullKey . slice ( 0 , sep ) ;
41+ const key = fullKey . slice ( sep + 1 ) ;
3842 keys . set ( `${ ns } :${ key } ` , { key, defaultValue : key , ns } ) ;
3943 } else {
4044 console . warn ( `Invalid key format: ${ fullKey } ` ) ;
4145 }
4246 }
47+
48+ // Validate keys have the correct namespace
49+ const mismatchedKeys = [ ...keys . entries ( ) ] . filter ( ( [ fullKey , { ns } ] ) => {
50+ const fullNs = fullKey . split ( ':' ) [ 0 ] ;
51+ return ! ns || fullNs !== EXPECTED_NAMESPACE ;
52+ } ) . map ( ( [ fullKey ] ) => fullKey ) ;
53+ if ( mismatchedKeys . length > 0 ) {
54+ throw new Error (
55+ `Found ${ mismatchedKeys . length } keys with mismatched namespace. Expected namespace: ${ EXPECTED_NAMESPACE }
56+ ${ mismatchedKeys . map ( ( key ) => ` - ${ key } ` ) . join ( '\n' ) } `,
57+ ) ;
58+ }
4359 } ,
4460} ) ;
4561
@@ -49,10 +65,11 @@ export default defineConfig({
4965 input : 'src/**/*.{js,jsx,ts,tsx}' ,
5066 output : 'locales/{{language}}/{{namespace}}.json' ,
5167
68+ defaultValue : ( key ) => key . replace ( / _ (?: o n e | o t h e r ) $ / , '' ) ,
5269 sort : true ,
5370 keySeparator : false ,
5471 nsSeparator : '~' ,
55- defaultNS : 'plugin__console-plugin-template' , // TODO: Change me!
72+ defaultNS : EXPECTED_NAMESPACE ,
5673 } ,
5774 plugins : [ consoleExtensionsPlugin ( ) ] ,
5875} ) ;
0 commit comments