@@ -49,7 +49,6 @@ export function createPoDataLoader(
4949
5050 async push ( locale , data , originalInput , originalLocale , pullInput ) {
5151 // Parse each section to maintain structure
52- const currentSections = pullInput ?. split ( "\n\n" ) . filter ( Boolean ) || [ ] ;
5352 const originalSections =
5453 originalInput ?. split ( "\n\n" ) . filter ( Boolean ) || [ ] ;
5554 const result = originalSections
@@ -63,45 +62,40 @@ export function createPoDataLoader(
6362 const contextKey = _ . keys ( sectionPo . translations ) [ 0 ] ;
6463 const entries = sectionPo . translations [ contextKey ] ;
6564 const msgid = Object . keys ( entries ) . find ( ( key ) => entries [ key ] . msgid ) ;
66-
67- // If the section is empty, try to find it in the current sections
68- const currentSection = currentSections . find ( ( cs ) => {
69- const csPo = gettextParser . po . parse ( cs ) ;
70- if ( Object . keys ( csPo . translations ) . length === 0 ) {
71- return false ;
72- }
73- const csContextKey = _ . keys ( csPo . translations ) [ 0 ] ;
74- const csEntries = csPo . translations [ csContextKey ] ;
75- if ( ! csEntries ) {
76- return false ;
77- }
78- const csMsgid = Object . keys ( csEntries ) . find (
79- ( key ) => csEntries [ key ] . msgid ,
80- ) ;
81- return csMsgid === msgid ;
82- } ) ;
8365
8466 if ( ! msgid ) {
85- if ( currentSection ) {
86- return currentSection ;
67+ // If the section is empty, try to find it in the current sections
68+ const currentSections =
69+ pullInput ?. split ( "\n\n" ) . filter ( Boolean ) || [ ] ;
70+ const currentSection = currentSections . find ( ( cs ) => {
71+ const csPo = gettextParser . po . parse ( cs ) ;
72+ if ( Object . keys ( csPo . translations ) . length === 0 ) {
73+ return false ;
74+ }
75+ const csContextKey = _ . keys ( csPo . translations ) [ 0 ] ;
76+ const csEntries = csPo . translations [ csContextKey ] ;
77+ if ( ! csEntries ) {
78+ return false ;
79+ }
80+ const csMsgid = Object . keys ( csEntries ) . find (
81+ ( key ) => csEntries [ key ] . msgid ,
82+ ) ;
83+ return csMsgid === msgid ;
84+ } ) ;
85+ return currentSection || section ;
86+ }
87+
88+ const entriesToMerge : Record < string , { msgstr : string [ ] } > = { } ;
89+ for ( const [ id , entry ] of Object . entries ( entries ) ) {
90+ if ( entry . msgid && data [ id ] ) {
91+ entriesToMerge [ id ] = { msgstr : data [ id ] . msgstr } ;
8792 }
88- return section ;
8993 }
90- if ( data [ msgid ] ) {
91- // Preserve headers from the target file
92- const headers = currentSection
93- ? gettextParser . po . parse ( currentSection ) . headers
94- : sectionPo . headers ;
9594
95+ if ( Object . keys ( entriesToMerge ) . length > 0 ) {
9696 const updatedPo = _ . merge ( { } , sectionPo , {
97- headers,
98- translations : {
99- [ contextKey ] : {
100- [ msgid ] : {
101- msgstr : data [ msgid ] . msgstr ,
102- } ,
103- } ,
104- } ,
97+ headers : resolveTargetHeaders ( pullInput , sectionPo ) ,
98+ translations : { [ contextKey ] : entriesToMerge } ,
10599 } ) ;
106100 const updatedSection = gettextParser . po
107101 . compile ( updatedPo , { foldLength : params . multiline ? 76 : false } )
@@ -174,6 +168,23 @@ export function createPoContentLoader(): ILoader<
174168 } ) ;
175169}
176170
171+ function resolveTargetHeaders (
172+ pullInput : string | null | undefined ,
173+ sectionPo : GetTextTranslations ,
174+ ) : Record < string , string > | undefined {
175+ // Only needed for embedded headers (header entry + regular entries in the same section)
176+ if ( ! sectionPo . translations [ "" ] ?. [ "" ] || ! pullInput ) {
177+ return undefined ;
178+ }
179+ const headerSection = pullInput
180+ . split ( "\n\n" )
181+ . find ( ( s ) => s . includes ( 'msgid ""' ) ) ;
182+ if ( ! headerSection ) {
183+ return undefined ;
184+ }
185+ return gettextParser . po . parse ( headerSection ) . headers ;
186+ }
187+
177188function preserveCommentOrder ( section : string , originalSection : string ) {
178189 // Split both sections into lines
179190 const sectionLines = section . split ( / \r ? \n / ) ;
0 commit comments