Skip to content

Commit

Permalink
Align the number of plural forms to plural forms defined by a language.
Browse files Browse the repository at this point in the history
  • Loading branch information
pomek committed Oct 22, 2024
1 parent 36fb675 commit 866892c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ export default function synchronizeTranslationsBasedOnContext( { packageContexts

// (4.1) Update file headers.
const { languageCode, localeCode } = languages.find( language => language.localeCode === translations.headers.Language );

translations.headers = getHeaders( languageCode, localeCode );

const numberOfPluralForms = parseInt( PO.parsePluralForms( translations.headers[ 'Plural-Forms' ] ).nplurals );

// (4.2) Remove unused translations.
translations.items = translations.items.filter( item => contextContent[ item.msgid ] );

Expand All @@ -59,7 +62,6 @@ export default function synchronizeTranslationsBasedOnContext( { packageContexts
...sourceMessagesForPackage
.filter( message => !translations.items.find( item => item.msgid === message.id ) )
.map( message => {
const numberOfPluralForms = PO.parsePluralForms( translations.headers[ 'Plural-Forms' ] ).nplurals;
const item = new PO.Item( { nplurals: numberOfPluralForms } );

item.msgctxt = contextContent[ message.id ];
Expand All @@ -75,9 +77,19 @@ export default function synchronizeTranslationsBasedOnContext( { packageContexts
} )
);

// (4.4) Align the number of plural forms to plural forms defined by a language.
translations.items = translations.items.map( item => {
if ( item.msgid_plural ) {
item.msgstr = [ ...Array( numberOfPluralForms ) ]
.map( ( value, index ) => item.msgstr[ index ] || '' );
}

return item;
} );

const translationFileUpdated = cleanTranslationFileContent( translations ).toString();

// (4.4) Save translation file only if it has been updated.
// (4.5) Save translation file only if it has been updated.
if ( translationFile === translationFileUpdated ) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,76 @@ describe( 'synchronizeTranslationsBasedOnContext()', () => {
] );
} );

it( 'should remove existing plural forms if a source file contains more than a language defines', () => {
translations.items = [
{
msgid: 'id1',
msgctxt: 'Context for example message 1',
msgid_plural: '',
msgstr: [ '' ]
},
{
msgid: 'id2',
msgctxt: 'Context for example message 2',
msgid_plural: 'Example message 2 - plural form',
msgstr: [ '1', '2', '3', '4', '5', '6' ]
}
];

synchronizeTranslationsBasedOnContext( defaultOptions );

expect( translations.items ).toEqual( [
// `id1` is not updated as it does not offer plural forms.
{
msgid: 'id1',
msgctxt: 'Context for example message 1',
msgid_plural: '',
msgstr: [ '' ]
},
{
msgid: 'id2',
msgctxt: 'Context for example message 2',
msgid_plural: 'Example message 2 - plural form',
msgstr: [ '1', '2', '3', '4' ]
}
] );
} );

it( 'should remove existing plural forms if a source file contains less than a language defines', () => {
translations.items = [
{
msgid: 'id1',
msgctxt: 'Context for example message 1',
msgid_plural: '',
msgstr: [ '' ]
},
{
msgid: 'id2',
msgctxt: 'Context for example message 2',
msgid_plural: 'Example message 2 - plural form',
msgstr: [ '1', '2' ]
}
];

synchronizeTranslationsBasedOnContext( defaultOptions );

expect( translations.items ).toEqual( [
// `id1` is not updated as it does not offer plural forms.
{
msgid: 'id1',
msgctxt: 'Context for example message 1',
msgid_plural: '',
msgstr: [ '' ]
},
{
msgid: 'id2',
msgctxt: 'Context for example message 2',
msgid_plural: 'Example message 2 - plural form',
msgstr: [ '1', '2', '', '' ]
}
] );
} );

it( 'should save updated translation files on filesystem after cleaning the content', () => {
synchronizeTranslationsBasedOnContext( defaultOptions );

Expand Down

0 comments on commit 866892c

Please sign in to comment.