Add NamespacedJsonCompiler
This commit is contained in:
parent
183592acc3
commit
7822916e28
@ -26,7 +26,8 @@ Options:
|
|||||||
-d, --dir [DIR] Directory path you would like to extract strings from (Default is current directory)
|
-d, --dir [DIR] Directory path you would like to extract strings from (Default is current directory)
|
||||||
-o, --output [DIR] Directory path you would like to save extracted
|
-o, --output [DIR] Directory path you would like to save extracted
|
||||||
strings (Default is current directory/template.json)
|
strings (Default is current directory/template.json)
|
||||||
-f, --format [VALUE] Output format. VALUE must be either [json|pot] (Default is json)
|
-f, --format [VALUE] Output format. VALUE must be either
|
||||||
|
[json|namespaced-json|pot] (Default is json)
|
||||||
-r, --replace BOOLEAN Replace the contents of output file if it exists
|
-r, --replace BOOLEAN Replace the contents of output file if it exists
|
||||||
(Merges by default)
|
(Merges by default)
|
||||||
-s, --sort BOOLEAN Sort translations in the output file in alphabetical
|
-s, --sort BOOLEAN Sort translations in the output file in alphabetical
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@biesbjerg/ng2-translate-extract",
|
"name": "@biesbjerg/ng2-translate-extract",
|
||||||
"version": "0.4.0",
|
"version": "0.5.0",
|
||||||
"description": "Extract strings from projects using ng2-translate",
|
"description": "Extract strings from projects using ng2-translate",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist/index.d.ts",
|
||||||
@ -60,6 +60,7 @@
|
|||||||
"fs": "0.0.1-security",
|
"fs": "0.0.1-security",
|
||||||
"gettext-parser": "1.2.1",
|
"gettext-parser": "1.2.1",
|
||||||
"glob": "7.1.1",
|
"glob": "7.1.1",
|
||||||
"path": "0.12.7"
|
"path": "0.12.7",
|
||||||
|
"flat": "2.0.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,9 @@ import { ParserInterface } from '../parsers/parser.interface';
|
|||||||
import { PipeParser } from '../parsers/pipe.parser';
|
import { PipeParser } from '../parsers/pipe.parser';
|
||||||
import { DirectiveParser } from '../parsers/directive.parser';
|
import { DirectiveParser } from '../parsers/directive.parser';
|
||||||
import { ServiceParser } from '../parsers/service.parser';
|
import { ServiceParser } from '../parsers/service.parser';
|
||||||
|
import { CompilerInterface } from '../compilers/compiler.interface';
|
||||||
import { JsonCompiler } from '../compilers/json.compiler';
|
import { JsonCompiler } from '../compilers/json.compiler';
|
||||||
|
import { NamespacedJsonCompiler } from '../compilers/namespaced-json.compiler';
|
||||||
import { PoCompiler } from '../compilers/po.compiler';
|
import { PoCompiler } from '../compilers/po.compiler';
|
||||||
|
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
@ -14,17 +16,45 @@ import * as cli from 'cli';
|
|||||||
const options = cli.parse({
|
const options = cli.parse({
|
||||||
dir: ['d', 'Path you would like to extract strings from', 'dir', process.env.PWD],
|
dir: ['d', 'Path you would like to extract strings from', 'dir', process.env.PWD],
|
||||||
output: ['o', 'Path you would like to save extracted strings to', 'dir', process.env.PWD],
|
output: ['o', 'Path you would like to save extracted strings to', 'dir', process.env.PWD],
|
||||||
format: ['f', 'Output format', ['json', 'pot'], 'json'],
|
format: ['f', 'Output format', ['json', 'namespaced-json', 'pot'], 'json'],
|
||||||
replace: ['r', 'Replace the contents of output file if it exists (Merges by default)', 'boolean', false],
|
replace: ['r', 'Replace the contents of output file if it exists (Merges by default)', 'boolean', false],
|
||||||
sort: ['s', 'Sort translations in the output file in alphabetical order', 'boolean', false],
|
sort: ['s', 'Sort translations in the output file in alphabetical order', 'boolean', false],
|
||||||
clean: ['c', 'Remove obsolete strings when merging', 'boolean', false]
|
clean: ['c', 'Remove obsolete strings when merging', 'boolean', false]
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const patterns: string[] = [
|
||||||
|
'/**/*.html',
|
||||||
|
'/**/*.ts',
|
||||||
|
'/**/*.js'
|
||||||
|
];
|
||||||
|
const parsers: ParserInterface[] = [
|
||||||
|
new PipeParser(),
|
||||||
|
new DirectiveParser(),
|
||||||
|
new ServiceParser()
|
||||||
|
];
|
||||||
|
|
||||||
|
let compiler: CompilerInterface;
|
||||||
|
let ext: string;
|
||||||
|
switch (options.format) {
|
||||||
|
case 'pot':
|
||||||
|
compiler = new PoCompiler();
|
||||||
|
ext = 'pot';
|
||||||
|
break;
|
||||||
|
case 'json':
|
||||||
|
compiler = new JsonCompiler();
|
||||||
|
ext = 'json';
|
||||||
|
break;
|
||||||
|
case 'namespaced-json':
|
||||||
|
compiler = new NamespacedJsonCompiler();
|
||||||
|
ext = 'json';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
const normalizedDir: string = path.resolve(options.dir);
|
const normalizedDir: string = path.resolve(options.dir);
|
||||||
const normalizedOutput: string = path.resolve(options.output);
|
const normalizedOutput: string = path.resolve(options.output);
|
||||||
|
|
||||||
let outputDir: string = normalizedOutput;
|
let outputDir: string = normalizedOutput;
|
||||||
let outputFilename: string = `template.${options.format}`;
|
let outputFilename: string = `template.${ext}`;
|
||||||
if (!fs.existsSync(normalizedOutput) || !fs.statSync(normalizedOutput).isDirectory()) {
|
if (!fs.existsSync(normalizedOutput) || !fs.statSync(normalizedOutput).isDirectory()) {
|
||||||
outputDir = path.dirname(normalizedOutput);
|
outputDir = path.dirname(normalizedOutput);
|
||||||
outputFilename = path.basename(normalizedOutput);
|
outputFilename = path.basename(normalizedOutput);
|
||||||
@ -37,21 +67,6 @@ const outputPath: string = path.join(outputDir, outputFilename);
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let compiler = new JsonCompiler();
|
|
||||||
if (options.format === 'pot') {
|
|
||||||
compiler = new PoCompiler();
|
|
||||||
}
|
|
||||||
const parsers: ParserInterface[] = [
|
|
||||||
new PipeParser(),
|
|
||||||
new DirectiveParser(),
|
|
||||||
new ServiceParser()
|
|
||||||
];
|
|
||||||
const patterns: string[] = [
|
|
||||||
'/**/*.html',
|
|
||||||
'/**/*.ts',
|
|
||||||
'/**/*.js'
|
|
||||||
];
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const extractor: Extractor = new Extractor(parsers, patterns);
|
const extractor: Extractor = new Extractor(parsers, patterns);
|
||||||
cli.info(`Extracting strings from '${normalizedDir}'`);
|
cli.info(`Extracting strings from '${normalizedDir}'`);
|
||||||
|
18
src/compilers/namespaced-json.compiler.ts
Normal file
18
src/compilers/namespaced-json.compiler.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { CompilerInterface } from './compiler.interface';
|
||||||
|
import { TranslationCollection } from '../utils/translation.collection';
|
||||||
|
|
||||||
|
import * as flat from 'flat';
|
||||||
|
|
||||||
|
export class NamespacedJsonCompiler implements CompilerInterface {
|
||||||
|
|
||||||
|
public compile(collection: TranslationCollection): string {
|
||||||
|
const values = flat.unflatten(collection.values);
|
||||||
|
return JSON.stringify(values, null, '\t');
|
||||||
|
}
|
||||||
|
|
||||||
|
public parse(contents: string): TranslationCollection {
|
||||||
|
const values = flat.flatten(JSON.parse(contents));
|
||||||
|
return new TranslationCollection(values);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
1
src/declarations.d.ts
vendored
1
src/declarations.d.ts
vendored
@ -1,2 +1,3 @@
|
|||||||
declare module 'cli';
|
declare module 'cli';
|
||||||
|
declare module 'flat';
|
||||||
declare module 'gettext-parser';
|
declare module 'gettext-parser';
|
||||||
|
38
tests/compilers/namespaced-compiler.spec.ts
Normal file
38
tests/compilers/namespaced-compiler.spec.ts
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import { expect } from 'chai';
|
||||||
|
|
||||||
|
import { TranslationCollection } from '../../src/utils/translation.collection';
|
||||||
|
import { NamespacedJsonCompiler } from '../../src/compilers/namespaced-json.compiler';
|
||||||
|
|
||||||
|
describe('NamespacedJsonCompiler', () => {
|
||||||
|
|
||||||
|
let compiler: NamespacedJsonCompiler;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
compiler = new NamespacedJsonCompiler();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should flatten keys on parse', () => {
|
||||||
|
const contents = `
|
||||||
|
{
|
||||||
|
"NAMESPACE": {
|
||||||
|
"KEY": {
|
||||||
|
"FIRST_KEY": "",
|
||||||
|
"SECOND_KEY": "VALUE"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
const collection: TranslationCollection = compiler.parse(contents);
|
||||||
|
expect(collection.values).to.deep.equal({'NAMESPACE.KEY.FIRST_KEY': '', 'NAMESPACE.KEY.SECOND_KEY': 'VALUE' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should unflatten keys on compile', () => {
|
||||||
|
const collection = new TranslationCollection({
|
||||||
|
'NAMESPACE.KEY.FIRST_KEY': '',
|
||||||
|
'NAMESPACE.KEY.SECOND_KEY': 'VALUE'
|
||||||
|
});
|
||||||
|
const result: string = compiler.compile(collection);
|
||||||
|
expect(result).to.equal('{\n\t"NAMESPACE": {\n\t\t"KEY": {\n\t\t\t"FIRST_KEY": "",\n\t\t\t"SECOND_KEY": "VALUE"\n\t\t}\n\t}\n}');
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user