Skip to content

Commit b017c46

Browse files
authored
Remove unused comment when message is used again (#34)
Previously the "unused" comment was kept even if a translation was "brought" back into the game again. This change fixes that by removing the comment if the translation is actually used again.
1 parent 4c217c8 commit b017c46

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

.changeset/giant-needles-compete.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@fransvilhelm/wp-bundler': patch
3+
---
4+
5+
Remove unused comment when translation is used again

src/utils/po.test.ts

+38
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,44 @@ it('handles translations that exists as single but get appended as plural', asyn
279279
`);
280280
});
281281

282+
it('removes removed comment if translation is brought back', async () => {
283+
let po = new Po(
284+
`
285+
msgid ""
286+
msgstr ""
287+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
288+
"Content-Type: text/plain; charset=UTF-8\n"
289+
"Content-Transfer-Encoding: 8bit\n"
290+
"MIME-Version: 1.0\n"
291+
"Language: en_US\n"
292+
293+
# THIS TRANSLATION IS NO LONGER REFERENCED INSIDE YOUR PROJECT
294+
msgid "test"
295+
msgstr "test"
296+
`,
297+
'test.po',
298+
);
299+
300+
let pot = await Po.load('test.pot');
301+
302+
pot.set({ text: 'test', location: createLocation() });
303+
po.updateFromTemplate(pot);
304+
305+
expect(po.toString()).toMatchInlineSnapshot(`
306+
"msgid \\"\\"
307+
msgstr \\"\\"
308+
\\"Plural-Forms: nplurals=2; plural=(n != 1);\\\\n\\"
309+
\\"Content-Type: text/plain; charset=utf-8\\\\n\\"
310+
\\"Content-Transfer-Encoding: 8bit\\\\n\\"
311+
\\"MIME-Version: 1.0\\\\n\\"
312+
\\"Language: en_US\\\\n\\"
313+
314+
#: test.ts:1
315+
msgid \\"test\\"
316+
msgstr \\"test\\""
317+
`);
318+
});
319+
282320
function createLocation(replace: Partial<Location> = {}): Location {
283321
return {
284322
file: 'test.ts',

src/utils/po.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,20 @@ export class Po {
114114

115115
if (keysToMerge.includes(key) && typeof value === 'string' && typeof srcValue === 'string') {
116116
let lines = [...value.trim().split('\n'), ...srcValue.trim().split('\n')];
117-
return lines
117+
118+
let out = lines
119+
// Keep only lines with content, and uniq
118120
.filter((line, i, self) => !!line && self.indexOf(line) === i)
121+
// Remove unused comment if translation is back in the game
122+
.filter((line) => {
123+
if (srcValue.includes(Po.UNUSED_COMMENT)) return true;
124+
return !line.includes(Po.UNUSED_COMMENT);
125+
})
119126
.sort()
120127
.join('\n')
121128
.replace(/translators:/gi, 'translators:');
129+
130+
return out;
122131
}
123132

124133
if (key === 'msgstr' && Array.isArray(value) && Array.isArray(srcValue)) {

0 commit comments

Comments
 (0)