Skip to content

Commit

Permalink
Check if language files exists
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinNovak committed Oct 17, 2021
1 parent 9f9c1f6 commit e36fac6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
31 changes: 23 additions & 8 deletions src/linguini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,23 @@ export class Linguini {
* @returns The retrieved language file item.
*/
public getRaw(location: string, langCode: string, variables?: { [name: string]: string }): any {
let data = this.langDatas[langCode].data[location];
if (!data) {
let langData = this.langDatas[langCode];
if (langData === undefined) {
let filePath = path.join(this.folderPath, `${this.fileName}.${langCode}.json`);
throw new LinguiniError(`Language file '${filePath}' not found.`);
}

let jsonValue = langData.data[location];
if (jsonValue === undefined) {
let filePath = path.join(this.folderPath, `${this.fileName}.${langCode}.json`);
throw new LinguiniError(`Language item '${location}' does not exist in '${filePath}'.`);
}
data = JSON.parse(JSON.stringify(this.langDatas[langCode].data[location]));

jsonValue = JSON.parse(JSON.stringify(jsonValue));
if (variables) {
data = DataUtils.replaceVariablesInObj(data, variables);
jsonValue = DataUtils.replaceVariablesInObj(jsonValue, variables);
}
return data;
return jsonValue;
}

/**
Expand All @@ -143,13 +150,20 @@ export class Linguini {
langCode: string,
variables?: { [name: string]: string }
): string {
let ref = this.langDatas[langCode].refs[`REF:${location}`];
if (!ref) {
let langData = this.langDatas[langCode];
if (langData === undefined) {
let filePath = path.join(this.folderPath, `${this.fileName}.${langCode}.json`);
throw new LinguiniError(`Language file '${filePath}' not found.`);
}

let ref = langData.refs[`REF:${location}`];
if (ref === undefined) {
let filePath = path.join(this.folderPath, `${this.fileName}.${langCode}.json`);
throw new LinguiniError(
`Reference string '${location}' does not exist in '${filePath}'.`
);
}

if (variables) {
ref = DataUtils.replaceVariablesInObj(ref, variables);
}
Expand All @@ -166,12 +180,13 @@ export class Linguini {
*/
public getCom(location: string, variables?: { [name: string]: string }): string {
let com = this.comData[`COM:${location}`];
if (!com) {
if (com === undefined) {
let filePath = path.join(this.folderPath, `${this.fileName}.common.json`);
throw new LinguiniError(
`Common reference string '${location}' does not exist in '${filePath}'.`
);
}

if (variables) {
com = DataUtils.replaceVariablesInObj(com, variables);
}
Expand Down
30 changes: 27 additions & 3 deletions tests/unit/linguini.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,19 @@ describe('Linguini', (): void => {
expect(greetingTwo).to.equal('Hello, nice to meet you Clark Kent!');
});

it('Does not exist', (): void => {
it('Lang file does not exist', (): void => {
function myFunction(): void {
linguini.getRaw('myCategory.myName', 'badLang');
}

assert.throw(
myFunction,
LinguiniError,
`Language file '${folderPath}\\lang.badLang.json' not found`
);
});

it('Location does not exist', (): void => {
function myFunction(): void {
linguini.getRaw('badCategory.badName', 'en');
}
Expand All @@ -74,7 +86,19 @@ describe('Linguini', (): void => {
expect(ref).to.equal('Blue');
});

it('Does not exist', (): void => {
it('Lang file does not exist', (): void => {
function myFunction(): void {
linguini.getRef('myCategory.myName', 'badLang');
}

assert.throw(
myFunction,
LinguiniError,
`Language file '${folderPath}\\lang.badLang.json' not found`
);
});

it('Location does not exist', (): void => {
function myFunction(): void {
linguini.getRef('badCategory.badName', 'en');
}
Expand All @@ -93,7 +117,7 @@ describe('Linguini', (): void => {
expect(com).to.equal('https://github.com/KevinNovak');
});

it('Does not exist', (): void => {
it('Location does not exist', (): void => {
function myFunction(): void {
linguini.getCom('badCategory.badName');
}
Expand Down

0 comments on commit e36fac6

Please sign in to comment.