Skip to content

Commit

Permalink
fix: Show language name with -s option to resolve an indeterminate la…
Browse files Browse the repository at this point in the history
…nguage for auto detection
  • Loading branch information
kabeep committed May 9, 2024
1 parent 4f3bc0d commit 1171d67
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"dependencies": {
"@kabeep/exception": "^1.2.3",
"@kabeep/node-translate": "^1.1.2",
"@kabeep/node-translate": "1.1.3",
"ora": "^8.0.1",
"os-locale": "^6.0.2",
"yargs": "^17.7.2"
Expand Down
9 changes: 5 additions & 4 deletions src/pipeline/after.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { TranslationOption } from '@kabeep/node-translate';
import type { ArgumentVector } from '../shared/index.js';
import { getColumns, Polysemy, Sentence, Source, Synonym, Translation } from '../utils/index.js';
import { getColumns, getLanguageName, Polysemy, Sentence, Source, Synonym, Translation } from '../utils/index.js';

function after(
result: TranslationOption,
options: Pick<ArgumentVector, 'showPhonetics' | 'showSource' | 'showDetail'>,
options: Pick<ArgumentVector, 'from' | 'showPhonetics' | 'showSource' | 'showDetail'>,
) {
const { showPhonetics, showSource, showDetail } = options;
const { from, showPhonetics, showSource, showDetail } = options;
const columns: number = Math.max(getColumns(), 32) - 32;

if (showSource) {
Expand All @@ -18,7 +18,8 @@ function after(
result.from.text.didYouMean && (sourceColor = 'Red');

const isOverflow = sourceText.length > columns;
const source = new Source(isOverflow ? '' : sourceText).toString(sourceColor);
const sourceLanguage = getLanguageName(from, result.from.language.iso);
const source = new Source(isOverflow ? '' : sourceText).toString(sourceLanguage, sourceColor);
console.log(`${source}\n${isOverflow ? ` > ${sourceText}` : ''}`);
}

Expand Down
2 changes: 1 addition & 1 deletion src/pipeline/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async function pipeline(argv: ArgumentVector) {
}

const { showPhonetics, showSource, showDetail } = argv;
after(result, { showPhonetics, showSource, showDetail });
after(result, { from, showPhonetics, showSource, showDetail });
}

export default pipeline;
11 changes: 11 additions & 0 deletions src/utils/get-language-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { iso6391X } from '@kabeep/node-translate';

function getLanguageName(from?: string, detectFrom?: string) {
const code = !from || from === 'auto' ? detectFrom : from;

const language = code ? iso6391X.getName(code) : '';

return language || detectFrom;
}

export default getLanguageName;
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { default as catcher } from './catcher.js';
export { default as getColumns } from './get-columns.js';
export { default as getLanguageName } from './get-language-name.js';
export { default as isError } from './is-error.js';
export { Info, Success, Warning, Failure } from './notify.js';
export { Translation, Source, Synonym, Polysemy, Sentence, Language, Adaptive, Code } from './typography.js';
4 changes: 2 additions & 2 deletions src/utils/typography.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export class Translation extends Exception {
}

export class Source extends Exception {
toString(bgColor = 'White') {
this.name = locale.CMD_TYPO_SOURCE;
toString(code: string = locale.CMD_TYPO_SOURCE, bgColor = 'White') {
this.name = code;
return this.info(`#15141b.bg${bgColor}`);
}
}
Expand Down
27 changes: 27 additions & 0 deletions test/utils/get-language-name.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { expect, test } from 'vitest';
import getLanguageName from '../../src/utils/get-language-name';

test('getLanguageName - should return undefined when both from and detectFrom are undefined', () => {
const result = getLanguageName();
expect(result).toBe(undefined);
});

test('getLanguageName - should return the language name when from is provided', () => {
const result = getLanguageName('en', 'English');
expect(result).toBe('English');
});

test('getLanguageName - should return the detected language name when from is not provided', () => {
const result = getLanguageName(undefined, 'en');
expect(result).toBe('English');
});

test('getLanguageName - should return the detected language name when from is "auto"', () => {
const result = getLanguageName('auto', 'en');
expect(result).toBe('English');
});

test('getLanguageName - should return the detected language name when from is not valid', () => {
const result = getLanguageName('auto', 'Unknown');
expect(result).toBe('Unknown');
});

0 comments on commit 1171d67

Please sign in to comment.