Skip to content

Commit

Permalink
feat: Add po-source-locations CLI option
Browse files Browse the repository at this point in the history
The `--po-source-locations` option controls whether to include file location
comments in `.po` files. Defaults to `true`.

`--po-source-locations=false` or `--no-po-source-locations`: Excludes location comments.

Closes #63
  • Loading branch information
pmpak committed Dec 9, 2024
1 parent 325f3c1 commit 3a06463
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ npm-debug.log*
# Compiled files
src/**/*.js
tests/**/*.js
tests/cli/tmp
dist

# Extracted strings
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,11 @@ Output
--format, -f Format [string] [choices: "json", "namespaced-json", "pot"] [default: "json"]
--format-indentation, --fi Format indentation (JSON/Namedspaced JSON) [string] [default: "\t"]
--sort, -s Sort strings in alphabetical order [boolean]
--sort-sensitivity, -ss Sensitivity when sorting strings (only when sort is enabled) [string]
--sort-sensitivity, -ss Sensitivity when sorting strings (only when sort is enabled) [string]
--clean, -c Remove obsolete strings after merge [boolean]
--replace, -r Replace the contents of output file if it exists (Merges by default) [boolean]
--strip-prefix, -sp Strip prefix from key [string]
--po-source-locations Include file location comments in .po files [boolean] [default: true]

Extracted key value (defaults to empty string)
--key-as-default-value, -k Use key as default value [boolean]
Expand Down
10 changes: 8 additions & 2 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ const cli = await y
choices: ['base', 'accent', 'case', 'variant'],
default: undefined
})
.option('po-source-locations', {
describe: 'Include file location comments in .po files',
type: 'boolean',
default: true,
})
.option('clean', {
alias: 'c',
describe: 'Remove obsolete strings after merge',
Expand Down Expand Up @@ -132,7 +137,7 @@ const cli = await y
describe: 'Strip a prefix from the extracted key',
type: 'string'
})
.group(['format', 'format-indentation', 'sort', 'sort-sensitivity', 'clean', 'replace', 'strip-prefix'], 'Output')
.group(['format', 'format-indentation', 'sort', 'sort-sensitivity', 'clean', 'replace', 'strip-prefix', 'no-po-source-locations'], 'Output')
.group(['key-as-default-value', 'key-as-initial-default-value', 'null-as-default-value', 'string-as-default-value'], 'Extracted key value (defaults to empty string)')
.conflicts('key-as-default-value', 'null-as-default-value')
.conflicts('key-as-initial-default-value', 'null-as-default-value')
Expand Down Expand Up @@ -190,7 +195,8 @@ extractTask.setPostProcessors(postProcessors);

// Compiler
const compiler: CompilerInterface = CompilerFactory.create(cli.format, {
indentation: cli.formatIndentation
indentation: cli.formatIndentation,
poSourceLocation: cli.poSourceLocations,
});
extractTask.setCompiler(compiler);

Expand Down
4 changes: 2 additions & 2 deletions src/compilers/compiler.factory.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CompilerInterface } from './compiler.interface.js';
import { CompilerInterface, CompilerOptions } from './compiler.interface.js';
import { JsonCompiler } from './json.compiler.js';
import { NamespacedJsonCompiler } from './namespaced-json.compiler.js';
import { PoCompiler } from './po.compiler.js';

