Skip to content

Commit a502caf

Browse files
committed
feat(cli): aded metadata transfers for .po files
1 parent 1d76799 commit a502caf

4 files changed

Lines changed: 47 additions & 7 deletions

File tree

.changeset/slimy-onions-collect.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"lingo.dev": patch
3+
---
4+
5+
metadata transfers for .po

packages/cli/src/cli/loaders/_types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
export interface ILoaderDefinition<I, O, C> {
22
init?(): Promise<C>;
33
pull(locale: string, input: I, initCtx?: C): Promise<O>;
4-
push(locale: string, data: O, originalInput: I | null, originalLocale: string): Promise<I>;
4+
push(
5+
locale: string,
6+
data: O,
7+
originalInput: I | null,
8+
originalLocale: string,
9+
pullInput: I | null,
10+
pullOutput: O | null,
11+
): Promise<I>;
512
}
613

714
export interface ILoader<I, O, C = void> extends ILoaderDefinition<I, O, C> {

packages/cli/src/cli/loaders/_utils.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export function createLoader<I, O, C>(lDefinition: ILoaderDefinition<I, O, C>):
3434
const state = {
3535
defaultLocale: undefined as string | undefined,
3636
originalInput: undefined as I | undefined | null,
37+
pullInput: undefined as I | undefined | null,
38+
pullOutput: undefined as O | undefined | null,
3739
initCtx: undefined as C | undefined,
3840
};
3941
return {
@@ -62,7 +64,11 @@ export function createLoader<I, O, C>(lDefinition: ILoaderDefinition<I, O, C>):
6264
state.originalInput = input || null;
6365
}
6466

65-
return lDefinition.pull(locale, input, state.initCtx);
67+
state.pullInput = input;
68+
const result = await lDefinition.pull(locale, input, state.initCtx);
69+
state.pullOutput = result;
70+
71+
return result;
6672
},
6773
async push(locale, data) {
6874
if (!state.defaultLocale) {
@@ -72,7 +78,14 @@ export function createLoader<I, O, C>(lDefinition: ILoaderDefinition<I, O, C>):
7278
throw new Error("Cannot push data without pulling first");
7379
}
7480

75-
const pushResult = await lDefinition.push(locale, data, state.originalInput, state.defaultLocale);
81+
const pushResult = await lDefinition.push(
82+
locale,
83+
data,
84+
state.originalInput,
85+
state.defaultLocale,
86+
state.pullInput!,
87+
state.pullOutput!,
88+
);
7689
return pushResult;
7790
},
7891
};

packages/cli/src/cli/loaders/po/index.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,31 @@ export function createPoDataLoader(params: PoLoaderParams): ILoader<string, PoTr
4040
return result;
4141
},
4242

43-
async push(locale, data, originalInput) {
43+
async push(locale, data, originalInput, originalLocale, pullInput) {
4444
// Parse each section to maintain structure
45-
const sections = originalInput?.split("\n\n").filter(Boolean) || [];
46-
const result = sections
45+
const currentSections = pullInput?.split("\n\n").filter(Boolean) || [];
46+
const originalSections = originalInput?.split("\n\n").filter(Boolean) || [];
47+
const result = originalSections
4748
.map((section) => {
4849
const sectionPo = gettextParser.po.parse(section);
4950
const contextKey = _.keys(sectionPo.translations)[0];
5051
const entries = sectionPo.translations[contextKey];
5152
const msgid = Object.keys(entries).find((key) => entries[key].msgid);
52-
if (!msgid) return section;
53+
if (!msgid) {
54+
// If the section is empty, try to find it in the current sections
55+
const currentSection = currentSections.find((cs) => {
56+
const csPo = gettextParser.po.parse(cs);
57+
const csContextKey = _.keys(csPo.translations)[0];
58+
const csEntries = csPo.translations[csContextKey];
59+
const csMsgid = Object.keys(csEntries).find((key) => csEntries[key].msgid);
60+
return csMsgid === msgid;
61+
});
62+
63+
if (currentSection) {
64+
return currentSection;
65+
}
66+
return section;
67+
}
5368
if (data[msgid]) {
5469
const updatedPo = _.merge({}, sectionPo, {
5570
translations: {

0 commit comments

Comments
 (0)