Skip to content

Commit

Permalink
Merge pull request #74 from rwlogel/AddI18nBaseFile
Browse files Browse the repository at this point in the history
Add ability to change i18n basename to something other then 'messages'
  • Loading branch information
martinroob committed Mar 19, 2018
2 parents e4305d7 + e958552 commit c18eb1c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/xliffmerge/i-xliff-merge-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface IXliffMergeOptions {
defaultLanguage?: string; // the default language (the language, which is used in the original templates)
languages?: string[]; // all languages, if not specified via commandline
srcDir?: string; // Directory, where the master file is
i18nBaseFile?: string; // Basename for i18n input and output, default is 'messages'
i18nFile?: string; // master file, if not absolute, it is relative to srcDir
i18nFormat?: string; // xlf or xmb
encoding?: string; // encoding to write xml
Expand Down
27 changes: 23 additions & 4 deletions src/xliffmerge/xliff-merge-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class XliffMergeParameters {
private _allowIdChange: boolean;
private _defaultLanguage: string;
private _srcDir: string;
private _i18nBaseFile: string;
private _i18nFile: string;
private _i18nFormat: string;
private _encoding: string;
Expand Down Expand Up @@ -139,6 +140,9 @@ export class XliffMergeParameters {
// this must be after angularCompilerOptions to be preferred
this._genDir = profile.genDir;
}
if (profile.i18nBaseFile) {
this._i18nBaseFile = profile.i18nBaseFile;
}
if (profile.i18nFile) {
this._i18nFile = profile.i18nFile;
}
Expand Down Expand Up @@ -286,8 +290,12 @@ export class XliffMergeParameters {
commandOutput.debug('defaultLanguage:\t"%s"', this.defaultLanguage());
commandOutput.debug('srcDir:\t"%s"', this.srcDir());
commandOutput.debug('genDir:\t"%s"', this.genDir());
commandOutput.debug('i18nBaseFile:\t"%s"', this.i18nBaseFile());
commandOutput.debug('i18nFile:\t"%s"', this.i18nFile());
commandOutput.debug('languages:\t%s', this.languages());
for (let language of this.languages()) {
commandOutput.debug('outputFile[%s]:\t%s', language, this.generatedI18nFile(language));
}
commandOutput.debug('removeUnusedIds:\t%s', this.removeUnusedIds());
commandOutput.debug('supportNgxTranslate:\t%s', this.supportNgxTranslate());
if (this.supportNgxTranslate()) {
Expand Down Expand Up @@ -331,13 +339,24 @@ export class XliffMergeParameters {
return this._srcDir ? this._srcDir : '.';
}

/**
* The base file name of the xlif file for input and output.
* Default is messages
* @return {string}
*/
public i18nBaseFile(): string {
return this._i18nBaseFile ? this._i18nBaseFile : 'messages';
}

/**
* The master xlif file (the one generated by ng-xi18n).
* Default is <srcDir>/messages.xlf.
* Default is <srcDir>/<i18nBaseFile>.xlf.
* @return {string}
*/
public i18nFile(): string {
return this.srcDir() + '/' + (this._i18nFile ? this._i18nFile : 'messages.' + this.i18nFormat());
return this.srcDir() + '/' + (
this._i18nFile ? this._i18nFile : this.i18nBaseFile() + '.' + this.i18nFormat()
);
}

/**
Expand All @@ -355,7 +374,7 @@ export class XliffMergeParameters {
* @return {string} Path of file
*/
public generatedI18nFile(lang: string): string {
return this.genDir() + '/' + 'messages.' + lang + '.' + this.suffixForGeneratedI18nFile();
return this.genDir() + '/' + this.i18nBaseFile() + '.' + lang + '.' + this.suffixForGeneratedI18nFile();
}

private suffixForGeneratedI18nFile(): string {
Expand All @@ -375,7 +394,7 @@ export class XliffMergeParameters {
* @return {string} Path of file
*/
public generatedNgxTranslateFile(lang: string): string {
return this.genDir() + '/' + 'messages.' + lang + '.' + 'json';
return this.genDir() + '/' + this.i18nBaseFile() + '.' + lang + '.' + 'json';
}

/**
Expand Down
25 changes: 24 additions & 1 deletion src/xliffmerge/xliff-merge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ describe('XliffMerge test spec', () => {
expect(ws.writtenData()).not.toContain('autotranslate language "en" cannot be translated, because it is the source language');
done();
});

it('should accept i18n format xlf', (done) => {
let ws: WriterToString = new WriterToString();
let commandOut = new CommandOutput(ws);
Expand Down Expand Up @@ -332,6 +332,29 @@ describe('XliffMerge test spec', () => {
let xliffMergeCmd = XliffMerge.createFromOptions(commandOut, {verbose: true}, profileContent);
xliffMergeCmd.run();
expect(ws.writtenData()).toContain('languages: de,en,fr');
expect(ws.writtenData()).toContain('outputFile[de]: ./messages.de.xlf');
expect(ws.writtenData()).toContain('outputFile[en]: ./messages.en.xlf');
expect(ws.writtenData()).toContain('outputFile[fr]: ./messages.fr.xlf');
done();
});

it('should accept i18nBaseFile', (done) => {
let ws: WriterToString = new WriterToString();
let commandOut = new CommandOutput(ws);
let profileContent: IConfigFile = {
xliffmergeOptions: {
i18nBaseFile: 'custom_file',
languages: ['de', 'en', 'fr']
}
};
let xliffMergeCmd = XliffMerge.createFromOptions(commandOut, {verbose: true}, profileContent);
xliffMergeCmd.run();
expect(ws.writtenData()).toContain('i18nBaseFile: "custom_file"');
expect(ws.writtenData()).toContain('i18nFile: "./custom_file.xlf"');
expect(ws.writtenData()).toContain('outputFile[de]: ./custom_file.de.xlf');
expect(ws.writtenData()).toContain('outputFile[en]: ./custom_file.en.xlf');
expect(ws.writtenData()).toContain('outputFile[fr]: ./custom_file.fr.xlf');
expect(ws.writtenData()).toContain('i18nFile "./custom_file.xlf" is not readable');
done();
});

Expand Down
2 changes: 1 addition & 1 deletion src/xliffmerge/xliff-merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class XliffMerge {
console.log(' <language> has to be a valid language short string, e,g. "en", "de", "de-ch"');
console.log('');
console.log(' configfile can contain the following values:');
console.log('\tquiet verbose defaultLanguage languages srcDir i18nFile i18nFormat encoding genDir removeUnusedIds');
console.log('\tquiet verbose defaultLanguage languages srcDir i18nBaseFile i18nFile i18nFormat encoding genDir removeUnusedIds');
console.log('\tfor details please consult the home page https://github.com/martinroob/ngx-i18nsupport');
})
.action((languageArray) => {
Expand Down

0 comments on commit c18eb1c

Please sign in to comment.