Skip to content

Commit

Permalink
Option to use a custom common language file
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinNovak committed Dec 23, 2021
1 parent 4ece06c commit 88fa4ae
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/linguini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ type LinguiniOptions = {
* @defaultValue `10`
*/
replacementLevels: number;
/**
* Full path to a custom common language file.
*/
customCommonFile?: string;
};

export class Linguini {
private options: LinguiniOptions = {
replacementLevels: 10,
customCommonFile: undefined,
};
private comData: { [location: string]: string } = {};
private langDatas: {
Expand All @@ -35,9 +40,16 @@ export class Linguini {
constructor(folderPath: string, fileName: string, options: Partial<LinguiniOptions> = {}) {
this.options = Object.assign(this.options, options);

// Validate options
if (this.options.customCommonFile && !FileUtils.exists(this.options.customCommonFile)) {
throw new LinguiniError(
`Custom common file does not exist: ${this.options.customCommonFile}`
);
}

// Locate common file
let comFileName = `${fileName}.common.json`;
let comFilePath = path.join(folderPath, comFileName);
let comFilePath =
this.options.customCommonFile ?? path.join(folderPath, `${fileName}.common.json`);

let comVars: { [refName: string]: string } = {};
if (FileUtils.exists(comFilePath)) {
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/data/custom-common-file/custom.common.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"custom": {
"description": "This is a custom common file."
},
"links": {
"github": "{{COM:github.baseUrl}}/{{COM:github.tag}}"
},
"github": {
"baseUrl": "https://github.com",
"tag": "KevinNovak"
}
}
36 changes: 36 additions & 0 deletions tests/unit/linguini.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,42 @@ describe('Linguini', (): void => {
let fileName = 'lang';
let linguini = new Linguini(folderPath, fileName);

describe('#constructor()', (): void => {
it('Custom common file', (): void => {
let linguini = new Linguini(folderPath, fileName, {
customCommonFile: path.join(
__dirname,
'./data/custom-common-file/custom.common.json'
),
});

let com = linguini.getCom('custom.description');
expect(com).to.equal('This is a custom common file.');

let line = linguini.get('intro.myGitHub', 'en', TypeMappers.String);
expect(line).to.equal('My GitHub is: https://github.com/KevinNovak');
});

it('Custom common file does not exist', (): void => {
let customCommonFile = path.join(
__dirname,
'./data/custom-common-file/does-not-exist.common.json'
);

function myFunction(): void {
new Linguini(folderPath, fileName, {
customCommonFile,
});
}

assert.throw(
myFunction,
LinguiniError,
`Custom common file does not exist: ${customCommonFile}`
);
});
});

describe('#get()', (): void => {
it('Regular expression', (): void => {
let regex = linguini.get('regexes.hello', 'en', TypeMappers.RegExp);
Expand Down

0 comments on commit 88fa4ae

Please sign in to comment.