Compare commits
84 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
5d5b07ba2c | ||
|
7eefd6c8d3 | ||
|
50fd3ae9e2 | ||
|
a5b8f6e6c6 | ||
|
3cbc20e0a0 | ||
|
7ce01b97e4 | ||
|
393e1ed03f | ||
|
8b014abf49 | ||
|
71cc6e6883 | ||
|
ceb4be7e3d | ||
|
e41fc88d97 | ||
|
ac551b1824 | ||
|
73877a5a35 | ||
|
6e161c83f8 | ||
|
7d1bcd2a80 | ||
|
bc2bfac7d7 | ||
|
c7563d4998 | ||
|
f9b3c63c4c | ||
|
9e5dad362c | ||
|
e133e0ce30 | ||
|
7ee0b7da71 | ||
|
c38ca59d43 | ||
|
98b84447c7 | ||
|
2507d0cdd2 | ||
|
69047857b2 | ||
|
04b6684024 | ||
|
9a8abb3248 | ||
|
c8ba1312b5 | ||
|
e0178b5a97 | ||
|
3e43fde1cc | ||
|
8493015e15 | ||
|
deb38eb7c3 | ||
|
36928e253d | ||
|
230b13e245 | ||
|
4fd7efa2dc | ||
|
02fc705bc0 | ||
|
cb53f6f3b1 | ||
|
842b0a7d97 | ||
|
f32128b2ec | ||
|
53eb4d1202 | ||
|
102286a209 | ||
|
ab29c9ab67 | ||
|
590f58fff3 | ||
|
d07d81681e | ||
|
141eaca7b1 | ||
|
7d5d38e6a1 | ||
|
64ebb5e6e8 | ||
|
40051f4144 | ||
|
14eb09f947 | ||
|
8d1e2c5a2f | ||
|
4892ea5146 | ||
|
ee28fe2a64 | ||
|
7c06b66974 | ||
|
b2ae17697d | ||
|
5259da8fe3 | ||
|
2d73f056ff | ||
|
fde5245731 | ||
|
4ee7258a31 | ||
|
a6c7af0630 | ||
|
0949bf765b | ||
|
d416c6b9fd | ||
|
4e351405fb | ||
|
39a335638b | ||
|
3b9561916b | ||
|
5cef383f3b | ||
|
677d2a35ca | ||
|
262a89206d | ||
|
bcb4a9c069 | ||
|
5ad1fe6a18 | ||
|
bc5ce7e80d | ||
|
030ab145d6 | ||
|
daaebede6f | ||
|
42a6568d47 | ||
|
9a2108e9a9 | ||
|
ff1c91010e | ||
|
7654397bb3 | ||
|
7b10b246e6 | ||
|
2f68bbc660 | ||
|
371a14d73e | ||
|
4537c1224a | ||
|
f61cdc4064 | ||
|
72739f2e9a | ||
|
3facc0c287 | ||
|
c5d68cfcaa |
2
.github/FUNDING.yml
vendored
Normal file
2
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
github: #biesbjerg
|
||||
custom: https://donate.biesbjerg.com
|
4
.gitignore
vendored
4
.gitignore
vendored
@@ -10,8 +10,8 @@ npm-debug.log*
|
||||
dist
|
||||
|
||||
# Extracted strings
|
||||
template.json
|
||||
template.pot
|
||||
strings.json
|
||||
strings.pot
|
||||
|
||||
# Dependency directory
|
||||
node_modules
|
||||
|
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Kim Biesbjerg
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
115
README.md
115
README.md
@@ -1,40 +1,109 @@
|
||||
If you like this project please show your support with a GitHub star. Much appreciated!
|
||||
If this tool saves you time, please consider making a donation towards the continued maintainence and development: https://donate.biesbjerg.com
|
||||
|
||||
# ngx-translate-extract
|
||||
[](https://donate.biesbjerg.com)
|
||||
|
||||
# Usage
|
||||
|
||||
## ngx-translate-extract
|
||||
Extract translatable (ngx-translate) strings and save as a JSON or Gettext pot file.
|
||||
Merges with existing strings if the output file already exists.
|
||||
|
||||
## Usage
|
||||
### Usage
|
||||
Install the package in your project:
|
||||
|
||||
`npm install @biesbjerg/ngx-translate-extract --save-dev`
|
||||
|
||||
Add an `extract` script to your project's `package.json`:
|
||||
```
|
||||
Add a script to your project's `package.json`:
|
||||
```json
|
||||
...
|
||||
"scripts": {
|
||||
"extract": "ngx-translate-extract --dir ./src --output ./ --format=json --clean"
|
||||
"extract-i18n": "ngx-translate-extract --input ./src --output ./src/assets/i18n/ --clean --sort --format namespaced-json"
|
||||
}
|
||||
...
|
||||
```
|
||||
You can now run `npm run extract` to extract strings from your project's `src` dir. The extracted strings are saved in `JSON`-format in your project's root.
|
||||
You can now run `npm run extract-i18n` and it will extract strings from your project.
|
||||
|
||||
Modify the scripts arguments as required.
|
||||
### Examples
|
||||
|
||||
**Extract from dir and save to file**
|
||||
|
||||
`ngx-translate-extract --input ./src --output ./src/assets/i18n/template.json`
|
||||
|
||||
**Extract from multiple dirs**
|
||||
|
||||
`ngx-translate-extract --input ./src-a ./src-b --output ./src/assets/i18n/template.json`
|
||||
|
||||
|
||||
**Extract and save to multiple files using path expansion**
|
||||
|
||||
_Note: This method does not work on Windows!_
|
||||
|
||||
`ngx-translate-extract --input ./src --output ./src/i18n/{da,en}.json`
|
||||
|
||||
On Windows you must specify each output destination individually:
|
||||
|
||||
`ngx-translate-extract --input ./src --output ./src/i18n/da.json ./src/i18n/en.json`
|
||||
|
||||
### JSON indentation
|
||||
Tabs are used by default for indentation when saving extracted strings in json formats:
|
||||
|
||||
If you want to use spaces instead, you can do the following:
|
||||
|
||||
`ngx-translate-extract --input ./src --output ./src/i18n/en.json --format-indentation ' '`
|
||||
|
||||
## Mark strings for extraction using a marker function
|
||||
If, for some reason, you want to extract strings not passed directly to `TranslateService`'s `get()` or `instant()` methods, you can wrap them in a custom marker function to let `ngx-translate-extract` know you want to extract them.
|
||||
|
||||
Install marker function:
|
||||
`npm install @biesbjerg/ngx-translate-extract-marker`
|
||||
|
||||
```ts
|
||||
import { marker } from '@biesbjerg/ngx-translate-extract-marker';
|
||||
|
||||
marker('Extract me');
|
||||
```
|
||||
|
||||
Add the `marker` argument when running the extract script:
|
||||
|
||||
`ngx-translate-extract ... -m extract`
|
||||
|
||||
You can alias the marker function if needed:
|
||||
|
||||
```ts
|
||||
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
|
||||
|
||||
_('Extract me');
|
||||
```
|
||||
|
||||
`ngx-translate-extract ... -m _`
|
||||
|
||||
## Commandline arguments
|
||||
```
|
||||
```shell
|
||||
Usage:
|
||||
ngx-translate-extract [OPTIONS] [ARGS]
|
||||
ngx-translate-extract [options]
|
||||
|
||||
Options:
|
||||
-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
|
||||
strings (Default is current directory/template.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
|
||||
(Merges by default)
|
||||
-s, --sort BOOLEAN Sort translations in the output file in alphabetical
|
||||
order
|
||||
-c, --clean BOOLEAN Remove obsolete strings when merging
|
||||
-e, --experimental BOOLEAN Use experimental AST Service Parser
|
||||
-h, --help Display help and usage details
|
||||
```
|
||||
--version, -v Show version number [boolean]
|
||||
--help, -h Show help [boolean]
|
||||
--input, -i Paths you would like to extract strings from. You
|
||||
can use path expansion, glob patterns and multiple
|
||||
paths
|
||||
[array] [default: current working path]
|
||||
--patterns, -p Extract strings from the following file patterns
|
||||
[array] [default: ["/**/*.html","/**/*.ts"]]
|
||||
--output, -o Paths where you would like to save extracted
|
||||
strings. You can use path expansion, glob patterns
|
||||
and multiple paths [array] [required]
|
||||
--marker, -m Extract strings passed to a marker function
|
||||
[string] [default: false]
|
||||
--format, -f Output format
|
||||
[string] [choices: "json", "namespaced-json", "pot"] [default: "json"]
|
||||
--format-indentation, --fi Output format indentation [string] [default: " "]
|
||||
--replace, -r Replace the contents of output file if it exists
|
||||
(Merges by default) [boolean] [default: false]
|
||||
--sort, -s Sort strings in alphabetical order when saving
|
||||
[boolean] [default: false]
|
||||
--clean, -c Remove obsolete strings when merging
|
||||
[boolean] [default: false]
|
||||
--key-as-default-value, -k Use key as default value for translations
|
||||
[boolean] [default: false]
|
3
bin/cli.js
Executable file
3
bin/cli.js
Executable file
@@ -0,0 +1,3 @@
|
||||
#! /usr/bin/env node
|
||||
|
||||
require('../dist/cli/cli');
|
@@ -1,3 +0,0 @@
|
||||
#! /usr/bin/env node
|
||||
|
||||
require('../dist/cli/extract');
|
BIN
images/donate-badge.png
Normal file
BIN
images/donate-badge.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
1635
package-lock.json
generated
Normal file
1635
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
46
package.json
46
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@biesbjerg/ngx-translate-extract",
|
||||
"version": "1.0.0",
|
||||
"version": "3.0.2",
|
||||
"description": "Extract strings from projects using ngx-translate",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
@@ -9,7 +9,7 @@
|
||||
"dist/"
|
||||
],
|
||||
"bin": {
|
||||
"ngx-translate-extract": "bin/extract.js"
|
||||
"ngx-translate-extract": "bin/cli.js"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "npm run clean && tsc",
|
||||
@@ -24,10 +24,7 @@
|
||||
},
|
||||
"keywords": [
|
||||
"angular",
|
||||
"angular2",
|
||||
"ionic",
|
||||
"ionic2",
|
||||
"ng2-translate",
|
||||
"ngx-translate",
|
||||
"extract",
|
||||
"extractor",
|
||||
@@ -47,24 +44,29 @@
|
||||
},
|
||||
"config": {},
|
||||
"devDependencies": {
|
||||
"@types/chai": "3.4.35",
|
||||
"@types/cheerio": "0.17.31",
|
||||
"@types/glob": "5.0.30",
|
||||
"@types/mocha": "2.2.39",
|
||||
"chai": "3.5.0",
|
||||
"mocha": "3.2.0",
|
||||
"ts-node": "2.1.0",
|
||||
"tslint": "4.5.1",
|
||||
"tslint-eslint-rules": "3.4.0",
|
||||
"typescript": "2.2.1"
|
||||
"@types/chai": "^4.1.7",
|
||||
"@types/flat": "^0.0.28",
|
||||
"@types/glob": "^7.1.1",
|
||||
"@types/mkdirp": "^0.5.2",
|
||||
"@types/mocha": "^5.2.7",
|
||||
"@types/yargs": "^13.0.1",
|
||||
"chai": "^4.2.0",
|
||||
"mocha": "^6.2.0",
|
||||
"ts-node": "^8.3.0",
|
||||
"tslint": "^5.18.0",
|
||||
"tslint-eslint-rules": "^5.4.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"cheerio": "0.22.0",
|
||||
"cli": "1.0.1",
|
||||
"fs": "0.0.1-security",
|
||||
"gettext-parser": "1.2.2",
|
||||
"glob": "7.1.1",
|
||||
"path": "0.12.7",
|
||||
"flat": "2.0.1"
|
||||
"@angular/compiler": "^8.1.3",
|
||||
"boxen": "^4.1.0",
|
||||
"colorette": "^1.1.0",
|
||||
"flat": "git://github.com/lenchvolodymyr/flat.git#ffe77ef",
|
||||
"gettext-parser": "^4.0.1",
|
||||
"glob": "^7.1.4",
|
||||
"mkdirp": "^0.5.1",
|
||||
"path": "^0.12.7",
|
||||
"terminal-link": "^1.3.0",
|
||||
"typescript": "^3.5.3",
|
||||
"yargs": "^13.3.0"
|
||||
}
|
||||
}
|
||||
|
138
src/cli/cli.ts
Executable file
138
src/cli/cli.ts
Executable file
@@ -0,0 +1,138 @@
|
||||
import * as fs from 'fs';
|
||||
import * as yargs from 'yargs';
|
||||
|
||||
import { ExtractTask } from './tasks/extract.task';
|
||||
import { ParserInterface } from '../parsers/parser.interface';
|
||||
import { PipeParser } from '../parsers/pipe.parser';
|
||||
import { DirectiveParser } from '../parsers/directive.parser';
|
||||
import { ServiceParser } from '../parsers/service.parser';
|
||||
import { FunctionParser } from '../parsers/function.parser';
|
||||
import { PostProcessorInterface } from '../post-processors/post-processor.interface';
|
||||
import { SortByKeyPostProcessor } from '../post-processors/sort-by-key.post-processor';
|
||||
import { KeyAsDefaultValuePostProcessor } from '../post-processors/key-as-default-value.post-processor';
|
||||
import { PurgeObsoleteKeysPostProcessor } from '../post-processors/purge-obsolete-keys.post-processor';
|
||||
import { CompilerInterface } from '../compilers/compiler.interface';
|
||||
import { CompilerFactory } from '../compilers/compiler.factory';
|
||||
import { donateMessage } from '../utils/donate';
|
||||
|
||||
export const cli = yargs
|
||||
.usage('Extract strings from files for translation.\nUsage: $0 [options]')
|
||||
.version(require(__dirname + '/../../package.json').version)
|
||||
.alias('version', 'v')
|
||||
.help('help')
|
||||
.alias('help', 'h')
|
||||
.option('input', {
|
||||
alias: 'i',
|
||||
describe: 'Paths you would like to extract strings from. You can use path expansion, glob patterns and multiple paths',
|
||||
default: process.env.PWD,
|
||||
type: 'array',
|
||||
normalize: true
|
||||
})
|
||||
.check(options => {
|
||||
options.input.forEach((dir: string) => {
|
||||
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) {
|
||||
throw new Error(`The path you supplied was not found: '${dir}'`);
|
||||
}
|
||||
|
||||
});
|
||||
return true;
|
||||
})
|
||||
.option('patterns', {
|
||||
alias: 'p',
|
||||
describe: 'Extract strings from the following file patterns',
|
||||
type: 'array',
|
||||
default: ['/**/*.html', '/**/*.ts']
|
||||
})
|
||||
.option('output', {
|
||||
alias: 'o',
|
||||
describe: 'Paths where you would like to save extracted strings. You can use path expansion, glob patterns and multiple paths',
|
||||
type: 'array',
|
||||
normalize: true,
|
||||
required: true
|
||||
})
|
||||
.option('marker', {
|
||||
alias: 'm',
|
||||
describe: 'Extract strings passed to a marker function',
|
||||
default: false,
|
||||
type: 'string'
|
||||
})
|
||||
.option('format', {
|
||||
alias: 'f',
|
||||
describe: 'Output format',
|
||||
default: 'json',
|
||||
type: 'string',
|
||||
choices: ['json', 'namespaced-json', 'pot']
|
||||
})
|
||||
.option('format-indentation', {
|
||||
alias: 'fi',
|
||||
describe: 'Output format indentation',
|
||||
default: '\t',
|
||||
type: 'string'
|
||||
})
|
||||
.option('replace', {
|
||||
alias: 'r',
|
||||
describe: 'Replace the contents of output file if it exists (Merges by default)',
|
||||
default: false,
|
||||
type: 'boolean'
|
||||
})
|
||||
.option('sort', {
|
||||
alias: 's',
|
||||
describe: 'Sort strings in alphabetical order when saving',
|
||||
default: false,
|
||||
type: 'boolean'
|
||||
})
|
||||
.option('clean', {
|
||||
alias: 'c',
|
||||
describe: 'Remove obsolete strings when merging',
|
||||
default: false,
|
||||
type: 'boolean'
|
||||
})
|
||||
.option('key-as-default-value', {
|
||||
alias: 'k',
|
||||
describe: 'Use key as default value for translations',
|
||||
default: false,
|
||||
type: 'boolean'
|
||||
})
|
||||
.exitProcess(true)
|
||||
.parse(process.argv);
|
||||
|
||||
const extractTask = new ExtractTask(cli.input, cli.output, {
|
||||
replace: cli.replace,
|
||||
patterns: cli.patterns
|
||||
});
|
||||
|
||||
// Parsers
|
||||
const parsers: ParserInterface[] = [
|
||||
new PipeParser(),
|
||||
new DirectiveParser(),
|
||||
new ServiceParser()
|
||||
];
|
||||
if (cli.marker) {
|
||||
parsers.push(new FunctionParser({
|
||||
identifier: cli.marker
|
||||
}));
|
||||
}
|
||||
extractTask.setParsers(parsers);
|
||||
|
||||
// Post processors
|
||||
const postProcessors: PostProcessorInterface[] = [];
|
||||
if (cli.clean) {
|
||||
postProcessors.push(new PurgeObsoleteKeysPostProcessor());
|
||||
}
|
||||
if (cli.keyAsDefaultValue) {
|
||||
postProcessors.push(new KeyAsDefaultValuePostProcessor());
|
||||
}
|
||||
if (cli.sort) {
|
||||
postProcessors.push(new SortByKeyPostProcessor());
|
||||
}
|
||||
extractTask.setPostProcessors(postProcessors);
|
||||
|
||||
// Compiler
|
||||
const compiler: CompilerInterface = CompilerFactory.create(cli.format, {
|
||||
indentation: cli.formatIndentation
|
||||
});
|
||||
extractTask.setCompiler(compiler);
|
||||
|
||||
extractTask.execute();
|
||||
|
||||
console.log(donateMessage);
|
@@ -1,105 +0,0 @@
|
||||
import { Extractor } from '../utils/extractor';
|
||||
import { TranslationCollection } from '../utils/translation.collection';
|
||||
import { ParserInterface } from '../parsers/parser.interface';
|
||||
import { PipeParser } from '../parsers/pipe.parser';
|
||||
import { DirectiveParser } from '../parsers/directive.parser';
|
||||
import { ServiceParser } from '../parsers/service.parser';
|
||||
import { AstServiceParser } from '../parsers/ast-service.parser';
|
||||
import { CompilerInterface } from '../compilers/compiler.interface';
|
||||
import { JsonCompiler } from '../compilers/json.compiler';
|
||||
import { NamespacedJsonCompiler } from '../compilers/namespaced-json.compiler';
|
||||
import { PoCompiler } from '../compilers/po.compiler';
|
||||
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as cli from 'cli';
|
||||
|
||||
const options = cli.parse({
|
||||
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],
|
||||
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],
|
||||
sort: ['s', 'Sort translations in the output file in alphabetical order', 'boolean', false],
|
||||
clean: ['c', 'Remove obsolete strings when merging', 'boolean', false],
|
||||
experimental: ['e', 'Use experimental AST Service Parser', 'boolean', false]
|
||||
});
|
||||
|
||||
const patterns: string[] = [
|
||||
'/**/*.html',
|
||||
'/**/*.ts'
|
||||
];
|
||||
const parsers: ParserInterface[] = [
|
||||
new PipeParser(),
|
||||
new DirectiveParser(),
|
||||
options.experimental ? new AstServiceParser() : 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 normalizedOutput: string = path.resolve(options.output);
|
||||
|
||||
let outputDir: string = normalizedOutput;
|
||||
let outputFilename: string = `template.${ext}`;
|
||||
if (!fs.existsSync(normalizedOutput) || !fs.statSync(normalizedOutput).isDirectory()) {
|
||||
outputDir = path.dirname(normalizedOutput);
|
||||
outputFilename = path.basename(normalizedOutput);
|
||||
}
|
||||
const outputPath: string = path.join(outputDir, outputFilename);
|
||||
|
||||
[normalizedDir, outputDir].forEach(dir => {
|
||||
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) {
|
||||
cli.fatal(`The path you supplied was not found: '${dir}'`);
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
const extractor: Extractor = new Extractor(parsers, patterns);
|
||||
cli.info(`Extracting strings from '${normalizedDir}'`);
|
||||
|
||||
const extracted: TranslationCollection = extractor.process(normalizedDir);
|
||||
cli.ok(`* Extracted ${extracted.count()} strings`);
|
||||
|
||||
let collection: TranslationCollection = extracted;
|
||||
|
||||
if (!options.replace && fs.existsSync(outputPath)) {
|
||||
const existing: TranslationCollection = compiler.parse(fs.readFileSync(outputPath, 'utf-8'));
|
||||
if (existing.count() > 0) {
|
||||
collection = extracted.union(existing);
|
||||
cli.ok(`* Merged with ${existing.count()} existing strings`);
|
||||
}
|
||||
|
||||
if (options.clean) {
|
||||
const collectionCount = collection.count();
|
||||
collection = collection.intersect(extracted);
|
||||
const removeCount = collectionCount - collection.count();
|
||||
if (removeCount > 0) {
|
||||
cli.ok(`* Removed ${removeCount} obsolete strings`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (options.sort) {
|
||||
collection = collection.sort();
|
||||
}
|
||||
|
||||
fs.writeFileSync(outputPath, compiler.compile(collection));
|
||||
cli.ok(`* Saved to '${outputPath}'`);
|
||||
} catch (e) {
|
||||
cli.fatal(e.toString());
|
||||
}
|
179
src/cli/tasks/extract.task.ts
Normal file
179
src/cli/tasks/extract.task.ts
Normal file
@@ -0,0 +1,179 @@
|
||||
import { TranslationCollection } from '../../utils/translation.collection';
|
||||
import { TaskInterface } from './task.interface';
|
||||
import { ParserInterface } from '../../parsers/parser.interface';
|
||||
import { PostProcessorInterface } from '../../post-processors/post-processor.interface';
|
||||
import { CompilerInterface } from '../../compilers/compiler.interface';
|
||||
|
||||
import { cyan, green, bold, dim } from 'colorette';
|
||||
import * as glob from 'glob';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as mkdirp from 'mkdirp';
|
||||
|
||||
export interface ExtractTaskOptionsInterface {
|
||||
replace?: boolean;
|
||||
patterns?: string[];
|
||||
}
|
||||
|
||||
export class ExtractTask implements TaskInterface {
|
||||
|
||||
protected options: ExtractTaskOptionsInterface = {
|
||||
replace: false,
|
||||
patterns: []
|
||||
};
|
||||
|
||||
protected parsers: ParserInterface[] = [];
|
||||
protected postProcessors: PostProcessorInterface[] = [];
|
||||
protected compiler: CompilerInterface;
|
||||
|
||||
public constructor(protected inputs: string[], protected outputs: string[], options?: ExtractTaskOptionsInterface) {
|
||||
this.inputs = inputs.map(input => path.resolve(input));
|
||||
this.outputs = outputs.map(output => path.resolve(output));
|
||||
this.options = { ...this.options, ...options };
|
||||
}
|
||||
|
||||
public execute(): void {
|
||||
if (!this.compiler) {
|
||||
throw new Error('No compiler configured');
|
||||
}
|
||||
|
||||
this.printEnabledParsers();
|
||||
this.printEnabledPostProcessors();
|
||||
this.printEnabledCompiler();
|
||||
|
||||
this.out(bold('Extracting:'));
|
||||
const extracted = this.extract();
|
||||
this.out(green(`\nFound %d strings.\n`), extracted.count());
|
||||
|
||||
this.out(bold('Saving:'));
|
||||
|
||||
this.outputs.forEach(output => {
|
||||
let dir: string = output;
|
||||
let filename: string = `strings.${this.compiler.extension}`;
|
||||
if (!fs.existsSync(output) || !fs.statSync(output).isDirectory()) {
|
||||
dir = path.dirname(output);
|
||||
filename = path.basename(output);
|
||||
}
|
||||
|
||||
const outputPath: string = path.join(dir, filename);
|
||||
|
||||
let existing: TranslationCollection = new TranslationCollection();
|
||||
if (!this.options.replace && fs.existsSync(outputPath)) {
|
||||
existing = this.compiler.parse(fs.readFileSync(outputPath, 'utf-8'));
|
||||
}
|
||||
|
||||
// merge extracted strings with existing
|
||||
const draft = extracted.union(existing);
|
||||
|
||||
if (existing.isEmpty()) {
|
||||
this.out(dim(`- ${outputPath}`));
|
||||
} else {
|
||||
this.out(dim(`- ${outputPath} (merged)`));
|
||||
}
|
||||
|
||||
// Run collection through post processors
|
||||
const final = this.process(draft, extracted, existing);
|
||||
|
||||
// Save to file
|
||||
this.save(outputPath, final);
|
||||
});
|
||||
|
||||
this.out(green('\nDone.\n'));
|
||||
}
|
||||
|
||||
public setParsers(parsers: ParserInterface[]): this {
|
||||
this.parsers = parsers;
|
||||
return this;
|
||||
}
|
||||
|
||||
public setPostProcessors(postProcessors: PostProcessorInterface[]): this {
|
||||
this.postProcessors = postProcessors;
|
||||
return this;
|
||||
}
|
||||
|
||||
public setCompiler(compiler: CompilerInterface): this {
|
||||
this.compiler = compiler;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract strings from specified input dirs using configured parsers
|
||||
*/
|
||||
protected extract(): TranslationCollection {
|
||||
let extracted: TranslationCollection = new TranslationCollection();
|
||||
this.inputs.forEach(dir => {
|
||||
this.readDir(dir, this.options.patterns).forEach(path => {
|
||||
this.out(dim('- %s'), path);
|
||||
const contents: string = fs.readFileSync(path, 'utf-8');
|
||||
this.parsers.forEach(parser => {
|
||||
extracted = extracted.union(parser.extract(contents, path));
|
||||
});
|
||||
});
|
||||
});
|
||||
return extracted;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run strings through configured post processors
|
||||
*/
|
||||
protected process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
|
||||
this.postProcessors.forEach(postProcessor => {
|
||||
draft = postProcessor.process(draft, extracted, existing);
|
||||
});
|
||||
return draft;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile and save translations
|
||||
* @param collection
|
||||
*/
|
||||
protected save(output: string, collection: TranslationCollection): void {
|
||||
const dir = path.dirname(output);
|
||||
if (!fs.existsSync(dir)) {
|
||||
mkdirp.sync(dir);
|
||||
}
|
||||
fs.writeFileSync(output, this.compiler.compile(collection));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all files in dir matching patterns
|
||||
*/
|
||||
protected readDir(dir: string, patterns: string[]): string[] {
|
||||
return patterns.reduce((results, pattern) => {
|
||||
return glob.sync(dir + pattern)
|
||||
.filter(path => fs.statSync(path).isFile())
|
||||
.concat(results);
|
||||
}, []);
|
||||
}
|
||||
|
||||
protected out(...args: any[]): void {
|
||||
console.log.apply(this, arguments);
|
||||
}
|
||||
|
||||
protected printEnabledParsers(): void {
|
||||
this.out(cyan('Enabled parsers:'));
|
||||
if (this.parsers.length) {
|
||||
this.out(cyan(dim(this.parsers.map(parser => `- ${parser.constructor.name}`).join('\n'))));
|
||||
} else {
|
||||
this.out(cyan(dim('(none)')));
|
||||
}
|
||||
this.out();
|
||||
}
|
||||
|
||||
protected printEnabledPostProcessors(): void {
|
||||
this.out(cyan('Enabled post processors:'));
|
||||
if (this.postProcessors.length) {
|
||||
this.out(cyan(dim(this.postProcessors.map(postProcessor => `- ${postProcessor.constructor.name}`).join('\n'))));
|
||||
} else {
|
||||
this.out(cyan(dim('(none)')));
|
||||
}
|
||||
this.out();
|
||||
}
|
||||
|
||||
protected printEnabledCompiler(): void {
|
||||
this.out(cyan('Compiler:'));
|
||||
this.out(cyan(dim(`- ${this.compiler.constructor.name}`)));
|
||||
this.out();
|
||||
}
|
||||
|
||||
}
|
3
src/cli/tasks/task.interface.ts
Normal file
3
src/cli/tasks/task.interface.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export interface TaskInterface {
|
||||
execute(): void;
|
||||
}
|
17
src/compilers/compiler.factory.ts
Normal file
17
src/compilers/compiler.factory.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { CompilerInterface } from '../compilers/compiler.interface';
|
||||
import { JsonCompiler } from '../compilers/json.compiler';
|
||||
import { NamespacedJsonCompiler } from '../compilers/namespaced-json.compiler';
|
||||
import { PoCompiler } from '../compilers/po.compiler';
|
||||
|
||||
export class CompilerFactory {
|
||||
|
||||
public static create(format: string, options?: {}): CompilerInterface {
|
||||
switch (format) {
|
||||
case 'pot': return new PoCompiler(options);
|
||||
case 'json': return new JsonCompiler(options);
|
||||
case 'namespaced-json': return new NamespacedJsonCompiler(options);
|
||||
default: throw new Error(`Unknown format: ${format}`);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -2,6 +2,8 @@ import { TranslationCollection } from '../utils/translation.collection';
|
||||
|
||||
export interface CompilerInterface {
|
||||
|
||||
extension: string;
|
||||
|
||||
compile(collection: TranslationCollection): string;
|
||||
|
||||
parse(contents: string): TranslationCollection;
|
||||
|
@@ -1,14 +1,35 @@
|
||||
import { CompilerInterface } from './compiler.interface';
|
||||
import { TranslationCollection } from '../utils/translation.collection';
|
||||
import { stripBOM } from '../utils/utils';
|
||||
|
||||
import { flatten } from 'flat';
|
||||
|
||||
export class JsonCompiler implements CompilerInterface {
|
||||
|
||||
public indentation: string = '\t';
|
||||
|
||||
public extension: string = 'json';
|
||||
|
||||
public constructor(options?: any) {
|
||||
if (options && typeof options.indentation !== 'undefined') {
|
||||
this.indentation = options.indentation;
|
||||
}
|
||||
}
|
||||
|
||||
public compile(collection: TranslationCollection): string {
|
||||
return JSON.stringify(collection.values, null, '\t');
|
||||
return JSON.stringify(collection.values, null, this.indentation);
|
||||
}
|
||||
|
||||
public parse(contents: string): TranslationCollection {
|
||||
return new TranslationCollection(JSON.parse(contents));
|
||||
let values: any = JSON.parse(stripBOM(contents));
|
||||
if (this.isNamespacedJsonFormat(values)) {
|
||||
values = flatten(values);
|
||||
}
|
||||
return new TranslationCollection(values);
|
||||
}
|
||||
|
||||
protected isNamespacedJsonFormat(values: any): boolean {
|
||||
return Object.keys(values).some(key => typeof values[key] === 'object');
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,17 +1,30 @@
|
||||
import { CompilerInterface } from './compiler.interface';
|
||||
import { TranslationCollection } from '../utils/translation.collection';
|
||||
import { stripBOM } from '../utils/utils';
|
||||
|
||||
import * as flat from 'flat';
|
||||
import { flatten, unflatten } from 'flat';
|
||||
|
||||
export class NamespacedJsonCompiler implements CompilerInterface {
|
||||
|
||||
public indentation: string = '\t';
|
||||
|
||||
public extension = 'json';
|
||||
|
||||
public constructor(options?: any) {
|
||||
if (options && typeof options.indentation !== 'undefined') {
|
||||
this.indentation = options.indentation;
|
||||
}
|
||||
}
|
||||
|
||||
public compile(collection: TranslationCollection): string {
|
||||
const values = flat.unflatten(collection.values);
|
||||
return JSON.stringify(values, null, '\t');
|
||||
const values: {} = unflatten(collection.values, {
|
||||
object: true
|
||||
});
|
||||
return JSON.stringify(values, null, this.indentation);
|
||||
}
|
||||
|
||||
public parse(contents: string): TranslationCollection {
|
||||
const values = flat.flatten(JSON.parse(contents));
|
||||
const values: {} = flatten(JSON.parse(stripBOM(contents)));
|
||||
return new TranslationCollection(values);
|
||||
}
|
||||
|
||||
|
@@ -5,10 +5,14 @@ import * as gettext from 'gettext-parser';
|
||||
|
||||
export class PoCompiler implements CompilerInterface {
|
||||
|
||||
public extension: string = 'po';
|
||||
|
||||
/**
|
||||
* Translation domain
|
||||
*/
|
||||
public domain = '';
|
||||
public domain: string = '';
|
||||
|
||||
public constructor(options?: any) {}
|
||||
|
||||
public compile(collection: TranslationCollection): string {
|
||||
const data = {
|
||||
@@ -25,17 +29,17 @@ export class PoCompiler implements CompilerInterface {
|
||||
msgstr: collection.get(key)
|
||||
};
|
||||
return translations;
|
||||
}, <any> {})
|
||||
}, {} as any)
|
||||
}
|
||||
};
|
||||
|
||||
return gettext.po.compile(data, 'utf-8');
|
||||
return gettext.po.compile(data);
|
||||
}
|
||||
|
||||
public parse(contents: string): TranslationCollection {
|
||||
const collection = new TranslationCollection();
|
||||
|
||||
const po = gettext.po.parse(contents, 'utf-8');
|
||||
const po = gettext.po.parse(contents, 'utf8');
|
||||
if (!po.translations.hasOwnProperty(this.domain)) {
|
||||
return collection;
|
||||
}
|
||||
@@ -45,7 +49,7 @@ export class PoCompiler implements CompilerInterface {
|
||||
.reduce((values, key) => {
|
||||
values[key] = po.translations[this.domain][key].msgstr.pop();
|
||||
return values;
|
||||
}, <TranslationType> {});
|
||||
}, {} as TranslationType);
|
||||
|
||||
return new TranslationCollection(values);
|
||||
}
|
||||
|
2
src/declarations.d.ts
vendored
2
src/declarations.d.ts
vendored
@@ -1,3 +1 @@
|
||||
declare module 'cli';
|
||||
declare module 'flat';
|
||||
declare module 'gettext-parser';
|
||||
|
16
src/index.ts
16
src/index.ts
@@ -1,12 +1,24 @@
|
||||
export * from './utils/extractor';
|
||||
export * from './utils/translation.collection';
|
||||
export * from './utils/utils';
|
||||
|
||||
export * from './cli/cli';
|
||||
export * from './cli/tasks/task.interface';
|
||||
export * from './cli/tasks/extract.task';
|
||||
|
||||
export * from './parsers/parser.interface';
|
||||
export * from './parsers/abstract-template.parser';
|
||||
export * from './parsers/abstract-ast.parser';
|
||||
export * from './parsers/directive.parser';
|
||||
export * from './parsers/pipe.parser';
|
||||
export * from './parsers/service.parser';
|
||||
export * from './parsers/function.parser';
|
||||
|
||||
export * from './compilers/compiler.interface';
|
||||
export * from './compilers/compiler.factory';
|
||||
export * from './compilers/json.compiler';
|
||||
export * from './compilers/namespaced-json.compiler';
|
||||
export * from './compilers/po.compiler';
|
||||
|
||||
export * from './post-processors/post-processor.interface';
|
||||
export * from './post-processors/key-as-default-value.post-processor';
|
||||
export * from './post-processors/purge-obsolete-keys.post-processor';
|
||||
export * from './post-processors/sort-by-key.post-processor';
|
||||
|
43
src/parsers/abstract-ast.parser.ts
Normal file
43
src/parsers/abstract-ast.parser.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import {
|
||||
createSourceFile,
|
||||
SourceFile,
|
||||
CallExpression,
|
||||
Node,
|
||||
SyntaxKind,
|
||||
StringLiteral
|
||||
} from 'typescript';
|
||||
|
||||
export abstract class AbstractAstParser {
|
||||
|
||||
protected sourceFile: SourceFile;
|
||||
|
||||
protected createSourceFile(path: string, contents: string): SourceFile {
|
||||
return createSourceFile(path, contents, null, /*setParentNodes */ false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get strings from function call's first argument
|
||||
*/
|
||||
protected getStringLiterals(callNode: CallExpression): string[] {
|
||||
if (!callNode.arguments.length) {
|
||||
return[];
|
||||
}
|
||||
|
||||
const firstArg = callNode.arguments[0];
|
||||
return this.findNodes(firstArg, SyntaxKind.StringLiteral)
|
||||
.map((node: StringLiteral) => node.text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all child nodes of a kind
|
||||
*/
|
||||
protected findNodes(node: Node, kind: SyntaxKind): Node[] {
|
||||
const childrenNodes: Node[] = node.getChildren(this.sourceFile);
|
||||
const initialValue: Node[] = node.kind === kind ? [node] : [];
|
||||
|
||||
return childrenNodes.reduce((result: Node[], childNode: Node) => {
|
||||
return result.concat(this.findNodes(childNode, kind));
|
||||
}, initialValue);
|
||||
}
|
||||
|
||||
}
|
@@ -1,24 +0,0 @@
|
||||
export abstract class AbstractTemplateParser {
|
||||
|
||||
/**
|
||||
* Checks if file is of type javascript or typescript and
|
||||
* makes the assumption that it is an Angular Component
|
||||
*/
|
||||
protected _isAngularComponent(path: string): boolean {
|
||||
return (/\.ts|js$/i).test(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts inline template from components
|
||||
*/
|
||||
protected _extractInlineTemplate(contents: string): string {
|
||||
const regExp: RegExp = /template\s*:\s*(["'`])([^\1]*?)\1/;
|
||||
const match = regExp.exec(contents);
|
||||
if (match !== null) {
|
||||
return match[2];
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
}
|
@@ -1,159 +0,0 @@
|
||||
import { ParserInterface } from './parser.interface';
|
||||
import { TranslationCollection } from '../utils/translation.collection';
|
||||
import { syntaxKindToName } from '../utils/ast-utils';
|
||||
|
||||
import * as ts from 'typescript';
|
||||
|
||||
export class AstServiceParser implements ParserInterface {
|
||||
|
||||
protected _sourceFile: ts.SourceFile;
|
||||
|
||||
protected _instancePropertyName: any;
|
||||
protected _serviceClassName: string = 'TranslateService';
|
||||
protected _serviceMethodNames: string[] = ['get', 'instant'];
|
||||
|
||||
public extract(contents: string, path?: string): TranslationCollection {
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
|
||||
this._sourceFile = this._createSourceFile(path, contents);
|
||||
|
||||
this._instancePropertyName = this._getInstancePropertyName();
|
||||
if (!this._instancePropertyName) {
|
||||
return collection;
|
||||
}
|
||||
|
||||
const callNodes = this._findCallNodes();
|
||||
callNodes.forEach(callNode => {
|
||||
const keys: string[] = this._getCallArgStrings(callNode);
|
||||
if (keys && keys.length) {
|
||||
collection = collection.addKeys(keys);
|
||||
}
|
||||
});
|
||||
|
||||
return collection;
|
||||
}
|
||||
|
||||
protected _createSourceFile(path: string, contents: string): ts.SourceFile {
|
||||
return ts.createSourceFile(path, contents, null, /*setParentNodes */ false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect what the TranslateService instance property
|
||||
* is called by inspecting constructor params
|
||||
*/
|
||||
protected _getInstancePropertyName(): string {
|
||||
const constructorNode = this._findConstructorNode();
|
||||
if (!constructorNode) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const result = constructorNode.parameters.find(parameter => {
|
||||
// Skip if visibility modifier is not present (we want it set as an instance property)
|
||||
if (!parameter.modifiers) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Make sure className is of the correct type
|
||||
const className: string = ((parameter.type as ts.TypeReferenceNode).typeName as ts.Identifier).text;
|
||||
if (className !== this._serviceClassName) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
if (result) {
|
||||
return (result.name as ts.Identifier).text;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find first constructor
|
||||
*/
|
||||
protected _findConstructorNode(): ts.ConstructorDeclaration {
|
||||
const constructors = this._findNodes(this._sourceFile, ts.SyntaxKind.Constructor, true) as ts.ConstructorDeclaration[];
|
||||
if (constructors.length) {
|
||||
return constructors[0];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all calls to TranslateService methods
|
||||
*/
|
||||
protected _findCallNodes(node?: ts.Node): ts.CallExpression[] {
|
||||
if (!node) {
|
||||
node = this._sourceFile;
|
||||
}
|
||||
|
||||
let callNodes = this._findNodes(node, ts.SyntaxKind.CallExpression) as ts.CallExpression[];
|
||||
callNodes = callNodes
|
||||
// Only call expressions with arguments
|
||||
.filter(callNode => callNode.arguments.length > 0)
|
||||
// More filters
|
||||
.filter(callNode => {
|
||||
const propAccess = callNode.getChildAt(0).getChildAt(0) as ts.PropertyAccessExpression;
|
||||
if (!propAccess || propAccess.kind !== ts.SyntaxKind.PropertyAccessExpression) {
|
||||
return false;
|
||||
}
|
||||
if (!propAccess.getFirstToken() || propAccess.getFirstToken().kind !== ts.SyntaxKind.ThisKeyword) {
|
||||
return false;
|
||||
}
|
||||
if (propAccess.name.text !== this._instancePropertyName) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const methodAccess = callNode.getChildAt(0) as ts.PropertyAccessExpression;
|
||||
if (!methodAccess || methodAccess.kind !== ts.SyntaxKind.PropertyAccessExpression) {
|
||||
return false;
|
||||
}
|
||||
if (!methodAccess.name || this._serviceMethodNames.indexOf(methodAccess.name.text) === -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
return callNodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get strings from function call's first argument
|
||||
*/
|
||||
protected _getCallArgStrings(callNode: ts.CallExpression): string[] {
|
||||
if (!callNode.arguments.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
const firstArg = callNode.arguments[0];
|
||||
switch (firstArg.kind) {
|
||||
case ts.SyntaxKind.StringLiteral:
|
||||
case ts.SyntaxKind.FirstTemplateToken:
|
||||
return [(firstArg as ts.StringLiteral).text];
|
||||
case ts.SyntaxKind.ArrayLiteralExpression:
|
||||
return (firstArg as ts.ArrayLiteralExpression).elements
|
||||
.map((element: ts.StringLiteral) => element.text);
|
||||
case ts.SyntaxKind.Identifier:
|
||||
console.log('WARNING: We cannot extract variable values passed to TranslateService (yet)');
|
||||
break;
|
||||
default:
|
||||
console.log(`SKIP: Unknown argument type: '${syntaxKindToName(firstArg.kind)}'`, firstArg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all child nodes of a kind
|
||||
*/
|
||||
protected _findNodes(node: ts.Node, kind: ts.SyntaxKind, onlyOne: boolean = false): ts.Node[] {
|
||||
if (node.kind === kind && onlyOne) {
|
||||
return [node];
|
||||
}
|
||||
|
||||
const childrenNodes: ts.Node[] = node.getChildren(this._sourceFile);
|
||||
const initialValue: ts.Node[] = node.kind === kind ? [node] : [];
|
||||
|
||||
return childrenNodes.reduce((result: ts.Node[], childNode: ts.Node) => {
|
||||
return result.concat(this._findNodes(childNode, kind));
|
||||
}, initialValue);
|
||||
}
|
||||
|
||||
}
|
@@ -1,55 +1,83 @@
|
||||
import { ParserInterface } from './parser.interface';
|
||||
import { AbstractTemplateParser } from './abstract-template.parser';
|
||||
import { TranslationCollection } from '../utils/translation.collection';
|
||||
import { isPathAngularComponent, extractComponentInlineTemplate } from '../utils/utils';
|
||||
|
||||
import * as $ from 'cheerio';
|
||||
import { parseTemplate, TmplAstNode, TmplAstElement, TmplAstTextAttribute } from '@angular/compiler';
|
||||
|
||||
export class DirectiveParser extends AbstractTemplateParser implements ParserInterface {
|
||||
export class DirectiveParser implements ParserInterface {
|
||||
|
||||
public extract(contents: string, path?: string): TranslationCollection {
|
||||
if (path && this._isAngularComponent(path)) {
|
||||
contents = this._extractInlineTemplate(contents);
|
||||
public extract(template: string, path: string): TranslationCollection {
|
||||
if (path && isPathAngularComponent(path)) {
|
||||
template = extractComponentInlineTemplate(template);
|
||||
}
|
||||
|
||||
return this._parseTemplate(contents);
|
||||
}
|
||||
|
||||
protected _parseTemplate(template: string): TranslationCollection {
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
|
||||
template = this._normalizeTemplateAttributes(template);
|
||||
|
||||
const selector = '[translate], [ng2-translate]';
|
||||
$(template)
|
||||
.find(selector)
|
||||
.addBack(selector)
|
||||
.each((i: number, element: CheerioElement) => {
|
||||
const $element = $(element);
|
||||
const attr = $element.attr('translate') || $element.attr('ng2-translate');
|
||||
|
||||
if (attr) {
|
||||
collection = collection.add(attr);
|
||||
} else {
|
||||
$element
|
||||
.contents()
|
||||
.toArray()
|
||||
.filter(node => node.type === 'text')
|
||||
.map(node => node.nodeValue.trim())
|
||||
.filter(text => text.length > 0)
|
||||
.forEach(text => collection = collection.add(text));
|
||||
}
|
||||
});
|
||||
const nodes: TmplAstNode[] = this.parseTemplate(template, path);
|
||||
this.getTranslatableElements(nodes).forEach(element => {
|
||||
const key = this.getElementTranslateAttrValue(element) || this.getElementContents(element);
|
||||
collection = collection.add(key);
|
||||
});
|
||||
|
||||
return collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Angular's `[attr]="'val'"` syntax is not valid HTML,
|
||||
* so it can't be parsed by standard HTML parsers.
|
||||
* This method replaces `[attr]="'val'""` with `attr="val"`
|
||||
*/
|
||||
protected _normalizeTemplateAttributes(template: string): string {
|
||||
return template.replace(/\[([^\]]+)\]="'([^']*)'"/g, '$1="$2"');
|
||||
protected getTranslatableElements(nodes: TmplAstNode[]): TmplAstElement[] {
|
||||
return nodes
|
||||
.filter(element => this.isElement(element))
|
||||
.reduce((result: TmplAstElement[], element: TmplAstElement) => {
|
||||
return result.concat(this.findChildrenElements(element));
|
||||
}, [])
|
||||
.filter(element => this.isTranslatable(element));
|
||||
}
|
||||
|
||||
protected findChildrenElements(node: TmplAstNode): TmplAstElement[] {
|
||||
if (!this.isElement(node)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// If element has translate attribute all its contents is translatable
|
||||
// so we don't need to traverse any deeper
|
||||
if (this.isTranslatable(node)) {
|
||||
return [node];
|
||||
}
|
||||
|
||||
return node.children.reduce((result: TmplAstElement[], childNode: TmplAstNode) => {
|
||||
if (this.isElement(childNode)) {
|
||||
const children = this.findChildrenElements(childNode);
|
||||
return result.concat(children);
|
||||
}
|
||||
return result;
|
||||
}, [node]);
|
||||
}
|
||||
|
||||
protected parseTemplate(template: string, path: string): TmplAstNode[] {
|
||||
return parseTemplate(template, path).nodes;
|
||||
}
|
||||
|
||||
protected isElement(node: any): node is TmplAstElement {
|
||||
return node
|
||||
&& node.attributes !== undefined
|
||||
&& node.children !== undefined;
|
||||
}
|
||||
|
||||
protected isTranslatable(node: TmplAstNode): boolean {
|
||||
if (this.isElement(node) && node.attributes.some(attribute => attribute.name === 'translate')) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected getElementTranslateAttrValue(element: TmplAstElement): string {
|
||||
const attr: TmplAstTextAttribute = element.attributes.find(attribute => attribute.name === 'translate');
|
||||
return attr && attr.value || '';
|
||||
}
|
||||
|
||||
protected getElementContents(element: TmplAstElement): string {
|
||||
const contents = element.sourceSpan.start.file.content;
|
||||
const start = element.startSourceSpan.end.offset;
|
||||
const end = element.endSourceSpan.start.offset;
|
||||
return contents.substring(start, end).trim();
|
||||
}
|
||||
|
||||
}
|
||||
|
60
src/parsers/function.parser.ts
Normal file
60
src/parsers/function.parser.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { Node, CallExpression, SyntaxKind, Identifier } from 'typescript';
|
||||
|
||||
import { ParserInterface } from './parser.interface';
|
||||
import { AbstractAstParser } from './abstract-ast.parser';
|
||||
import { TranslationCollection } from '../utils/translation.collection';
|
||||
|
||||
export class FunctionParser extends AbstractAstParser implements ParserInterface {
|
||||
|
||||
protected functionIdentifier: string = 'marker';
|
||||
|
||||
public constructor(options?: any) {
|
||||
super();
|
||||
if (options && typeof options.identifier !== 'undefined') {
|
||||
this.functionIdentifier = options.identifier;
|
||||
}
|
||||
}
|
||||
|
||||
public extract(template: string, path: string): TranslationCollection {
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
|
||||
this.sourceFile = this.createSourceFile(path, template);
|
||||
|
||||
const callNodes = this.findCallNodes();
|
||||
callNodes.forEach(callNode => {
|
||||
const keys: string[] = this.getStringLiterals(callNode);
|
||||
if (keys && keys.length) {
|
||||
collection = collection.addKeys(keys);
|
||||
}
|
||||
});
|
||||
return collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all calls to marker function
|
||||
*/
|
||||
protected findCallNodes(node?: Node): CallExpression[] {
|
||||
if (!node) {
|
||||
node = this.sourceFile;
|
||||
}
|
||||
|
||||
let callNodes = this.findNodes(node, SyntaxKind.CallExpression) as CallExpression[];
|
||||
callNodes = callNodes
|
||||
.filter(callNode => {
|
||||
// Only call expressions with arguments
|
||||
if (callNode.arguments.length < 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const identifier = (callNode.getChildAt(0) as Identifier).text;
|
||||
if (identifier !== this.functionIdentifier) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
return callNodes;
|
||||
}
|
||||
|
||||
}
|
@@ -2,6 +2,6 @@ import { TranslationCollection } from '../utils/translation.collection';
|
||||
|
||||
export interface ParserInterface {
|
||||
|
||||
extract(contents: string, path?: string): TranslationCollection;
|
||||
extract(template: string, path: string): TranslationCollection;
|
||||
|
||||
}
|
||||
|
@@ -1,24 +1,24 @@
|
||||
import { ParserInterface } from './parser.interface';
|
||||
import { AbstractTemplateParser } from './abstract-template.parser';
|
||||
import { TranslationCollection } from '../utils/translation.collection';
|
||||
import { isPathAngularComponent, extractComponentInlineTemplate } from '../utils/utils';
|
||||
|
||||
export class PipeParser extends AbstractTemplateParser implements ParserInterface {
|
||||
export class PipeParser implements ParserInterface {
|
||||
|
||||
public extract(contents: string, path?: string): TranslationCollection {
|
||||
if (path && this._isAngularComponent(path)) {
|
||||
contents = this._extractInlineTemplate(contents);
|
||||
public extract(template: string, path: string): TranslationCollection {
|
||||
if (path && isPathAngularComponent(path)) {
|
||||
template = extractComponentInlineTemplate(template);
|
||||
}
|
||||
|
||||
return this._parseTemplate(contents);
|
||||
return this.parseTemplate(template);
|
||||
}
|
||||
|
||||
protected _parseTemplate(template: string): TranslationCollection {
|
||||
protected parseTemplate(template: string): TranslationCollection {
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
|
||||
const regExp: RegExp = /(['"`])([^>\1\r\n]*?)\1\s*\|\s*translate/g;
|
||||
const regExp: RegExp = /(['"`])((?:(?!\1).|\\\1)+)\1\s*\|\s*translate/g;
|
||||
let matches: RegExpExecArray;
|
||||
while (matches = regExp.exec(template)) {
|
||||
collection = collection.add(matches[2]);
|
||||
collection = collection.add(matches[2].split('\\\'').join('\''));
|
||||
}
|
||||
|
||||
return collection;
|
||||
|
@@ -1,59 +1,142 @@
|
||||
import {
|
||||
SourceFile,
|
||||
Node,
|
||||
ConstructorDeclaration,
|
||||
Identifier,
|
||||
TypeReferenceNode,
|
||||
ClassDeclaration,
|
||||
SyntaxKind,
|
||||
CallExpression,
|
||||
PropertyAccessExpression,
|
||||
isPropertyAccessExpression
|
||||
} from 'typescript';
|
||||
|
||||
import { ParserInterface } from './parser.interface';
|
||||
import { AbstractAstParser } from './abstract-ast.parser';
|
||||
import { TranslationCollection } from '../utils/translation.collection';
|
||||
|
||||
export class ServiceParser implements ParserInterface {
|
||||
export class ServiceParser extends AbstractAstParser implements ParserInterface {
|
||||
|
||||
public extract(contents: string, path?: string): TranslationCollection {
|
||||
protected sourceFile: SourceFile;
|
||||
|
||||
public extract(template: string, path: string): TranslationCollection {
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
|
||||
const translateServiceVar = this._extractTranslateServiceVar(contents);
|
||||
if (!translateServiceVar) {
|
||||
return collection;
|
||||
}
|
||||
|
||||
const methodRegExp: RegExp = /(?:get|instant)\s*\(\s*(\[?\s*(['"`])([^\1\r\n]*)\2\s*\]?)/;
|
||||
const regExp: RegExp = new RegExp(`\\.${translateServiceVar}\\.${methodRegExp.source}`, 'g');
|
||||
|
||||
let matches: RegExpExecArray;
|
||||
while (matches = regExp.exec(contents)) {
|
||||
if (this._stringContainsArray(matches[1])) {
|
||||
collection = collection.addKeys(this._stringToArray(matches[1]));
|
||||
} else {
|
||||
collection = collection.add(matches[3]);
|
||||
this.sourceFile = this.createSourceFile(path, template);
|
||||
const classNodes = this.findClassNodes(this.sourceFile);
|
||||
classNodes.forEach(classNode => {
|
||||
const constructorNode = this.findConstructorNode(classNode);
|
||||
if (!constructorNode) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const propertyName: string = this.findTranslateServicePropertyName(constructorNode);
|
||||
if (!propertyName) {
|
||||
return;
|
||||
}
|
||||
|
||||
const callNodes = this.findCallNodes(classNode, propertyName);
|
||||
callNodes.forEach(callNode => {
|
||||
const keys: string[] = this.getStringLiterals(callNode);
|
||||
if (keys && keys.length) {
|
||||
collection = collection.addKeys(keys);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts name of TranslateService variable for use in patterns
|
||||
* Detect what the TranslateService instance property
|
||||
* is called by inspecting constructor arguments
|
||||
*/
|
||||
protected _extractTranslateServiceVar(contents: string): string {
|
||||
const matches = contents.match(/([a-z0-9_]+)\s*:\s*TranslateService/i);
|
||||
if (matches === null) {
|
||||
return '';
|
||||
protected findTranslateServicePropertyName(constructorNode: ConstructorDeclaration): string {
|
||||
if (!constructorNode) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return matches[1];
|
||||
const result = constructorNode.parameters.find(parameter => {
|
||||
// Skip if visibility modifier is not present (we want it set as an instance property)
|
||||
if (!parameter.modifiers) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parameter has no type
|
||||
if (!parameter.type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Make sure className is of the correct type
|
||||
const parameterType: Identifier = (parameter.type as TypeReferenceNode).typeName as Identifier;
|
||||
if (!parameterType) {
|
||||
return false;
|
||||
}
|
||||
const className: string = parameterType.text;
|
||||
if (className !== 'TranslateService') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
if (result) {
|
||||
return (result.name as Identifier).text;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if string contains an array
|
||||
* Find class nodes
|
||||
*/
|
||||
protected _stringContainsArray(input: string): boolean {
|
||||
return input.startsWith('[') && input.endsWith(']');
|
||||
protected findClassNodes(node: Node): ClassDeclaration[] {
|
||||
return this.findNodes(node, SyntaxKind.ClassDeclaration) as ClassDeclaration[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts string to array
|
||||
* Find constructor
|
||||
*/
|
||||
protected _stringToArray(input: string): string[] {
|
||||
if (this._stringContainsArray(input)) {
|
||||
return eval(input);
|
||||
protected findConstructorNode(node: ClassDeclaration): ConstructorDeclaration {
|
||||
const constructorNodes = this.findNodes(node, SyntaxKind.Constructor) as ConstructorDeclaration[];
|
||||
if (constructorNodes) {
|
||||
return constructorNodes[0];
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
/**
|
||||
* Find all calls to TranslateService methods
|
||||
*/
|
||||
protected findCallNodes(node: Node, propertyIdentifier: string): CallExpression[] {
|
||||
let callNodes = this.findNodes(node, SyntaxKind.CallExpression) as CallExpression[];
|
||||
callNodes = callNodes
|
||||
.filter(callNode => {
|
||||
// Only call expressions with arguments
|
||||
if (callNode.arguments.length < 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const propAccess = callNode.getChildAt(0).getChildAt(0) as PropertyAccessExpression;
|
||||
if (!propAccess || !isPropertyAccessExpression(propAccess)) {
|
||||
return false;
|
||||
}
|
||||
if (!propAccess.getFirstToken() || propAccess.getFirstToken().kind !== SyntaxKind.ThisKeyword) {
|
||||
return false;
|
||||
}
|
||||
if (propAccess.name.text !== propertyIdentifier) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const methodAccess = callNode.getChildAt(0) as PropertyAccessExpression;
|
||||
if (!methodAccess || methodAccess.kind !== SyntaxKind.PropertyAccessExpression) {
|
||||
return false;
|
||||
}
|
||||
if (!methodAccess.name || (methodAccess.name.text !== 'get' && methodAccess.name.text !== 'instant' && methodAccess.name.text !== 'stream')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
return callNodes;
|
||||
}
|
||||
|
||||
}
|
||||
|
12
src/post-processors/key-as-default-value.post-processor.ts
Normal file
12
src/post-processors/key-as-default-value.post-processor.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { TranslationCollection } from '../utils/translation.collection';
|
||||
import { PostProcessorInterface } from './post-processor.interface';
|
||||
|
||||
export class KeyAsDefaultValuePostProcessor implements PostProcessorInterface {
|
||||
|
||||
public name: string = 'KeyAsDefaultValue';
|
||||
|
||||
public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
|
||||
return draft.map((key, val) => val === '' ? key : val);
|
||||
}
|
||||
|
||||
}
|
9
src/post-processors/post-processor.interface.ts
Normal file
9
src/post-processors/post-processor.interface.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { TranslationCollection } from '../utils/translation.collection';
|
||||
|
||||
export interface PostProcessorInterface {
|
||||
|
||||
name: string;
|
||||
|
||||
process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection;
|
||||
|
||||
}
|
12
src/post-processors/purge-obsolete-keys.post-processor.ts
Normal file
12
src/post-processors/purge-obsolete-keys.post-processor.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { TranslationCollection } from '../utils/translation.collection';
|
||||
import { PostProcessorInterface } from './post-processor.interface';
|
||||
|
||||
export class PurgeObsoleteKeysPostProcessor implements PostProcessorInterface {
|
||||
|
||||
public name: string = 'PurgeObsoleteKeys';
|
||||
|
||||
public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
|
||||
return draft.intersect(extracted);
|
||||
}
|
||||
|
||||
}
|
12
src/post-processors/sort-by-key.post-processor.ts
Normal file
12
src/post-processors/sort-by-key.post-processor.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { TranslationCollection } from '../utils/translation.collection';
|
||||
import { PostProcessorInterface } from './post-processor.interface';
|
||||
|
||||
export class SortByKeyPostProcessor implements PostProcessorInterface {
|
||||
|
||||
public name: string = 'SortByKey';
|
||||
|
||||
public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
|
||||
return draft.sort();
|
||||
}
|
||||
|
||||
}
|
@@ -1,19 +0,0 @@
|
||||
import * as ts from 'typescript';
|
||||
|
||||
export function printAllChildren(sourceFile: ts.SourceFile, node: ts.Node, depth = 0) {
|
||||
console.log(
|
||||
new Array(depth + 1).join('----'),
|
||||
`[${node.kind}]`,
|
||||
syntaxKindToName(node.kind),
|
||||
`[pos: ${node.pos}-${node.end}]`,
|
||||
':\t\t\t',
|
||||
node.getFullText(sourceFile).trim()
|
||||
);
|
||||
|
||||
depth++;
|
||||
node.getChildren(sourceFile).forEach(childNode => printAllChildren(sourceFile, childNode, depth));
|
||||
}
|
||||
|
||||
export function syntaxKindToName(kind: ts.SyntaxKind) {
|
||||
return ts.SyntaxKind[kind];
|
||||
}
|
17
src/utils/donate.ts
Normal file
17
src/utils/donate.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { yellow } from 'colorette';
|
||||
import * as boxen from 'boxen';
|
||||
import * as terminalLink from 'terminal-link';
|
||||
|
||||
const url = 'https://donate.biesbjerg.com';
|
||||
const link = terminalLink(url, url);
|
||||
const message = `
|
||||
If this tool saves you or your company time, please consider making a
|
||||
donation to support my work and the continued maintainence and development:
|
||||
|
||||
${yellow(link)}`;
|
||||
|
||||
export const donateMessage = boxen(message.trim(), {
|
||||
padding: 1,
|
||||
margin: 0,
|
||||
dimBorder: true
|
||||
});
|
@@ -1,38 +0,0 @@
|
||||
import { ParserInterface } from '../parsers/parser.interface';
|
||||
import { TranslationCollection } from './translation.collection';
|
||||
|
||||
import * as glob from 'glob';
|
||||
import * as fs from 'fs';
|
||||
|
||||
export class Extractor {
|
||||
|
||||
public constructor(public parsers: ParserInterface[], public patterns: string[]) { }
|
||||
|
||||
/**
|
||||
* Extract strings from dir
|
||||
*/
|
||||
public process(dir: string): TranslationCollection {
|
||||
let collection: TranslationCollection = new TranslationCollection();
|
||||
|
||||
this._readDir(dir, this.patterns).forEach(path => {
|
||||
const contents: string = fs.readFileSync(path, 'utf-8');
|
||||
this.parsers.forEach((parser: ParserInterface) => {
|
||||
collection = collection.union(parser.extract(contents, path));
|
||||
});
|
||||
});
|
||||
|
||||
return collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all files in dir matching patterns
|
||||
*/
|
||||
protected _readDir(dir: string, patterns: string[]): string[] {
|
||||
return patterns.reduce((results, pattern) => {
|
||||
return glob.sync(dir + pattern)
|
||||
.filter(path => fs.statSync(path).isFile())
|
||||
.concat(results);
|
||||
}, []);
|
||||
}
|
||||
|
||||
}
|
@@ -1,6 +1,6 @@
|
||||
export interface TranslationType {
|
||||
[key: string]: string
|
||||
};
|
||||
[key: string]: string;
|
||||
}
|
||||
|
||||
export class TranslationCollection {
|
||||
|
||||
@@ -11,15 +11,15 @@ export class TranslationCollection {
|
||||
}
|
||||
|
||||
public add(key: string, val: string = ''): TranslationCollection {
|
||||
return new TranslationCollection(Object.assign({}, this.values, { [key]: val }));
|
||||
return new TranslationCollection({ ...this.values, [key]: val });
|
||||
}
|
||||
|
||||
public addKeys(keys: string[]): TranslationCollection {
|
||||
const values = keys.reduce((results, key) => {
|
||||
results[key] = '';
|
||||
return results;
|
||||
}, <TranslationType> {});
|
||||
return new TranslationCollection(Object.assign({}, this.values, values));
|
||||
}, {} as TranslationType);
|
||||
return new TranslationCollection({ ...this.values, ...values });
|
||||
}
|
||||
|
||||
public remove(key: string): TranslationCollection {
|
||||
@@ -41,8 +41,16 @@ export class TranslationCollection {
|
||||
return new TranslationCollection(values);
|
||||
}
|
||||
|
||||
public map(callback: (key?: string, val?: string) => string): TranslationCollection {
|
||||
let values: TranslationType = {};
|
||||
this.forEach((key: string, val: string) => {
|
||||
values[key] = callback.call(this, key, val);
|
||||
});
|
||||
return new TranslationCollection(values);
|
||||
}
|
||||
|
||||
public union(collection: TranslationCollection): TranslationCollection {
|
||||
return new TranslationCollection(Object.assign({}, this.values, collection.values));
|
||||
return new TranslationCollection({ ...this.values, ...collection.values });
|
||||
}
|
||||
|
||||
public intersect(collection: TranslationCollection): TranslationCollection {
|
||||
|
22
src/utils/utils.ts
Normal file
22
src/utils/utils.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Assumes file is an Angular component if type is javascript/typescript
|
||||
*/
|
||||
export function isPathAngularComponent(path: string): boolean {
|
||||
return (/\.ts|js$/i).test(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract inline template from a component
|
||||
*/
|
||||
export function extractComponentInlineTemplate(contents: string): string {
|
||||
const regExp: RegExp = /template\s*:\s*(["'`])([^\1]*?)\1/;
|
||||
const match = regExp.exec(contents);
|
||||
if (match !== null) {
|
||||
return match[2];
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
export function stripBOM(contents: string): string {
|
||||
return contents.trim();
|
||||
}
|
@@ -35,4 +35,35 @@ describe('NamespacedJsonCompiler', () => {
|
||||
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}');
|
||||
});
|
||||
|
||||
it('should preserve numeric values on compile', () => {
|
||||
const collection = new TranslationCollection({
|
||||
'option.0': '',
|
||||
'option.1': '',
|
||||
'option.2': ''
|
||||
});
|
||||
const result: string = compiler.compile(collection);
|
||||
expect(result).to.equal('{\n\t"option": {\n\t\t"0": "",\n\t\t"1": "",\n\t\t"2": ""\n\t}\n}');
|
||||
});
|
||||
|
||||
it('should use custom indentation chars', () => {
|
||||
const collection = new TranslationCollection({
|
||||
'NAMESPACE.KEY.FIRST_KEY': '',
|
||||
'NAMESPACE.KEY.SECOND_KEY': 'VALUE'
|
||||
});
|
||||
const customCompiler = new NamespacedJsonCompiler({
|
||||
indentation: ' '
|
||||
});
|
||||
const result: string = customCompiler.compile(collection);
|
||||
expect(result).to.equal('{\n "NAMESPACE": {\n "KEY": {\n "FIRST_KEY": "",\n "SECOND_KEY": "VALUE"\n }\n }\n}');
|
||||
});
|
||||
|
||||
it('should not reorder keys when compiled', () => {
|
||||
const collection = new TranslationCollection({
|
||||
'BROWSE': '',
|
||||
'LOGIN': ''
|
||||
});
|
||||
const result: string = compiler.compile(collection);
|
||||
expect(result).to.equal('{\n\t"BROWSE": "",\n\t"LOGIN": ""\n}');
|
||||
});
|
||||
|
||||
});
|
@@ -1,154 +0,0 @@
|
||||
import { expect } from 'chai';
|
||||
|
||||
import { AstServiceParser } from '../../src/parsers/ast-service.parser';
|
||||
|
||||
class TestAstServiceParser extends AstServiceParser {
|
||||
|
||||
/*public getInstancePropertyName(): string {
|
||||
return this._getInstancePropertyName();
|
||||
}*/
|
||||
|
||||
}
|
||||
|
||||
describe('AstServiceParser', () => {
|
||||
|
||||
const componentFilename: string = 'test.component.ts';
|
||||
|
||||
let parser: TestAstServiceParser;
|
||||
|
||||
beforeEach(() => {
|
||||
parser = new TestAstServiceParser();
|
||||
});
|
||||
|
||||
/*it('should extract variable used for TranslateService', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
export class AppComponent {
|
||||
public constructor(
|
||||
_serviceA: ServiceA,
|
||||
public _serviceB: ServiceB,
|
||||
protected _translateService: TranslateService
|
||||
) { }
|
||||
`;
|
||||
const name = parser.getInstancePropertyName();
|
||||
expect(name).to.equal('_translateService');
|
||||
});*/
|
||||
|
||||
it('should extract strings in TranslateService\'s get() method', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
export class AppComponent {
|
||||
public constructor(protected _translateService: TranslateService) { }
|
||||
public test() {
|
||||
this._translateService.get('Hello World');
|
||||
}
|
||||
`;
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal(['Hello World']);
|
||||
});
|
||||
|
||||
it('should extract strings in TranslateService\'s instant() method', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
export class AppComponent {
|
||||
public constructor(protected _translateService: TranslateService) { }
|
||||
public test() {
|
||||
this._translateService.instant('Hello World');
|
||||
}
|
||||
`;
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal(['Hello World']);
|
||||
});
|
||||
|
||||
it('should extract array of strings in TranslateService\'s get() method', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
export class AppComponent {
|
||||
public constructor(protected _translateService: TranslateService) { }
|
||||
public test() {
|
||||
this._translateService.get(['Hello', 'World']);
|
||||
}
|
||||
`;
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal(['Hello', 'World']);
|
||||
});
|
||||
|
||||
it('should extract array of strings in TranslateService\'s instant() method', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
export class AppComponent {
|
||||
public constructor(protected _translateService: TranslateService) { }
|
||||
public test() {
|
||||
this._translateService.instant(['Hello', 'World']);
|
||||
}
|
||||
`;
|
||||
const key = parser.extract(contents, componentFilename).keys();
|
||||
expect(key).to.deep.equal(['Hello', 'World']);
|
||||
});
|
||||
|
||||
it('should not extract strings in get()/instant() methods of other services', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
export class AppComponent {
|
||||
public constructor(
|
||||
protected _translateService: TranslateService,
|
||||
protected _otherService: OtherService
|
||||
) { }
|
||||
public test() {
|
||||
this._otherService.get('Hello World');
|
||||
this._otherService.instant('Hi there');
|
||||
}
|
||||
`;
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it('should extract strings with liberal spacing', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
export class AppComponent {
|
||||
public constructor(
|
||||
protected _translateService: TranslateService,
|
||||
protected _otherService: OtherService
|
||||
) { }
|
||||
public test() {
|
||||
this._translateService.instant('Hello');
|
||||
this._translateService.get ( 'World' );
|
||||
this._translateService.instant ( ['How'] );
|
||||
this._translateService.get([ 'Are' ]);
|
||||
this._translateService.get([ 'You' , 'Today' ]);
|
||||
}
|
||||
`;
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal(['Hello', 'World', 'How', 'Are', 'You', 'Today']);
|
||||
});
|
||||
|
||||
it('should not extract string when not accessing property', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
export class AppComponent {
|
||||
public constructor(protected trans: TranslateService) { }
|
||||
public test() {
|
||||
trans.get("You are expected at {{time}}", {time: moment.format('H:mm')}).subscribe();
|
||||
}
|
||||
}
|
||||
`;
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it('should extract string with params on same line', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
export class AppComponent {
|
||||
public constructor(protected _translateService: TranslateService) { }
|
||||
public test() {
|
||||
this._translateService.get('You are expected at {{time}}', {time: moment.format('H:mm')});
|
||||
}
|
||||
}
|
||||
`;
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal(['You are expected at {{time}}']);
|
||||
});
|
||||
|
||||
});
|
@@ -2,78 +2,39 @@ import { expect } from 'chai';
|
||||
|
||||
import { DirectiveParser } from '../../src/parsers/directive.parser';
|
||||
|
||||
class TestDirectiveParser extends DirectiveParser {
|
||||
|
||||
public normalizeTemplateAttributes(template: string): string {
|
||||
return this._normalizeTemplateAttributes(template);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
describe('DirectiveParser', () => {
|
||||
|
||||
const templateFilename: string = 'test.template.html';
|
||||
const componentFilename: string = 'test.component.ts';
|
||||
|
||||
let parser: TestDirectiveParser;
|
||||
let parser: DirectiveParser;
|
||||
|
||||
beforeEach(() => {
|
||||
parser = new TestDirectiveParser();
|
||||
parser = new DirectiveParser();
|
||||
});
|
||||
|
||||
it('should extract contents when no translate attribute value is provided', () => {
|
||||
it('should not choke when no html is present in template', () => {
|
||||
const contents = 'Hello World';
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it('should use contents as key when there is no translate attribute value provided', () => {
|
||||
const contents = '<div translate>Hello World</div>';
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['Hello World']);
|
||||
});
|
||||
|
||||
it('should extract translate attribute if provided', () => {
|
||||
const contents = '<div translate="KEY">Hello World<div>';
|
||||
it('should use translate attribute value as key when provided', () => {
|
||||
const contents = '<div translate="MY_KEY">Hello World<div>';
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['KEY']);
|
||||
expect(keys).to.deep.equal(['MY_KEY']);
|
||||
});
|
||||
|
||||
it('should extract bound translate attribute as key if provided', () => {
|
||||
const contents = `<div [translate]="'KEY'">Hello World<div>`;
|
||||
it('should not process children when translate attribute is present', () => {
|
||||
const contents = `<div translate>Hello <strong translate>World</strong></div>`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['KEY']);
|
||||
});
|
||||
|
||||
it('should extract direct text nodes when no translate attribute value is provided', () => {
|
||||
const contents = `
|
||||
<div translate>
|
||||
<span>✓</span>
|
||||
Hello <strong>World</strong>
|
||||
Hi <em>there</em>
|
||||
</div>
|
||||
`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['Hello', 'Hi']);
|
||||
});
|
||||
|
||||
it('should extract direct text nodes of tags with a translate attribute', () => {
|
||||
const contents = `
|
||||
<div translate>
|
||||
<span>✓</span>
|
||||
Hello World
|
||||
<div translate>Hi there</div>
|
||||
</div>
|
||||
`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['Hello World', 'Hi there']);
|
||||
});
|
||||
|
||||
it('should extract translate attribute if provided or direct text nodes if not', () => {
|
||||
const contents = `
|
||||
<div translate="KEY">
|
||||
<span>✓</span>
|
||||
Hello World
|
||||
<p translate>Hi there</p>
|
||||
<p [translate]="'OTHER_KEY'">Lorem Ipsum</p>
|
||||
</div>
|
||||
`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['KEY', 'Hi there', 'OTHER_KEY']);
|
||||
expect(keys).to.deep.equal(['Hello <strong translate>World</strong>']);
|
||||
});
|
||||
|
||||
it('should extract and parse inline template', () => {
|
||||
@@ -88,34 +49,42 @@ describe('DirectiveParser', () => {
|
||||
expect(keys).to.deep.equal(['Hello World']);
|
||||
});
|
||||
|
||||
it('should extract contents when no ng2-translate attribute value is provided', () => {
|
||||
const contents = '<div ng2-translate>Hello World</div>';
|
||||
it('should extract contents when no translate attribute value is provided', () => {
|
||||
const contents = '<div translate>Hello World</div>';
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['Hello World']);
|
||||
});
|
||||
|
||||
it('should extract ng2-translate attribute if provided', () => {
|
||||
const contents = '<div ng2-translate="KEY">Hello World<div>';
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['KEY']);
|
||||
});
|
||||
|
||||
it('should extract bound ng2-translate attribute as key if provided', () => {
|
||||
const contents = `<div [ng2-translate]="'KEY'">Hello World<div>`;
|
||||
it('should extract translate attribute value if provided', () => {
|
||||
const contents = '<div translate="KEY">Hello World<div>';
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['KEY']);
|
||||
});
|
||||
|
||||
it('should not extract translate pipe in html tag', () => {
|
||||
const contents = `<p>{{ 'Audiobooks for personal development' | translate }}</p>`;
|
||||
const contents = `<p>{{ 'Audiobooks for personal development' | translate }}</p>`;
|
||||
const collection = parser.extract(contents, templateFilename);
|
||||
expect(collection.values).to.deep.equal({});
|
||||
});
|
||||
|
||||
it('should normalize bound attributes', () => {
|
||||
const contents = `<p [translate]="'KEY'">Hello World</p>`;
|
||||
const template = parser.normalizeTemplateAttributes(contents);
|
||||
expect(template).to.equal('<p translate="KEY">Hello World</p>');
|
||||
it('should extract contents from within custom tags', () => {
|
||||
const contents = `<custom-table><tbody><tr><td translate>Hello World</td></tr></tbody></custom-table>`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['Hello World']);
|
||||
});
|
||||
|
||||
it('should not cause error when no html is present in template', () => {
|
||||
const contents = `
|
||||
import { Component } from '@angular/core';
|
||||
@Component({
|
||||
template: '{{ variable }}'
|
||||
})
|
||||
export class MyComponent {
|
||||
variable: string
|
||||
}
|
||||
`;
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal([]);
|
||||
});
|
||||
|
||||
});
|
||||
|
30
tests/parsers/function.parser.spec.ts
Normal file
30
tests/parsers/function.parser.spec.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { expect } from 'chai';
|
||||
|
||||
import { FunctionParser } from '../../src/parsers/function.parser';
|
||||
|
||||
describe('FunctionParser', () => {
|
||||
|
||||
const componentFilename: string = 'test.component.ts';
|
||||
|
||||
let parser: FunctionParser;
|
||||
|
||||
beforeEach(() => {
|
||||
parser = new FunctionParser();
|
||||
});
|
||||
|
||||
|
||||
it('should extract strings using marker function', () => {
|
||||
const contents = `
|
||||
import { marker } from '@biesbjerg/ngx-translate-extract-marker';
|
||||
marker('Hello world');
|
||||
marker(['I', 'am', 'extracted']);
|
||||
otherFunction('But I am not');
|
||||
marker(message || 'binary expression');
|
||||
marker(message ? message : 'conditional operator');
|
||||
marker('FOO.bar');
|
||||
`;
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal(['Hello world', 'I', 'am', 'extracted', 'binary expression', 'conditional operator', 'FOO.bar']);
|
||||
});
|
||||
|
||||
});
|
@@ -18,26 +18,44 @@ describe('PipeParser', () => {
|
||||
expect(keys).to.deep.equal(['SomeKey_NotWorking']);
|
||||
});
|
||||
|
||||
it('should extract string using pipe, but between quotes only', () => {
|
||||
const contents = `<input class="form-control" type="text" placeholder="{{'user.settings.form.phone.placeholder' | translate}}" [formControl]="settingsForm.controls['phone']">`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['user.settings.form.phone.placeholder']);
|
||||
});
|
||||
|
||||
it('should extract interpolated strings using translate pipe', () => {
|
||||
const contents = `Hello {{ 'World' | translate }}`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['World']);
|
||||
});
|
||||
|
||||
it('should extract interpolated strings when translate pipe is used in conjunction with other pipes', () => {
|
||||
const contents = `Hello {{ 'World' | translate | upper }}`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['World']);
|
||||
});
|
||||
|
||||
it('should extract strings with escaped quotes', () => {
|
||||
const contents = `Hello {{ 'World\'s largest potato' | translate }}`;
|
||||
const contents = `Hello {{ 'World\\'s largest potato' | translate }}`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal([`World's largest potato`]);
|
||||
});
|
||||
|
||||
it('should extract strings with multiple escaped quotes', () => {
|
||||
const contents = `{{ 'C\\'est ok. C\\'est ok' | translate }}`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal([`C'est ok. C'est ok`]);
|
||||
});
|
||||
|
||||
it('should extract interpolated strings using translate pipe in attributes', () => {
|
||||
const contents = `<span attr="{{ 'Hello World' | translate }}"></span>`;
|
||||
const contents = `<span attr="{{ 'Hello World' | translate }}"></span>`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['Hello World']);
|
||||
});
|
||||
|
||||
it('should extract bound strings using translate pipe in attributes', () => {
|
||||
const contents = `<span [attr]="'Hello World' | translate"></span>`;
|
||||
const contents = `<span [attr]="'Hello World' | translate"></span>`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['Hello World']);
|
||||
});
|
||||
@@ -46,14 +64,14 @@ describe('PipeParser', () => {
|
||||
const contents = `
|
||||
<ion-header>
|
||||
<ion-navbar color="brand">
|
||||
<ion-title>{{ 'Info' | translate }}</ion-title>
|
||||
<ion-title>{{ 'Info' | translate }}</ion-title>
|
||||
</ion-navbar>
|
||||
</ion-header>
|
||||
|
||||
<ion-content>
|
||||
|
||||
<content-loading *ngIf="isLoading">
|
||||
{{ 'Loading...' | translate }}
|
||||
{{ 'Loading...' | translate }}
|
||||
</content-loading>
|
||||
|
||||
</ion-content>
|
||||
@@ -63,7 +81,7 @@ describe('PipeParser', () => {
|
||||
});
|
||||
|
||||
it('should extract strings on same line', () => {
|
||||
const contents = `<span [attr]="'Hello' | translate"></span><span [attr]="'World' | translate"></span>`;
|
||||
const contents = `<span [attr]="'Hello' | translate"></span><span [attr]="'World' | translate"></span>`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['Hello', 'World']);
|
||||
});
|
||||
@@ -73,7 +91,7 @@ describe('PipeParser', () => {
|
||||
<ion-list inset>
|
||||
<ion-item>
|
||||
<ion-icon item-left name="person" color="dark"></ion-icon>
|
||||
<ion-input formControlName="name" type="text" [placeholder]="'Name' | translate"></ion-input>
|
||||
<ion-input formControlName="name" type="text" [placeholder]="'Name' | translate"></ion-input>
|
||||
</ion-item>
|
||||
<ion-item>
|
||||
<p color="danger" danger *ngFor="let error of form.get('name').getError('remote')">
|
||||
@@ -82,11 +100,23 @@ describe('PipeParser', () => {
|
||||
</ion-item>
|
||||
</ion-list>
|
||||
<div class="form-actions">
|
||||
<button ion-button (click)="onSubmit()" color="secondary" block>{{ 'Create account' | translate }}</button>
|
||||
<button ion-button (click)="onSubmit()" color="secondary" block>{{ 'Create account' | translate }}</button>
|
||||
</div>
|
||||
`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['Name', 'Create account']);
|
||||
});
|
||||
|
||||
it('should not extract variables', () => {
|
||||
const contents = '<p>{{ message | translate }}</p>';
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it('should be able to extract without html', () => {
|
||||
const contents = `{{ 'message' | translate }}`;
|
||||
const keys = parser.extract(contents, templateFilename).keys();
|
||||
expect(keys).to.deep.equal(['message']);
|
||||
});
|
||||
|
||||
});
|
||||
|
@@ -4,10 +4,6 @@ import { ServiceParser } from '../../src/parsers/service.parser';
|
||||
|
||||
class TestServiceParser extends ServiceParser {
|
||||
|
||||
public extractTranslateServiceVar(contents: string): string {
|
||||
return this._extractTranslateServiceVar(contents);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
describe('ServiceParser', () => {
|
||||
@@ -20,18 +16,32 @@ describe('ServiceParser', () => {
|
||||
parser = new TestServiceParser();
|
||||
});
|
||||
|
||||
it('should extract variable used for TranslateService', () => {
|
||||
it('should support extracting binary expressions', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
export class AppComponent {
|
||||
public constructor(
|
||||
_serviceA: ServiceA,
|
||||
public _serviceB: ServiceB,
|
||||
protected _translateService: TranslateService
|
||||
) { }
|
||||
public constructor(protected _translateService: TranslateService) { }
|
||||
public test() {
|
||||
const message = 'The Message';
|
||||
this._translateService.get(message || 'Fallback message');
|
||||
}
|
||||
`;
|
||||
const name = parser.extractTranslateServiceVar(contents);
|
||||
expect(name).to.equal('_translateService');
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal(['Fallback message']);
|
||||
});
|
||||
|
||||
it('should support conditional operator', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
export class AppComponent {
|
||||
public constructor(protected _translateService: TranslateService) { }
|
||||
public test() {
|
||||
const message = 'The Message';
|
||||
this._translateService.get(message ? message : 'Fallback message');
|
||||
}
|
||||
`;
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal(['Fallback message']);
|
||||
});
|
||||
|
||||
it('should extract strings in TranslateService\'s get() method', () => {
|
||||
@@ -60,6 +70,19 @@ describe('ServiceParser', () => {
|
||||
expect(keys).to.deep.equal(['Hello World']);
|
||||
});
|
||||
|
||||
it('should extract strings in TranslateService\'s stream() method', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
export class AppComponent {
|
||||
public constructor(protected _translateService: TranslateService) { }
|
||||
public test() {
|
||||
this._translateService.stream('Hello World');
|
||||
}
|
||||
`;
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal(['Hello World']);
|
||||
});
|
||||
|
||||
it('should extract array of strings in TranslateService\'s get() method', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
@@ -86,7 +109,20 @@ describe('ServiceParser', () => {
|
||||
expect(key).to.deep.equal(['Hello', 'World']);
|
||||
});
|
||||
|
||||
it('should not extract strings in get()/instant() methods of other services', () => {
|
||||
it('should extract array of strings in TranslateService\'s stream() method', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
export class AppComponent {
|
||||
public constructor(protected _translateService: TranslateService) { }
|
||||
public test() {
|
||||
this._translateService.stream(['Hello', 'World']);
|
||||
}
|
||||
`;
|
||||
const key = parser.extract(contents, componentFilename).keys();
|
||||
expect(key).to.deep.equal(['Hello', 'World']);
|
||||
});
|
||||
|
||||
it('should not extract strings in get()/instant()/stream() methods of other services', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
export class AppComponent {
|
||||
@@ -97,6 +133,7 @@ describe('ServiceParser', () => {
|
||||
public test() {
|
||||
this._otherService.get('Hello World');
|
||||
this._otherService.instant('Hi there');
|
||||
this._otherService.stream('Hi there');
|
||||
}
|
||||
`;
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
@@ -129,7 +166,7 @@ describe('ServiceParser', () => {
|
||||
export class AppComponent {
|
||||
public constructor(protected trans: TranslateService) { }
|
||||
public test() {
|
||||
trans.get('Hello World');
|
||||
trans.get("You are expected at {{time}}", {time: moment.format('H:mm')}).subscribe();
|
||||
}
|
||||
}
|
||||
`;
|
||||
@@ -137,8 +174,7 @@ describe('ServiceParser', () => {
|
||||
expect(keys).to.deep.equal([]);
|
||||
});
|
||||
|
||||
// FAILS (Use AstServiceParser)
|
||||
/*it('should extract string with params on same line', () => {
|
||||
it('should extract string with params on same line', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
export class AppComponent {
|
||||
@@ -150,6 +186,68 @@ describe('ServiceParser', () => {
|
||||
`;
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal(['You are expected at {{time}}']);
|
||||
});*/
|
||||
});
|
||||
|
||||
it('should not crash when constructor parameter has no type', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
export class AppComponent {
|
||||
public constructor(protected _translateService) { }
|
||||
public test() {
|
||||
this._translateService.instant('Hello World');
|
||||
}
|
||||
`;
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it('should not extract variables', () => {
|
||||
const contents = `
|
||||
@Component({ })
|
||||
export class AppComponent {
|
||||
public constructor(protected translateService: TranslateService) { }
|
||||
public test() {
|
||||
this.translateService.get(["yes", variable]).then(translations => {
|
||||
console.log(translations[variable]);
|
||||
});
|
||||
}
|
||||
`;
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal(['yes']);
|
||||
});
|
||||
|
||||
it('should extract strings from all classes in the file', () => {
|
||||
const contents = `
|
||||
import { Injectable } from '@angular/core';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
export class Stuff {
|
||||
thing: string;
|
||||
translate: any;
|
||||
constructor(thing: string) {
|
||||
this.translate.get('Not me');
|
||||
this.thing = thing;
|
||||
}
|
||||
}
|
||||
@Injectable()
|
||||
export class MyComponent {
|
||||
constructor(public translate: TranslateService) {
|
||||
this.translate.instant("Extract me!");
|
||||
}
|
||||
}
|
||||
export class OtherClass {
|
||||
constructor(thing: string, _translate: TranslateService) {
|
||||
this._translate.get("Do not extract me");
|
||||
}
|
||||
}
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
constructor(public translate: TranslateService) {
|
||||
this.translate.instant("Hello!");
|
||||
}
|
||||
}
|
||||
`;
|
||||
const keys = parser.extract(contents, componentFilename).keys();
|
||||
expect(keys).to.deep.equal(['Extract me!', 'Hello!']);
|
||||
});
|
||||
|
||||
});
|
||||
|
@@ -1,39 +1,21 @@
|
||||
import { expect } from 'chai';
|
||||
|
||||
import { AbstractTemplateParser } from '../../src/parsers/abstract-template.parser';
|
||||
import { isPathAngularComponent, extractComponentInlineTemplate } from '../../src/utils/utils';
|
||||
|
||||
class TestTemplateParser extends AbstractTemplateParser {
|
||||
|
||||
public isAngularComponent(filePath: string): boolean {
|
||||
return this._isAngularComponent(filePath);
|
||||
}
|
||||
|
||||
public extractInlineTemplate(contents: string): string {
|
||||
return this._extractInlineTemplate(contents);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
describe('AbstractTemplateParser', () => {
|
||||
|
||||
let parser: TestTemplateParser;
|
||||
|
||||
beforeEach(() => {
|
||||
parser = new TestTemplateParser();
|
||||
});
|
||||
describe('Utils', () => {
|
||||
|
||||
it('should recognize js extension as angular component', () => {
|
||||
const result = parser.isAngularComponent('test.js');
|
||||
const result = isPathAngularComponent('test.js');
|
||||
expect(result).to.equal(true);
|
||||
});
|
||||
|
||||
it('should recognize ts extension as angular component', () => {
|
||||
const result = parser.isAngularComponent('test.ts');
|
||||
const result = isPathAngularComponent('test.ts');
|
||||
expect(result).to.equal(true);
|
||||
});
|
||||
|
||||
it('should not recognize html extension as angular component', () => {
|
||||
const result = parser.isAngularComponent('test.html');
|
||||
const result = isPathAngularComponent('test.html');
|
||||
expect(result).to.equal(false);
|
||||
});
|
||||
|
||||
@@ -45,10 +27,22 @@ describe('AbstractTemplateParser', () => {
|
||||
})
|
||||
export class TestComponent { }
|
||||
`;
|
||||
const template = parser.extractInlineTemplate(contents);
|
||||
const template = extractComponentInlineTemplate(contents);
|
||||
expect(template).to.equal('<p translate>Hello World</p>');
|
||||
});
|
||||
|
||||
it('should extract inline template without html', () => {
|
||||
const contents = `
|
||||
@Component({
|
||||
selector: 'test',
|
||||
template: '{{ "Hello World" | translate }}'
|
||||
})
|
||||
export class TestComponent { }
|
||||
`;
|
||||
const template = extractComponentInlineTemplate(contents);
|
||||
expect(template).to.equal('{{ "Hello World" | translate }}');
|
||||
});
|
||||
|
||||
it('should extract inline template spanning multiple lines', () => {
|
||||
const contents = `
|
||||
@Component({
|
||||
@@ -66,7 +60,7 @@ describe('AbstractTemplateParser', () => {
|
||||
})
|
||||
export class TestComponent { }
|
||||
`;
|
||||
const template = parser.extractInlineTemplate(contents);
|
||||
const template = extractComponentInlineTemplate(contents);
|
||||
expect(template).to.equal('\n\t\t\t\t\t<p>\n\t\t\t\t\t\tHello World\n\t\t\t\t\t</p>\n\t\t\t\t');
|
||||
});
|
||||
|
@@ -0,0 +1,31 @@
|
||||
import { expect } from 'chai';
|
||||
|
||||
import { PostProcessorInterface } from '../../src/post-processors/post-processor.interface';
|
||||
import { KeyAsDefaultValuePostProcessor } from '../../src/post-processors/key-as-default-value.post-processor';
|
||||
import { TranslationCollection } from '../../src/utils/translation.collection';
|
||||
|
||||
describe('KeyAsDefaultValuePostProcessor', () => {
|
||||
|
||||
let processor: PostProcessorInterface;
|
||||
|
||||
beforeEach(() => {
|
||||
processor = new KeyAsDefaultValuePostProcessor();
|
||||
});
|
||||
|
||||
it('should use key as default value', () => {
|
||||
const collection = new TranslationCollection({
|
||||
'I have no value': '',
|
||||
'I am already translated': 'Jeg er allerede oversat',
|
||||
'Use this key as value as well': ''
|
||||
});
|
||||
const extracted = new TranslationCollection();
|
||||
const existing = new TranslationCollection();
|
||||
|
||||
expect(processor.process(collection, extracted, existing).values).to.deep.equal({
|
||||
'I have no value': 'I have no value',
|
||||
'I am already translated': 'Jeg er allerede oversat',
|
||||
'Use this key as value as well': 'Use this key as value as well'
|
||||
});
|
||||
});
|
||||
|
||||
});
|
@@ -0,0 +1,36 @@
|
||||
import { expect } from 'chai';
|
||||
|
||||
import { PostProcessorInterface } from '../../src/post-processors/post-processor.interface';
|
||||
import { PurgeObsoleteKeysPostProcessor } from '../../src/post-processors/purge-obsolete-keys.post-processor';
|
||||
import { TranslationCollection } from '../../src/utils/translation.collection';
|
||||
|
||||
describe('PurgeObsoleteKeysPostProcessor', () => {
|
||||
|
||||
let processor: PostProcessorInterface;
|
||||
|
||||
beforeEach(() => {
|
||||
processor = new PurgeObsoleteKeysPostProcessor();
|
||||
});
|
||||
|
||||
it('should purge obsolete keys', () => {
|
||||
const collection = new TranslationCollection({
|
||||
'I am completely new': '',
|
||||
'I already exist': '',
|
||||
'I already exist but was not present in extract': ''
|
||||
});
|
||||
const extracted = new TranslationCollection({
|
||||
'I am completely new': '',
|
||||
'I already exist': ''
|
||||
});
|
||||
const existing = new TranslationCollection({
|
||||
'I already exist': '',
|
||||
'I already exist but was not present in extract': ''
|
||||
});
|
||||
|
||||
expect(processor.process(collection, extracted, existing).values).to.deep.equal({
|
||||
'I am completely new': '',
|
||||
'I already exist': ''
|
||||
});
|
||||
});
|
||||
|
||||
});
|
33
tests/post-processors/sort-by-key.post-processor.spec.ts
Normal file
33
tests/post-processors/sort-by-key.post-processor.spec.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { expect } from 'chai';
|
||||
|
||||
import { PostProcessorInterface } from '../../src/post-processors/post-processor.interface';
|
||||
import { SortByKeyPostProcessor } from '../../src/post-processors/sort-by-key.post-processor';
|
||||
import { TranslationCollection } from '../../src/utils/translation.collection';
|
||||
|
||||
describe('SortByKeyPostProcessor', () => {
|
||||
|
||||
let processor: PostProcessorInterface;
|
||||
|
||||
beforeEach(() => {
|
||||
processor = new SortByKeyPostProcessor();
|
||||
});
|
||||
|
||||
it('should sort keys alphanumerically', () => {
|
||||
const collection = new TranslationCollection({
|
||||
'z': 'last value',
|
||||
'a': 'a value',
|
||||
'9': 'a numeric key',
|
||||
'b': 'another value'
|
||||
});
|
||||
const extracted = new TranslationCollection();
|
||||
const existing = new TranslationCollection();
|
||||
|
||||
expect(processor.process(collection, extracted, existing).values).to.deep.equal({
|
||||
'9': 'a numeric key',
|
||||
'a': 'a value',
|
||||
'b': 'another value',
|
||||
'z': 'last value'
|
||||
});
|
||||
});
|
||||
|
||||
});
|
@@ -69,7 +69,7 @@ describe('StringCollection', () => {
|
||||
|
||||
it('should intersect with passed collection', () => {
|
||||
collection = collection.addKeys(['red', 'green', 'blue']);
|
||||
const newCollection = new TranslationCollection( { red: '', blue: '' });
|
||||
const newCollection = new TranslationCollection( { red: '', blue: '' });
|
||||
expect(collection.intersect(newCollection).values).to.deep.equal({ red: '', blue: '' });
|
||||
});
|
||||
|
||||
@@ -79,10 +79,16 @@ describe('StringCollection', () => {
|
||||
expect(collection.intersect(newCollection).values).to.deep.equal({ red: 'rød', blue: 'blå' });
|
||||
});
|
||||
|
||||
it('should sort translations in alphabetical order', () => {
|
||||
it('should sort keys alphabetically', () => {
|
||||
collection = new TranslationCollection({ red: 'rød', green: 'grøn', blue: 'blå' });
|
||||
collection = collection.sort();
|
||||
expect(collection.keys()).deep.equal(['blue', 'green', 'red']);
|
||||
});
|
||||
|
||||
it('should map values', () => {
|
||||
collection = new TranslationCollection({ red: 'rød', green: 'grøn', blue: 'blå' });
|
||||
collection = collection.map((key, val) => 'mapped value');
|
||||
expect(collection.values).to.deep.equal({ red: 'mapped value', green: 'mapped value', blue: 'mapped value' });
|
||||
});
|
||||
|
||||
});
|
||||
|
@@ -5,7 +5,7 @@
|
||||
"noImplicitAny": true,
|
||||
"removeComments": true,
|
||||
"declaration": true,
|
||||
"target": "es5",
|
||||
"target": "es6",
|
||||
"lib": [
|
||||
"dom",
|
||||
"es2015"
|
||||
|
Reference in New Issue
Block a user