Skip to content

Commit

Permalink
fix: display the language name instead of the label
Browse files Browse the repository at this point in the history
  • Loading branch information
kabeep committed Sep 1, 2024
1 parent a313d51 commit c00f813
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 15 deletions.
1 change: 1 addition & 0 deletions .xo-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/locale/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/locale/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '多义词',
Expand Down
24 changes: 17 additions & 7 deletions src/pipeline/after.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
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<ArgumentVector, 'from' | 'showPhonetics' | 'showSource' | 'showDetail'>,
options: Pick<ArgumentVector, 'from' | 'to' | 'showPhonetics' | 'showSource' | 'showDetail'>,
) {
const { from, showPhonetics, showSource, showDetail } = options;
const { from, to, showPhonetics, showSource, showDetail } = options;
const columns: number = Math.max(getColumns(), 32) - 32;

if (showSource) {
let sourceText = result.from.text.value;
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}` : ''}`);
}
Expand All @@ -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);
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, { from, showPhonetics, showSource, showDetail });
after(result, { from, to, showPhonetics, showSource, showDetail });
}

export default pipeline;
1 change: 0 additions & 1 deletion src/utils/get-columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
7 changes: 7 additions & 0 deletions src/utils/get-native-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { iso6391X } from '@kabeep/node-translate';

function getNativeName(code?: string) {
return iso6391X.getNativeName(code || '');
}

export default getNativeName;
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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';
8 changes: 4 additions & 4 deletions src/utils/typography.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/utils/get-native-name.spec.ts
Original file line number Diff line number Diff line change
@@ -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('简体中文');
});

0 comments on commit c00f813

Please sign in to comment.