diff --git a/.xo-config.json b/.xo-config.json index 09565f4..56c07ca 100644 --- a/.xo-config.json +++ b/.xo-config.json @@ -14,6 +14,7 @@ "unicorn/filename-case": "off", "unicorn/prefer-string-replace-all": "off", "@typescript-eslint/no-unused-expressions": "off", + "@typescript-eslint/prefer-nullish-coalescing": "off", "@typescript-eslint/consistent-type-definitions": [ "error", "interface" diff --git a/src/locale/en-US.ts b/src/locale/en-US.ts index 37e7f18..b73fe94 100644 --- a/src/locale/en-US.ts +++ b/src/locale/en-US.ts @@ -15,7 +15,7 @@ export default { CMD_SPIN_STDIN: 'Loading stdin...', CMD_SPIN_TRANSLATE: 'Waiting translate API...', - CMD_TYPO_TRANSLATION: 'Translation', + CMD_TYPO_TRANSLATION: 'System Language', CMD_TYPO_SOURCE: 'Source Text', CMD_TYPO_SYNONYM: 'Synonym', CMD_TYPO_POLYSEMY: 'Polysemy', diff --git a/src/locale/zh-CN.ts b/src/locale/zh-CN.ts index a3073c7..861767f 100644 --- a/src/locale/zh-CN.ts +++ b/src/locale/zh-CN.ts @@ -15,7 +15,7 @@ export default { CMD_SPIN_STDIN: '加载标准输入流...', CMD_SPIN_TRANSLATE: '等待翻译API...', - CMD_TYPO_TRANSLATION: '译文', + CMD_TYPO_TRANSLATION: '系统语言', CMD_TYPO_SOURCE: '原文', CMD_TYPO_SYNONYM: '同近义词', CMD_TYPO_POLYSEMY: '多义词', diff --git a/src/pipeline/after.ts b/src/pipeline/after.ts index 13aaffa..c81c403 100644 --- a/src/pipeline/after.ts +++ b/src/pipeline/after.ts @@ -1,12 +1,21 @@ import type { TranslationOption } from '@kabeep/node-translate'; import type { ArgumentVector } from '../shared/index.js'; -import { getColumns, getLanguageName, Polysemy, Sentence, Source, Synonym, Translation } from '../utils/index.js'; +import { + getColumns, + getLanguageName, + getNativeName, + Polysemy, + Sentence, + Source, + Synonym, + Translation, +} from '../utils/index.js'; function after( result: TranslationOption, - options: Pick, + options: Pick, ) { - const { from, showPhonetics, showSource, showDetail } = options; + const { from, to, showPhonetics, showSource, showDetail } = options; const columns: number = Math.max(getColumns(), 32) - 32; if (showSource) { @@ -14,11 +23,11 @@ function after( showPhonetics && result.from.text.phonetics && (sourceText += ` /${result.from.text.phonetics}/`); let sourceColor = 'White'; - result.from.language.didYouMean && (sourceColor = 'Yellow'); - result.from.text.didYouMean && (sourceColor = 'Red'); + result.from.text.didYouMean && (sourceColor = 'Yellow'); + result.from.language.didYouMean && (sourceColor = 'Red'); const isOverflow = sourceText.length > columns; - const sourceLanguage = getLanguageName(from, result.from.language.iso); + const sourceLanguage = getLanguageName(result.from.language.iso || from); const source = new Source(isOverflow ? '' : sourceText).toString(sourceLanguage, sourceColor); console.log(`${source}\n${isOverflow ? ` > ${sourceText}` : ''}`); } @@ -28,7 +37,8 @@ function after( const isOverflow = translationText.length > columns; if (showPhonetics || showSource || showDetail) { - const translation = new Translation(isOverflow ? '' : translationText).toString(); + const sourceLanguage = getNativeName(to); + const translation = new Translation(isOverflow ? '' : translationText).toString(sourceLanguage); console.log(`${translation}${isOverflow ? `\n > ${translationText}` : ''}`); } else { console.log(result.to.text.value); diff --git a/src/pipeline/index.ts b/src/pipeline/index.ts index 9c49cb9..1a12f10 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, { from, showPhonetics, showSource, showDetail }); + after(result, { from, to, showPhonetics, showSource, showDetail }); } export default pipeline; diff --git a/src/utils/get-columns.ts b/src/utils/get-columns.ts index 5c0dfee..8f6104a 100644 --- a/src/utils/get-columns.ts +++ b/src/utils/get-columns.ts @@ -2,7 +2,6 @@ import { stdout } from 'node:process'; import type { WriteStream } from 'node:tty'; function getColumns() { - // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing return (stdout as (WriteStream & { fd: 1 }) | undefined)?.columns || 0; } diff --git a/src/utils/get-native-name.ts b/src/utils/get-native-name.ts new file mode 100644 index 0000000..4b05460 --- /dev/null +++ b/src/utils/get-native-name.ts @@ -0,0 +1,7 @@ +import { iso6391X } from '@kabeep/node-translate'; + +function getNativeName(code?: string) { + return iso6391X.getNativeName(code || ''); +} + +export default getNativeName; diff --git a/src/utils/index.ts b/src/utils/index.ts index bde3677..731f243 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,6 +1,7 @@ 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 getNativeName } from './get-native-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 c5e7fc4..7ed3dfe 100644 --- a/src/utils/typography.ts +++ b/src/utils/typography.ts @@ -2,15 +2,15 @@ import Exception from '@kabeep/exception'; import { locale } from '../index.js'; export class Translation extends Exception { - toString() { - this.name = locale.CMD_TYPO_TRANSLATION; + toString(languageName?: string) { + this.name = languageName || locale.CMD_TYPO_TRANSLATION; return this.info('#15141b.bgGreen'); } } export class Source extends Exception { - toString(code: string = locale.CMD_TYPO_SOURCE, bgColor = 'White') { - this.name = code; + toString(languageName?: string, bgColor = 'White') { + this.name = languageName || locale.CMD_TYPO_SOURCE; return this.info(`#15141b.bg${bgColor}`); } } diff --git a/test/utils/get-native-name.spec.ts b/test/utils/get-native-name.spec.ts new file mode 100644 index 0000000..5cfcc82 --- /dev/null +++ b/test/utils/get-native-name.spec.ts @@ -0,0 +1,17 @@ +import { expect, test } from 'vitest'; +import getNativeName from '../../src/utils/get-native-name'; + +test('getNativeName - should return empty string when code is undefined', () => { + const result = getNativeName(); + expect(result).toBe(''); +}); + +test('getNativeName - should return empty string when code is invalid', () => { + const result = getNativeName('auto'); + expect(result).toBe(''); +}); + +test('getNativeName - should return the native name when code is provided', () => { + const result = getNativeName('zh-CN'); + expect(result).toBe('简体中文'); +}); \ No newline at end of file