Skip to content

Commit a97795f

Browse files
feat: PO - add source locations (bartholomej#9)
Closes bartholomej#8
1 parent 9f2f898 commit a97795f

21 files changed

+178
-129
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ root = true
33

44
[*]
55
charset = utf-8
6-
indent_style = space
6+
indent_style = tab
77
indent_size = 4
88
insert_final_newline = true
99
trim_trailing_whitespace = true

src/compilers/json.compiler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CompilerInterface } from './compiler.interface.js';
2-
import { TranslationCollection } from '../utils/translation.collection.js';
2+
import {TranslationCollection, TranslationInterface} from '../utils/translation.collection.js';
33
import { stripBOM } from '../utils/utils.js';
44

55
import pkg from 'flat';
@@ -17,7 +17,7 @@ export class JsonCompiler implements CompilerInterface {
1717
}
1818

1919
public compile(collection: TranslationCollection): string {
20-
return JSON.stringify(collection.values, null, this.indentation);
20+
return JSON.stringify(collection.toKeyValueObject(), null, this.indentation);
2121
}
2222

2323
public parse(contents: string): TranslationCollection {

src/compilers/namespaced-json.compiler.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ export class NamespacedJsonCompiler implements CompilerInterface {
1717
}
1818

1919
public compile(collection: TranslationCollection): string {
20-
const values: {} = unflatten(collection.values, {
21-
object: true
22-
});
20+
const values: {} = unflatten(
21+
collection.toKeyValueObject(),
22+
{object: true}
23+
);
2324
return JSON.stringify(values, null, this.indentation);
2425
}
2526

src/compilers/po.compiler.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CompilerInterface } from './compiler.interface.js';
2-
import { TranslationCollection, TranslationType } from '../utils/translation.collection.js';
2+
import {TranslationCollection, TranslationInterface, TranslationType} from '../utils/translation.collection.js';
33

44
import pkg from 'gettext-parser';
55
const { po } = pkg;
@@ -23,13 +23,21 @@ export class PoCompiler implements CompilerInterface {
2323
'content-transfer-encoding': '8bit'
2424
},
2525
translations: {
26-
[this.domain]: Object.keys(collection.values).reduce((translations, key) => ({
27-
...translations,
28-
[key]: {
29-
msgid: key,
30-
msgstr: collection.get(key)
31-
}
32-
}), {} as any)
26+
[this.domain]: Object.keys(collection.values)
27+
.reduce(
28+
(translations, key) => {
29+
const entry: TranslationInterface = collection.get(key);
30+
return {
31+
...translations,
32+
[key]: {
33+
msgid: key,
34+
msgstr: entry.value,
35+
comments: {reference: entry.sourceFiles?.join('\n')}
36+
}
37+
};
38+
},
39+
{} as any
40+
)
3341
}
3442
};
3543

@@ -49,7 +57,7 @@ export class PoCompiler implements CompilerInterface {
4957
.filter((key) => key.length > 0)
5058
.reduce((result, key) => ({
5159
...result,
52-
[key]: parsedPo.translations[this.domain][key].msgstr.pop()
60+
[key]: {value: parsedPo.translations[this.domain][key].msgstr.pop(), sourceFiles: parsedPo.translations[this.domain][key].comments.reference.split('\n')}
5361
}), {} as TranslationType);
5462

5563
return new TranslationCollection(values);

src/parsers/directive.parser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,21 @@ export class DirectiveParser implements ParserInterface {
3737
elements.forEach((element) => {
3838
const attribute = this.getAttribute(element, TRANSLATE_ATTR_NAMES);
3939
if (attribute?.value) {
40-
collection = collection.add(attribute.value);
40+
collection = collection.add(attribute.value, '', filePath);
4141
return;
4242
}
4343

4444
const boundAttribute = this.getBoundAttribute(element, TRANSLATE_ATTR_NAMES);
4545
if (boundAttribute?.value) {
4646
this.getLiteralPrimitives(boundAttribute.value).forEach((literalPrimitive) => {
47-
collection = collection.add(literalPrimitive.value);
47+
collection = collection.add(literalPrimitive.value, '', filePath);
4848
});
4949
return;
5050
}
5151

5252
const textNodes = this.getTextNodes(element);
5353
textNodes.forEach((textNode) => {
54-
collection = collection.add(textNode.value.trim());
54+
collection = collection.add(textNode.value.trim(), '', filePath);
5555
});
5656
});
5757
return collection;

src/parsers/function.parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class FunctionParser implements ParserInterface {
2626
return;
2727
}
2828
const strings = getStringsFromExpression(firstArg);
29-
collection = collection.addKeys(strings);
29+
collection = collection.addKeys(strings, filePath);
3030
});
3131
return collection;
3232
}

src/parsers/marker.parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class MarkerParser implements ParserInterface {
2525
return;
2626
}
2727
const strings = getStringsFromExpression(firstArg);
28-
collection = collection.addKeys(strings);
28+
collection = collection.addKeys(strings, filePath);
2929
});
3030
return collection;
3131
}

src/parsers/pipe.parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class PipeParser implements ParserInterface {
3030
const pipes: BindingPipe[] = nodes.map((node) => this.findPipesInNode(node)).flat();
3131
pipes.forEach((pipe) => {
3232
this.parseTranslationKeysFromPipe(pipe).forEach((key: string) => {
33-
collection = collection.add(key);
33+
collection = collection.add(key, '', filePath);
3434
});
3535
});
3636
return collection;

src/parsers/service.parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class ServiceParser implements ParserInterface {
3939
return;
4040
}
4141
const strings = getStringsFromExpression(firstArg);
42-
collection = collection.addKeys(strings);
42+
collection = collection.addKeys(strings, filePath);
4343
});
4444
});
4545
return collection;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { TranslationCollection } from '../utils/translation.collection.js';
1+
import {TranslationCollection, TranslationInterface} from '../utils/translation.collection.js';
22
import { PostProcessorInterface } from './post-processor.interface.js';
33

44
export class KeyAsDefaultValuePostProcessor implements PostProcessorInterface {
55
public name: string = 'KeyAsDefaultValue';
66

77
public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
8-
return draft.map((key, val) => (val === '' ? key : val));
8+
return draft.map((key: string, val: TranslationInterface): TranslationInterface => val.value === '' ? {value: key, sourceFiles: (val?.sourceFiles || [])} : val);
99
}
1010
}

0 commit comments

Comments
 (0)