diff --git a/package.json b/package.json index 6140229..2dc1ae7 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/pipeline/after.ts b/src/pipeline/after.ts index 77072f2..f856eb5 100644 --- a/src/pipeline/after.ts +++ b/src/pipeline/after.ts @@ -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, + options: Pick, ) { - const { showPhonetics, showSource, showDetail } = options; + const { from, showPhonetics, showSource, showDetail } = options; const columns: number = Math.max(getColumns(), 32) - 32; if (showSource) { @@ -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}` : ''}`); } diff --git a/src/pipeline/index.ts b/src/pipeline/index.ts index cea8da4..9c49cb9 100644 --- a/src/pipeline/index.ts +++ b/src/pipeline/index.ts @@ -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; diff --git a/src/utils/get-language-name.ts b/src/utils/get-language-name.ts new file mode 100644 index 0000000..2442839 --- /dev/null +++ b/src/utils/get-language-name.ts @@ -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; diff --git a/src/utils/index.ts b/src/utils/index.ts index 542acff..bde3677 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -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'; diff --git a/src/utils/typography.ts b/src/utils/typography.ts index 509ee88..c5e7fc4 100644 --- a/src/utils/typography.ts +++ b/src/utils/typography.ts @@ -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}`); } } diff --git a/test/utils/get-language-name.spec.ts b/test/utils/get-language-name.spec.ts new file mode 100644 index 0000000..d4852f2 --- /dev/null +++ b/test/utils/get-language-name.spec.ts @@ -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'); +});