@@ -17,7 +17,9 @@ export default function createPoLoader(
1717 return composeLoaders ( createPoDataLoader ( params ) , createPoContentLoader ( ) ) ;
1818}
1919
20- export function createPoDataLoader ( params : PoLoaderParams ) : ILoader < string , PoTranslationEntry > {
20+ export function createPoDataLoader (
21+ params : PoLoaderParams ,
22+ ) : ILoader < string , PoTranslationEntry > {
2123 return createLoader ( {
2224 async pull ( locale , input ) {
2325 const parsedPo = gettextParser . po . parse ( input ) ;
@@ -43,7 +45,8 @@ export function createPoDataLoader(params: PoLoaderParams): ILoader<string, PoTr
4345 async push ( locale , data , originalInput , originalLocale , pullInput ) {
4446 // Parse each section to maintain structure
4547 const currentSections = pullInput ?. split ( "\n\n" ) . filter ( Boolean ) || [ ] ;
46- const originalSections = originalInput ?. split ( "\n\n" ) . filter ( Boolean ) || [ ] ;
48+ const originalSections =
49+ originalInput ?. split ( "\n\n" ) . filter ( Boolean ) || [ ] ;
4750 const result = originalSections
4851 . map ( ( section ) => {
4952 const sectionPo = gettextParser . po . parse ( section ) ;
@@ -56,7 +59,9 @@ export function createPoDataLoader(params: PoLoaderParams): ILoader<string, PoTr
5659 const csPo = gettextParser . po . parse ( cs ) ;
5760 const csContextKey = _ . keys ( csPo . translations ) [ 0 ] ;
5861 const csEntries = csPo . translations [ csContextKey ] ;
59- const csMsgid = Object . keys ( csEntries ) . find ( ( key ) => csEntries [ key ] . msgid ) ;
62+ const csMsgid = Object . keys ( csEntries ) . find (
63+ ( key ) => csEntries [ key ] . msgid ,
64+ ) ;
6065 return csMsgid === msgid ;
6166 } ) ;
6267
@@ -78,7 +83,10 @@ export function createPoDataLoader(params: PoLoaderParams): ILoader<string, PoTr
7883 return gettextParser . po
7984 . compile ( updatedPo , { foldLength : params . multiline ? 76 : false } )
8085 . toString ( )
81- . replace ( [ `msgid ""` , `msgstr "Content-Type: text/plain\\n"` ] . join ( "\n" ) , "" )
86+ . replace (
87+ [ `msgid ""` , `msgstr "Content-Type: text/plain\\n"` ] . join ( "\n" ) ,
88+ "" ,
89+ )
8290 . trim ( ) ;
8391 }
8492 return section . trim ( ) ;
@@ -89,19 +97,33 @@ export function createPoDataLoader(params: PoLoaderParams): ILoader<string, PoTr
8997 } ) ;
9098}
9199
92- export function createPoContentLoader ( ) : ILoader < PoTranslationEntry , Record < string , PoTranslationEntry > > {
100+ export function createPoContentLoader ( ) : ILoader <
101+ PoTranslationEntry ,
102+ Record < string , PoTranslationEntry >
103+ > {
93104 return createLoader ( {
94- async pull ( locale , input ) {
105+ async pull ( locale , input , initCtx , originalLocale ) {
95106 const result = _ . chain ( input )
96107 . entries ( )
97108 . filter ( ( [ , entry ] ) => ! ! entry . msgid )
98- . map ( ( [ , entry ] ) => [
99- entry . msgid ,
100- {
101- singular : entry . msgstr [ 0 ] || entry . msgid ,
102- plural : ( entry . msgstr [ 1 ] || entry . msgid_plural || null ) as string | null ,
103- } ,
104- ] )
109+ . map ( ( [ , entry ] ) => {
110+ const singularFallback =
111+ locale === originalLocale ? entry . msgid : null ;
112+ const pluralFallback =
113+ locale === originalLocale
114+ ? entry . msgid_plural || entry . msgid
115+ : null ;
116+ const hasPlural = entry . msgstr . length > 1 ;
117+ return [
118+ entry . msgid ,
119+ {
120+ singular : entry . msgstr [ 0 ] || singularFallback ,
121+ plural : hasPlural
122+ ? ( ( entry . msgstr [ 1 ] || pluralFallback ) as string | null )
123+ : null ,
124+ } ,
125+ ] ;
126+ } )
105127 . fromPairs ( )
106128 . value ( ) ;
107129 return result ;
@@ -113,7 +135,10 @@ export function createPoContentLoader(): ILoader<PoTranslationEntry, Record<stri
113135 entry . msgid ,
114136 {
115137 ...entry ,
116- msgstr : [ data [ entry . msgid ] ?. singular , data [ entry . msgid ] ?. plural || null ] . filter ( Boolean ) ,
138+ msgstr : [
139+ data [ entry . msgid ] ?. singular ,
140+ data [ entry . msgid ] ?. plural || null ,
141+ ] . filter ( Boolean ) ,
117142 } ,
118143 ] )
119144 . fromPairs ( )
0 commit comments