feat(translator): preprocess some common mistranslated publishing terms (#1964)
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { useState, useCallback, useEffect } from 'react';
|
||||
import { useAuth } from '@/context/AuthContext';
|
||||
import { ErrorCodes, getTranslator, getTranslators, TranslatorName } from '@/services/translators';
|
||||
import { getFromCache, storeInCache, polish, UseTranslatorOptions } from '@/services/translators';
|
||||
import { getFromCache, storeInCache, UseTranslatorOptions } from '@/services/translators';
|
||||
import { polish, preprocess } from '@/services/translators';
|
||||
import { eventDispatcher } from '@/utils/event';
|
||||
import { useTranslation } from './useTranslation';
|
||||
|
||||
@@ -10,6 +11,7 @@ export function useTranslator({
|
||||
sourceLang = 'AUTO',
|
||||
targetLang = 'EN',
|
||||
enablePolishing = true,
|
||||
enablePreprocessing = true,
|
||||
}: UseTranslatorOptions = {}) {
|
||||
const _ = useTranslation();
|
||||
const { token } = useAuth();
|
||||
@@ -42,7 +44,7 @@ export function useTranslator({
|
||||
const sourceLanguage = options?.source || sourceLang;
|
||||
const targetLanguage = options?.target || targetLang;
|
||||
const useCache = options?.useCache ?? false;
|
||||
const textsToTranslate = input;
|
||||
const textsToTranslate = enablePreprocessing ? preprocess(input) : input;
|
||||
|
||||
if (textsToTranslate.length === 0 || textsToTranslate.every((t) => !t?.trim())) {
|
||||
return textsToTranslate;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
export * from './types';
|
||||
export * from './cache';
|
||||
export * from './polish';
|
||||
export * from './preprocess';
|
||||
export * from './providers';
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
export type Preprocessor = (text: string) => string;
|
||||
export type TextSubstitutions = Record<string, string>;
|
||||
|
||||
const defaultSubstitutions: TextSubstitutions = {
|
||||
Cover: 'The Cover',
|
||||
Dedication: 'Dedication Page',
|
||||
Acknowledgements: 'The Acknowledgements',
|
||||
};
|
||||
|
||||
export const basicSubstitute = (text: string, substitutions: TextSubstitutions): string => {
|
||||
const allSubstitutions = { ...defaultSubstitutions, ...substitutions };
|
||||
|
||||
if (Object.keys(allSubstitutions).length === 0) {
|
||||
return text;
|
||||
}
|
||||
|
||||
if (allSubstitutions[text]) {
|
||||
return allSubstitutions[text];
|
||||
}
|
||||
|
||||
return text;
|
||||
};
|
||||
|
||||
export function createPreprocessor(substitutions: TextSubstitutions = {}): Preprocessor {
|
||||
return (text: string) => basicSubstitute(text, substitutions);
|
||||
}
|
||||
|
||||
export function preprocess(texts: string[], substitutions: TextSubstitutions = {}): string[] {
|
||||
const preprocessor = createPreprocessor(substitutions);
|
||||
return texts.map(preprocessor);
|
||||
}
|
||||
@@ -23,6 +23,7 @@ export interface UseTranslatorOptions {
|
||||
sourceLang?: string;
|
||||
targetLang?: string;
|
||||
enablePolishing?: boolean;
|
||||
enablePreprocessing?: boolean;
|
||||
}
|
||||
|
||||
export const ErrorCodes = {
|
||||
|
||||
@@ -30,7 +30,11 @@ export const walkTextNodes = (root: HTMLElement, rejectTags: string[] = []): HTM
|
||||
if (node.nodeType === Node.TEXT_NODE && node.textContent?.trim()) {
|
||||
return true;
|
||||
}
|
||||
if (node.nodeType === Node.ELEMENT_NODE && (node as HTMLElement).tagName === 'SPAN') {
|
||||
if (
|
||||
node.nodeType === Node.ELEMENT_NODE &&
|
||||
(node as HTMLElement).tagName === 'SPAN' &&
|
||||
node.textContent?.trim()
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user