IT-TOOLS-PRO v1 - 定制版开发者工具站
12个原创工具(批量条码/二维码识别/在线翻译/OCR/面单设计/AI修图/网站收藏/PDF工具/图片工具/文档转换等) + 卡密授权系统(CardAuth对接,30分钟试用,换卡/撤销) + 体验优化(中文默认/收藏/最近使用/菜单展开/宽屏/品牌页脚)
This commit is contained in:
commit
01ff133ac9
528 files changed
+50026
No files matched your search
@@ -0,0 +1,12 @@
|
||||
import { Language } from '@vicons/tabler';
|
||||
import { defineTool } from '../tool';
|
||||
import { translate } from '@/plugins/i18n.plugin';
|
||||
|
||||
export const tool = defineTool({
|
||||
name: translate('tools.translator.title'),
|
||||
path: '/translator',
|
||||
description: translate('tools.translator.description'),
|
||||
keywords: ['translate', 'translation', 'language', 'i18n', 'google', 'chinese', 'english', 'japanese', '翻译', '语言', '互译'],
|
||||
component: () => import('./translator.vue'),
|
||||
icon: Language,
|
||||
});
|
||||
@@ -0,0 +1,330 @@
|
||||
<script setup lang="ts">
|
||||
import GenHistory from '@/tools/gen-history/GenHistory.vue';
|
||||
import { useGenHistory } from '@/tools/gen-history/gen-history';
|
||||
|
||||
const genHist = useGenHistory('translator');
|
||||
interface Lang {
|
||||
label: string;
|
||||
value: string;
|
||||
}
|
||||
|
||||
const langs: Lang[] = [
|
||||
{ label: '自动检测', value: 'auto' },
|
||||
{ label: '中文(简体)', value: 'zh-CN' },
|
||||
{ label: '中文(繁体)', value: 'zh-TW' },
|
||||
{ label: '英语', value: 'en' },
|
||||
{ label: '日语', value: 'ja' },
|
||||
{ label: '韩语', value: 'ko' },
|
||||
{ label: '法语', value: 'fr' },
|
||||
{ label: '德语', value: 'de' },
|
||||
{ label: '西班牙语', value: 'es' },
|
||||
{ label: '俄语', value: 'ru' },
|
||||
{ label: '葡萄牙语', value: 'pt' },
|
||||
{ label: '意大利语', value: 'it' },
|
||||
{ label: '阿拉伯语', value: 'ar' },
|
||||
{ label: '越南语', value: 'vi' },
|
||||
{ label: '泰语', value: 'th' },
|
||||
{ label: '印尼语', value: 'id' },
|
||||
];
|
||||
|
||||
import { licenseHeaders } from '../license/license';
|
||||
|
||||
const source = ref('Hello! This is a handy developer tools website with barcode generation, OCR and translation.\n欢迎使用在线翻译工具,支持中英日韩等16种语言互译。');
|
||||
const from = ref('auto');
|
||||
const to = ref('zh-CN');
|
||||
const result = ref('');
|
||||
const detected = ref('');
|
||||
const loading = ref(false);
|
||||
const error = ref('');
|
||||
|
||||
const langName = (v: string) => langs.find(l => l.value === v)?.label ?? v;
|
||||
|
||||
function swap() {
|
||||
if (from.value === 'auto') {
|
||||
return;
|
||||
}
|
||||
const t = from.value;
|
||||
from.value = to.value;
|
||||
to.value = t;
|
||||
}
|
||||
|
||||
async function translateText(allowAutoSwitch = true) {
|
||||
const text = source.value.trim();
|
||||
if (!text) {
|
||||
result.value = '';
|
||||
detected.value = '';
|
||||
error.value = '';
|
||||
return;
|
||||
}
|
||||
loading.value = true;
|
||||
error.value = '';
|
||||
try {
|
||||
const url = `/api/translate?client=gtx&sl=${encodeURIComponent(from.value)}&tl=${encodeURIComponent(to.value)}&dt=t&q=${encodeURIComponent(text)}`;
|
||||
const res = await fetch(url, { headers: licenseHeaders('translator') });
|
||||
if (!res.ok) {
|
||||
throw new Error(`HTTP ${res.status}`);
|
||||
}
|
||||
const data = await res.json();
|
||||
const segments: string[] = (data?.[0] ?? []).map((seg: any[]) => seg?.[0] ?? '').filter(Boolean);
|
||||
if (!segments.length) {
|
||||
throw new Error('empty translation');
|
||||
}
|
||||
result.value = segments.join('');
|
||||
detected.value = typeof data?.[2] === 'string' ? data[2] : '';
|
||||
genHist.add({ title: text.slice(0, 40), detail: result.value.slice(0, 80), dedupeTitle: true });
|
||||
|
||||
// source equals target: auto-switch target language and retry once
|
||||
if (allowAutoSwitch && from.value === 'auto' && detected.value && detected.value === to.value) {
|
||||
to.value = detected.value.startsWith('zh') ? 'en' : 'zh-CN';
|
||||
await translateText(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch (e: any) {
|
||||
// fallback: MyMemory
|
||||
try {
|
||||
const pair = `${from.value === 'auto' ? 'Autodetect' : from.value}|${to.value}`;
|
||||
const res = await fetch(`/api/translate-mm?q=${encodeURIComponent(text)}&langpair=${encodeURIComponent(pair)}`, { headers: licenseHeaders('translator') });
|
||||
const data = await res.json();
|
||||
const translated = data?.responseData?.translatedText;
|
||||
if (!translated || /MYMEMORY WARNING|INVALID/i.test(String(translated))) {
|
||||
throw new Error(String(translated || 'fallback failed'));
|
||||
}
|
||||
result.value = String(translated);
|
||||
detected.value = '';
|
||||
genHist.add({ title: text.slice(0, 40), detail: result.value.slice(0, 80), dedupeTitle: true });
|
||||
}
|
||||
catch (e2: any) {
|
||||
error.value = `翻译失败:${e2?.message ?? e2}`;
|
||||
result.value = '';
|
||||
}
|
||||
}
|
||||
finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function copyResult() {
|
||||
if (!result.value) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await navigator.clipboard.writeText(result.value);
|
||||
}
|
||||
catch {
|
||||
const ta = document.createElement('textarea');
|
||||
ta.value = result.value;
|
||||
document.body.appendChild(ta);
|
||||
ta.select();
|
||||
document.execCommand('copy');
|
||||
ta.remove();
|
||||
}
|
||||
}
|
||||
|
||||
function clearAll() {
|
||||
source.value = '';
|
||||
result.value = '';
|
||||
detected.value = '';
|
||||
error.value = '';
|
||||
}
|
||||
|
||||
// debounce auto translate
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
watch([source, from, to], () => {
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(translateText, 600);
|
||||
});
|
||||
|
||||
// fit viewport: no page scroll
|
||||
const rootEl = ref<HTMLElement>();
|
||||
const rootStyle = ref<Record<string, string>>({});
|
||||
|
||||
function fit() {
|
||||
const el = rootEl.value;
|
||||
if (!el) {
|
||||
return;
|
||||
}
|
||||
const top = el.getBoundingClientRect().top;
|
||||
const height = Math.max(300, window.innerHeight - top - 12);
|
||||
rootStyle.value = { height: `${height}px`, flex: '1 1 100%' };
|
||||
}
|
||||
|
||||
let observer: ResizeObserver | undefined;
|
||||
onMounted(() => {
|
||||
fit();
|
||||
window.addEventListener('resize', fit);
|
||||
observer = new ResizeObserver(() => fit());
|
||||
if (rootEl.value) {
|
||||
observer.observe(rootEl.value);
|
||||
}
|
||||
translateText();
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
window.removeEventListener('resize', fit);
|
||||
observer?.disconnect();
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div ref="rootEl" class="tr-root" :style="rootStyle">
|
||||
<div class="toolbar">
|
||||
<div class="stats">
|
||||
<template v-if="detected && from === 'auto'">
|
||||
检测到语言:{{ langName(detected) || detected }}
|
||||
</template>
|
||||
</div>
|
||||
<c-button type="primary" :disabled="loading" @click="translateText">
|
||||
翻译
|
||||
</c-button>
|
||||
<c-button :disabled="!result" @click="copyResult">
|
||||
复制译文
|
||||
</c-button>
|
||||
<c-button @click="clearAll">
|
||||
清空
|
||||
</c-button>
|
||||
</div>
|
||||
|
||||
<div class="body">
|
||||
<div class="pane">
|
||||
<div class="lang-row">
|
||||
<c-select v-model:value="from" :options="langs.map(l => ({ ...l }))" w-140px />
|
||||
<c-button size="small" @click="swap">
|
||||
⇄ 交换
|
||||
</c-button>
|
||||
<c-select v-model:value="to" :options="langs.filter(l => l.value !== 'auto').map(l => ({ ...l }))" w-140px />
|
||||
</div>
|
||||
<n-input
|
||||
v-model:value="source"
|
||||
type="textarea"
|
||||
:rows="10"
|
||||
placeholder="输入要翻译的文本..."
|
||||
:autosize="{ minRows: 6, maxRows: 16 }"
|
||||
/>
|
||||
<div class="hint">
|
||||
共 {{ source.length }} 字符,修改后自动翻译
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="pane">
|
||||
<div class="lang-row">
|
||||
<span class="target-label">译文({{ langName(to) }})</span>
|
||||
</div>
|
||||
<n-spin :show="loading" class="output-wrap">
|
||||
<div class="output" :class="{ hasError: error }">
|
||||
<template v-if="error">
|
||||
{{ error }}
|
||||
</template>
|
||||
<template v-else-if="result">
|
||||
{{ result }}
|
||||
</template>
|
||||
<template v-else>
|
||||
<span class="placeholder">译文将显示在这里</span>
|
||||
</template>
|
||||
</div>
|
||||
</n-spin>
|
||||
<div class="hint">
|
||||
<template v-if="result">
|
||||
共 {{ result.length }} 字符
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<GenHistory tool="translator" position="bottom" label="翻译记录" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style lang="less" scoped>
|
||||
.tr-root {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
width: 100%;
|
||||
min-height: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.toolbar {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
justify-content: flex-end;
|
||||
gap: 10px;
|
||||
padding-bottom: 12px;
|
||||
flex-shrink: 0;
|
||||
|
||||
.stats {
|
||||
margin-right: auto;
|
||||
font-size: 13px;
|
||||
opacity: 0.65;
|
||||
}
|
||||
}
|
||||
|
||||
.body {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
gap: 16px;
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
|
||||
@media (max-width: 760px) {
|
||||
flex-direction: column;
|
||||
}
|
||||
}
|
||||
|
||||
.pane {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
min-height: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.lang-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
margin-bottom: 10px;
|
||||
flex-shrink: 0;
|
||||
|
||||
.target-label {
|
||||
font-size: 13px;
|
||||
opacity: 0.7;
|
||||
}
|
||||
}
|
||||
|
||||
.hint {
|
||||
font-size: 12px;
|
||||
opacity: 0.55;
|
||||
margin-top: 6px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.output-wrap {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
}
|
||||
|
||||
.output {
|
||||
height: 100%;
|
||||
min-height: 150px;
|
||||
border: 1px solid rgba(128, 128, 128, 0.3);
|
||||
border-radius: 6px;
|
||||
padding: 12px;
|
||||
font-size: 14px;
|
||||
line-height: 1.7;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
overflow-y: auto;
|
||||
box-sizing: border-box;
|
||||
user-select: text;
|
||||
|
||||
&.hasError {
|
||||
color: #d03050;
|
||||
}
|
||||
|
||||
.placeholder {
|
||||
opacity: 0.4;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in new issue
Block a user