export class CompilerFactory {
public static create(format: string, options?: {}): CompilerInterface {
public static create(format: string, options?: CompilerOptions): CompilerInterface {
switch (format) {
case 'pot':
return new PoCompiler(options);
Expand Down
5 changes: 5 additions & 0 deletions src/compilers/compiler.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { TranslationCollection } from '../utils/translation.collection.js';

export interface CompilerOptions {
indentation?: string;
poSourceLocation?: boolean;
}

export interface CompilerInterface {
extension: string;

Expand Down
4 changes: 2 additions & 2 deletions src/compilers/json.compiler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CompilerInterface } from './compiler.interface.js';
import { CompilerInterface, CompilerOptions } from './compiler.interface.js';
import {TranslationCollection, TranslationInterface, TranslationType} from '../utils/translation.collection.js';
import { stripBOM } from '../utils/utils.js';

Expand All @@ -9,7 +9,7 @@ export class JsonCompiler implements CompilerInterface {

public extension: string = 'json';

public constructor(options?: any) {
constructor(options?: CompilerOptions) {
if (options && typeof options.indentation !== 'undefined') {
this.indentation = options.indentation;
}
Expand Down
4 changes: 2 additions & 2 deletions src/compilers/namespaced-json.compiler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CompilerInterface } from './compiler.interface.js';
import { CompilerInterface, CompilerOptions } from './compiler.interface.js';
import {TranslationCollection, TranslationInterface, TranslationType} from '../utils/translation.collection.js';
import { stripBOM } from '../utils/utils.js';

Expand All @@ -9,7 +9,7 @@ export class NamespacedJsonCompiler implements CompilerInterface {

public extension = 'json';

public constructor(options?: any) {
constructor(options?: CompilerOptions) {
if (options && typeof options.indentation !== 'undefined') {
this.indentation = options.indentation;
}
Expand Down
14 changes: 10 additions & 4 deletions src/compilers/po.compiler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CompilerInterface } from './compiler.interface.js';
import { CompilerInterface, CompilerOptions } from './compiler.interface.js';
import {TranslationCollection, TranslationInterface, TranslationType} from '../utils/translation.collection.js';

import pkg from 'gettext-parser';
Expand All @@ -12,7 +12,12 @@ export class PoCompiler implements CompilerInterface {
*/
public domain: string = '';

public constructor(options?: any) {}
/** Whether to include file location comments. **/
private readonly includeSources: boolean = true;

constructor(options?: CompilerOptions) {
this.includeSources = options?.poSourceLocation ?? true;
}

public compile(collection: TranslationCollection): string {
const data = {
Expand All @@ -27,16 +32,17 @@ export class PoCompiler implements CompilerInterface {
.reduce(
(translations, key) => {
const entry: TranslationInterface = collection.get(key);
const comments = this.includeSources ? {reference: entry.sourceFiles?.join('\n')} : undefined;
return {
...translations,
[key]: {
msgid: key,
msgstr: entry.value,
comments: {reference: entry.sourceFiles?.join('\n')}
comments: comments
}
};
},
{} as any
{}
)
}
};
Expand Down
53 changes: 53 additions & 0 deletions tests/cli/__snapshots__/cli.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,56 @@ msgstr ""
msgid "marker.dashboard.description"
msgstr """
`;

exports[`CLI Integration Tests > extracts translation keys to a .po file without file location comments 1`] = `
"msgid ""
msgstr ""
"mime-version: 1.0\\n"
"Content-Type: text/plain; charset=utf-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
msgid "translate-service.comp.welcome"
msgstr ""
msgid "translate-service.comp.description"
msgstr ""
msgid "translate-service.comp.details"
msgstr ""
msgid "translate-service.comp.show-more-label"
msgstr ""
msgid "translate-service.comp.show-less-label"
msgstr ""
msgid "pipe.comp.welcome"
msgstr ""
msgid "pipe.comp.description"
msgstr ""
msgid "directive.comp.welcome"
msgstr ""
msgid "directive.comp.description"
msgstr ""
msgid "private-translate-service.comp.welcome"
msgstr ""
msgid "private-translate-service.comp.description"
msgstr ""
msgid "ngx-translate.marker.dashboard.title"
msgstr ""
msgid "ngx-translate.marker.dashboard.description"
msgstr ""
msgid "marker.dashboard.title"
msgstr ""
msgid "marker.dashboard.description"
msgstr """
`;
9 changes: 9 additions & 0 deletions tests/cli/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,13 @@ describe.concurrent('CLI Integration Tests', () => {

expect(extracted).toMatchSnapshot();
});

test('extracts translation keys to a .po file without file location comments', async ({ expect }) => {
const OUTPUT_FILE = createUniqueFileName('strings.po');
await execAsync(`node ${CLI_PATH} --input ${FIXTURES_PATH} --output ${OUTPUT_FILE} --format=pot --no-po-source-locations`);

const extracted = await readFile(OUTPUT_FILE, { encoding: 'utf8' });

expect(extracted).toMatchSnapshot();
});
});

0 comments on commit 3a06463

Please sign in to comment.