fix: translate HiCAD input and spaced copy

This commit is contained in:
134 2026-07-13 17:07:18 +08:00
parent f29433a91b
commit 00cbfce56d

View File

@ -119,6 +119,17 @@ const excludedSelector = [
'.cm-content',
'.cm-line',
].join(',')
const excludedAttributeSelector = [
'script',
'style',
'.cm-editor',
'.cm-content',
'.cm-line',
].join(',')
function normalizeLookupKey(value: string) {
return value.replace(/\s+/g, ' ').trim()
}
function isExcluded(node: Node) {
const element =
@ -131,7 +142,7 @@ function isExcluded(node: Node) {
function translateTextNode(node: Text) {
if (isExcluded(node)) return
const value = node.nodeValue || ''
const normalized = value.trim()
const normalized = normalizeLookupKey(value)
const translated = translations.get(normalized)
if (!translated || translated === normalized) return
@ -141,9 +152,9 @@ function translateTextNode(node: Text) {
}
function translateElement(element: Element) {
if (isExcluded(element)) return
if (element.closest(excludedAttributeSelector)) return
for (const attribute of translatableAttributes) {
const value = element.getAttribute(attribute)?.trim()
const value = normalizeLookupKey(element.getAttribute(attribute) || '')
if (!value) continue
const translated = translations.get(value)
if (translated) element.setAttribute(attribute, translated)