@@ -24,14 +24,18 @@ interface LocaleInfo {
2424const countries = new Map < string , Map < string , LocaleInfo > > ( )
2525const availableLocales = new Map < string , LocaleObject > ( )
2626
27- const extractLocalInfo = (
28- filePath : string ,
29- forCountry : boolean = false ,
30- mergeLocale : boolean = false ,
31- ) : LocaleInfo => {
27+ function extractLocalInfo ( filePath : string ) : LocaleInfo {
3228 const locale = basename ( filePath , '.json' )
3329 const [ lang , country ] = locale . split ( '-' )
34- return { filePath, locale, lang, country, forCountry, mergeLocale }
30+ return { filePath, locale, lang, country }
31+ }
32+
33+ function createVariantInfo (
34+ code : string ,
35+ options : { forCountry : boolean ; mergeLocale : boolean } ,
36+ ) : LocaleInfo {
37+ const [ lang , country ] = code . split ( '-' )
38+ return { filePath : '' , locale : code , lang, country, ...options }
3539}
3640
3741const populateLocaleCountries = ( ) : void => {
@@ -42,10 +46,22 @@ const populateLocaleCountries = (): void => {
4246 countries . set ( lang , new Map ( ) )
4347 }
4448 if ( variant . country ) {
45- countries . get ( lang ) ! . set ( lang , extractLocalInfo ( lang , true ) )
46- countries . get ( lang ) ! . set ( variant . code , extractLocalInfo ( variant . code , true , true ) )
49+ countries
50+ . get ( lang ) !
51+ . set ( lang , createVariantInfo ( lang , { forCountry : true , mergeLocale : false } ) )
52+ countries
53+ . get ( lang ) !
54+ . set (
55+ variant . code ,
56+ createVariantInfo ( variant . code , { forCountry : true , mergeLocale : true } ) ,
57+ )
4758 } else {
48- countries . get ( lang ) ! . set ( variant . code , extractLocalInfo ( variant . code , false , true ) )
59+ countries
60+ . get ( lang ) !
61+ . set (
62+ variant . code ,
63+ createVariantInfo ( variant . code , { forCountry : false , mergeLocale : true } ) ,
64+ )
4965 }
5066 }
5167 }
@@ -117,7 +133,7 @@ const loadJson = async ({ filePath, mergeLocale, locale }: LocaleInfo): Promise<
117133 console . error ( `${ COLORS . red } Error: Failed to merge locale "${ locale } "${ COLORS . reset } ` )
118134 process . exit ( 1 )
119135 }
120- return merged as NestedObject
136+ return merged
121137}
122138
123139type SyncStats = {
@@ -146,8 +162,9 @@ const syncLocaleData = (
146162 if ( isNested ( refValue ) ) {
147163 const nextTarget = isNested ( target [ key ] ) ? target [ key ] : { }
148164 const data = syncLocaleData ( refValue , nextTarget , stats , fix , propertyPath )
149- // don't add empty objects: --fix will prevent this
150- if ( Object . keys ( data ) . length === 0 ) {
165+ // When fixing, empty objects won't occur since missing keys get placeholders.
166+ // Without --fix, keep empty objects to preserve structural parity with the reference.
167+ if ( fix && Object . keys ( data ) . length === 0 ) {
151168 delete result [ key ]
152169 } else {
153170 result [ key ] = data
@@ -359,4 +376,4 @@ const run = async (): Promise<void> => {
359376 }
360377}
361378
362- run ( )
379+ await run ( )
0 commit comments