1- // generate.ts
2- // eslint-disable no-console
3- //import "../../src/client/init";
1+ /* eslint-disable no-console */
42
53import * as fs from "fs" ;
64import { EOL } from "os" ;
@@ -11,6 +9,7 @@ import * as util from "util";
119import * as path from "path" ;
1210
1311type StringMap < V > = { [ key : string ] : V } ;
12+ type NestedStringMap < V > = StringMap < V > | StringMap < StringMap < V > > ;
1413
1514const writeFile = promisify ( fs . writeFile ) ;
1615
@@ -115,31 +114,34 @@ export class DevTranslationsLoader {
115114 ) {
116115 const content = await readFile ( path ) ;
117116 this . _mergeTranslations (
118- this . _flatten ( jsyaml . load ( content . toString ( ) ) ) ,
117+ this . _flatten (
118+ jsyaml . load ( content . toString ( ) ) as NestedStringMap < string >
119+ ) ,
119120 path ,
120121 duplicateCallback
121122 ) ;
122123 }
123124
124- private _flatten ( translations : unknown ) : StringMap < string > {
125- const output : StringMap < any > = { } ;
125+ private _flatten ( translations : NestedStringMap < string > ) : StringMap < string > {
126+ const output : StringMap < string > = { } ;
126127
127128 function step (
128- object : any ,
129+ object : NestedStringMap < string > ,
129130 prev : string | null = null ,
130131 currentDepth : number = 0
131132 ) {
132133 currentDepth = currentDepth || 1 ;
133134 for ( const key of Object . keys ( object ) ) {
134- const value = object [ key ] ;
135- const isString = typeof value === "string" ;
136-
137135 const newKey = prev ? prev + "." + key : key ;
138-
139- if ( ! isString && Object . keys ( value ) . length ) {
140- output [ newKey ] = step ( value , newKey , currentDepth + 1 ) ;
141- } else {
136+ const value = object [ key ] ;
137+ if ( typeof value === "string" ) {
142138 output [ newKey ] = value ;
139+ } else if ( value instanceof Object ) {
140+ if ( Object . keys ( value ) . length > 0 ) {
141+ step ( value , newKey , currentDepth + 1 ) ;
142+ }
143+ } else {
144+ throw new Error ( `Invalid translation value for ${ newKey } ` ) ;
143145 }
144146 }
145147 }
@@ -150,7 +152,7 @@ export class DevTranslationsLoader {
150152 }
151153
152154 private _mergeTranslations (
153- translations : StringMap < any > ,
155+ translations : StringMap < string > ,
154156 source : string ,
155157 duplicateCallback : DuplicateCallback
156158 ) {
0 commit comments