Compare commits

...

8 Commits

Author SHA1 Message Date
Kim Biesbjerg
90b59793a7 Bump version to 6.0.4 2020-04-16 09:07:02 +02:00
Kim Biesbjerg
deb6b2373b Remove noUnusedLocals since they are removed by prettier on commit anyway 2020-04-16 09:06:40 +02:00
Esteban Gehring
17dec7deb8 fix(npm-package): move typescript and angular to peer-dependencies (#183)
fix(npm-package): move typescript and angular to peer-dependencies. Fixes #173
2020-04-16 08:58:51 +02:00
Lorent Lempereur
71f4f42b33 Tests about support of HTML tags in translations keys with GetText (#172)
- Verify that html tags are supported in translation keys
- Add typed support of gettext-parser
2020-04-01 12:19:30 +02:00
Kim Biesbjerg
73f39d625c add single quote rule 2020-04-01 10:21:11 +02:00
Kim Biesbjerg
3bf2aaca4e Trim leading/trailing whitespace. Closes #175 2020-03-31 12:24:57 +02:00
Kim Biesbjerg
05d1917f9d Bump version 6.0.3 2020-03-25 15:10:12 +01:00
Kim Biesbjerg
e50d52003b Fix created vs replaced/merge message 2020-03-25 15:09:29 +01:00
12 changed files with 100 additions and 21 deletions

View File

@@ -99,4 +99,8 @@ Examples:
using brace expansion
ngx-translate-extract -i './src/**/*.{ts,tsx,html}' -o strings.json Extract from ts, tsx and html
ngx-translate-extract -i './src/**/!(*.spec).{ts,html}' -o Extract from ts, html, excluding files with ".spec"
strings.json
strings.json
## Note for GetText users
Please pay attention of which version of `gettext-parser` you actually use in your project. For instance, `gettext-parser:1.2.2` does not support HTML tags in translation keys.

21
package-lock.json generated
View File

@@ -1,13 +1,14 @@
{
"name": "@biesbjerg/ngx-translate-extract",
"version": "6.0.0",
"version": "6.0.3",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@angular/compiler": {
"version": "9.0.7",
"resolved": "https://registry.npmjs.org/@angular/compiler/-/compiler-9.0.7.tgz",
"integrity": "sha512-hFpkuGpzxpK5h59LHHAjTFWsY6DCXZwgJFqvCuTPxWi/srvLGZRXrpC6Z1SlgHI9xxXaPfoa4uWw2VfA3BnqEg=="
"version": "9.1.1",
"resolved": "https://registry.npmjs.org/@angular/compiler/-/compiler-9.1.1.tgz",
"integrity": "sha512-u1IP6IzUgK6lIzrG1cxp96umXgtThyhuFn/KPoyVt7wPxZ6vVR0ZxjM7zycEcrMGzk0nf0nyOKaksJk9sTXTbg==",
"dev": true
},
"@babel/code-frame": {
"version": "7.8.3",
@@ -136,6 +137,15 @@
"integrity": "sha512-GcgAp7RXXGmA61spVEKZYpIy3/iV6GHbTW9f9kaKwHVQgnWitt6X026e+3N6j8ep1bkIWj83qPHQ3Y9Ft8FqiQ==",
"dev": true
},
"@types/gettext-parser": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/@types/gettext-parser/-/gettext-parser-4.0.0.tgz",
"integrity": "sha512-I/wvhr+l5M7IwBF1ADBfNQ6qGfUg85UTjj/AZWn09Y+POqyLe1cfbdJSMWzCobiJ3EJNY23MQCbP6jxQT81OTQ==",
"dev": true,
"requires": {
"@types/node": "*"
}
},
"@types/glob": {
"version": "7.1.1",
"resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.1.1.tgz",
@@ -2437,7 +2447,8 @@
"typescript": {
"version": "3.8.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.8.3.tgz",
"integrity": "sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w=="
"integrity": "sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==",
"dev": true
},
"util": {
"version": "0.10.4",

View File

@@ -1,6 +1,6 @@
{
"name": "@biesbjerg/ngx-translate-extract",
"version": "6.0.2",
"version": "6.0.4",
"description": "Extract strings from projects using ngx-translate",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
@@ -61,9 +61,11 @@
},
"config": {},
"devDependencies": {
"@angular/compiler": "^9.1.1",
"@types/braces": "^3.0.0",
"@types/chai": "^4.2.11",
"@types/flat": "^5.0.0",
"@types/gettext-parser": "4.0.0",
"@types/glob": "^7.1.1",
"@types/mkdirp": "^1.0.0",
"@types/mocha": "^7.0.2",
@@ -80,10 +82,14 @@
"tslint": "^6.1.0",
"tslint-config-prettier": "^1.18.0",
"tslint-eslint-rules": "^5.4.0",
"tslint-etc": "^1.10.1"
"tslint-etc": "^1.10.1",
"typescript": "^3.8.3"
},
"peerDependencies": {
"@angular/compiler": "^8.0.0 || ^9.0.0",
"typescript": "^3.0.0"
},
"dependencies": {
"@angular/compiler": "^9.0.7",
"@phenomnomnominal/tsquery": "^4.0.0",
"boxen": "^4.2.0",
"colorette": "^1.1.0",
@@ -93,7 +99,6 @@
"mkdirp": "^1.0.3",
"path": "^0.12.7",
"terminal-link": "^2.1.1",
"typescript": "^3.8.3",
"yargs": "^15.3.1"
}
}

View File

@@ -73,7 +73,7 @@ export class ExtractTask implements TaskInterface {
// Save
try {
let event = 'CREATED';
if (!fs.existsSync(outputPath)) {
if (fs.existsSync(outputPath)) {
this.options.replace ? event = 'REPLACED' : event = 'MERGED';
}
this.save(outputPath, final);

View File

@@ -1,7 +1,7 @@
import { CompilerInterface } from './compiler.interface';
import { TranslationCollection, TranslationType } from '../utils/translation.collection';
import * as gettext from 'gettext-parser';
import { po } from 'gettext-parser';
export class PoCompiler implements CompilerInterface {
public extension: string = 'po';
@@ -34,23 +34,24 @@ export class PoCompiler implements CompilerInterface {
}
};
return gettext.po.compile(data);
return po.compile(data).toString('utf8');
}
public parse(contents: string): TranslationCollection {
const collection = new TranslationCollection();
const po = gettext.po.parse(contents, 'utf8');
if (!po.translations.hasOwnProperty(this.domain)) {
const parsedPo = po.parse(contents, 'utf8');
if (!parsedPo.translations.hasOwnProperty(this.domain)) {
return collection;
}
const values = Object.keys(po.translations[this.domain])
const values = Object.keys(parsedPo.translations[this.domain])
.filter((key) => key.length > 0)
.reduce((result, key) => {
return {
...result,
[key]: po.translations[this.domain][key].msgstr.pop()
[key]: parsedPo.translations[this.domain][key].msgstr.pop()
};
}, {} as TranslationType);

View File

@@ -1 +0,0 @@
declare module 'gettext-parser';

View File

@@ -82,6 +82,6 @@ export class DirectiveParser implements ParserInterface {
}
protected cleanKey(val: string): string {
return val.replace(/\r?\n|\r|\t/g, '');
return val.replace(/\r?\n|\r|\t/g, '').trim();
}
}

View File

@@ -0,0 +1,24 @@
import { expect } from 'chai';
import { TranslationCollection } from '../../src/utils/translation.collection';
import { PoCompiler } from '../../src/compilers/po.compiler';
describe('PoCompiler', () => {
let compiler: PoCompiler;
beforeEach(() => {
compiler = new PoCompiler();
});
it('should still include html ', () => {
const collection = new TranslationCollection({
'A <strong>test</strong>': 'Un <strong>test</strong>',
'With a lot of <em>html</em> included': 'Avec beaucoup d\'<em>html</em> inclus'
});
const result: Buffer = Buffer.from(compiler.compile(collection));
expect(result.toString('utf8')).to.equal('msgid ""\nmsgstr ""\n"mime-version: 1.0\\n"\n"Content-Type: text/plain; charset=utf-8\\n"\n"Content-Transfer-Encoding: 8bit\\n"\n\nmsgid "A <strong>test</strong>"\nmsgstr "Un <strong>test</strong>"\n\nmsgid "With a lot of <em>html</em> included"\nmsgstr "Avec beaucoup d\'<em>html</em> inclus"');
});
});

View File

@@ -36,6 +36,12 @@ describe('DirectiveParser', () => {
expect(keys).to.deep.equal(['Hello <strong translate>World</strong>']);
});
it('should not exclude html tags in children', () => {
const contents = `<div translate>Hello <strong>World</strong></div>`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['Hello <strong>World</strong>']);
});
it('should extract and parse inline template', () => {
const contents = `
@Component({
@@ -117,4 +123,22 @@ describe('DirectiveParser', () => {
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['client.search.searchBtn']);
});
it('should extract contents without indent spaces and trim leading/trailing whitespace', () => {
const contents = `
<div translate>
this is an example
of a long label
</div>
<div>
<p translate>
this is an example
of a long label
</p>
</div>
`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['this is an example of a long label']);
});
});

View File

@@ -36,6 +36,17 @@ describe('MarkerParser', () => {
expect(keys).to.deep.equal(['Hello world', 'This is a very very very very long line.', 'Mix of different types']);
});
it('should extract split strings while keeping html tags', () => {
const contents = `
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
_('Hello ' + 'world');
_('This <em>is</em> a ' + 'very ' + 'very ' + 'very ' + 'very ' + 'long line.');
_('Mix ' + \`of \` + 'different ' + \`types\`);
`;
const keys = parser.extract(contents, componentFilename).keys();
expect(keys).to.deep.equal(['Hello world', 'This <em>is</em> a very very very very long line.', 'Mix of different types']);
});
it('should extract the strings', () => {
const contents = `
import { marker } from '@biesbjerg/ngx-translate-extract-marker';

View File

@@ -1,7 +1,6 @@
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"noUnusedLocals": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,

View File

@@ -112,6 +112,7 @@
"check-operator",
"check-separator",
"check-type"
]
],
"quotemark": [true, "single"]
}
}