Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quiet-clowns-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"lingo.dev": patch
---

fix language header for PO files
39 changes: 39 additions & 0 deletions packages/cli/src/cli/loaders/po/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,45 @@ msgstr "[upd] Apple"
const result = await loader.push("en", updatedData);
expect(result).toEqual(expectedOutput);
});

it("should preserve existing Language header when pushing translations with metadata in first section", async () => {
const loader = createLoader();
const sourceInput = `msgid ""
msgstr ""
"Language: en\\n"
"Content-Type: text/plain; charset=utf-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"X-Generator: next-intl\\n"
#: hello.py:1
msgid "Hello world"
msgstr "Hello world"`;

const targetInput = `msgid ""
msgstr ""
"Language: es\\n"
"Content-Type: text/plain; charset=utf-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"X-Generator: next-intl\\n"
#: hello.py:1
msgid "Hello world"
msgstr ""`;

await loader.pull("en", sourceInput);
await loader.pull("es", targetInput);

const updatedData = {
"Hello world": {
singular: "Hola mundo",
plural: null,
},
};

const result = await loader.push("es", updatedData);

expect(result).toContain('"Language: es\\n"');
expect(result).not.toContain('"Language: en\\n"');
expect(result).toContain('msgstr "Hola mundo"');
});
});

function createLoader(params: PoLoaderParams = { multiline: false }) {
Expand Down
29 changes: 18 additions & 11 deletions packages/cli/src/cli/loaders/po/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,32 @@ export function createPoDataLoader(
const contextKey = _.keys(sectionPo.translations)[0];
const entries = sectionPo.translations[contextKey];
const msgid = Object.keys(entries).find((key) => entries[key].msgid);
if (!msgid) {
// If the section is empty, try to find it in the current sections
const currentSection = currentSections.find((cs) => {
const csPo = gettextParser.po.parse(cs);
const csContextKey = _.keys(csPo.translations)[0];
const csEntries = csPo.translations[csContextKey];
const csMsgid = Object.keys(csEntries).find(
(key) => csEntries[key].msgid,
);
return csMsgid === msgid;
});

// If the section is empty, try to find it in the current sections
const currentSection = currentSections.find((cs) => {
const csPo = gettextParser.po.parse(cs);
const csContextKey = _.keys(csPo.translations)[0];
const csEntries = csPo.translations[csContextKey];
const csMsgid = Object.keys(csEntries).find(
(key) => csEntries[key].msgid,
);
return csMsgid === msgid;
});

if (!msgid) {
if (currentSection) {
return currentSection;
}
return section;
}
if (data[msgid]) {
// Preserve headers from the target file
const headers = currentSection
? gettextParser.po.parse(currentSection).headers
: sectionPo.headers;

const updatedPo = _.merge({}, sectionPo, {
headers,
translations: {
[contextKey]: {
[msgid]: {
Expand Down