refactor: Remove unused DB translation system

Static dictionary (i18n.ts CAR_TRANSLATIONS) already covers all terms.
DB translations table had only 179 entries used as fallback and was
never actually reached. Simplifies useTranslate hook to static-only.
DB table preserved for safety.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
AutonetSellCar Deploy
2026-02-18 23:24:38 +09:00
parent 46973c8508
commit 3f27297c4a
10 changed files with 10 additions and 2140 deletions

View File

@@ -1,74 +1,16 @@
import { useState, useEffect, useCallback } from 'react';
import { translationsApi } from './api';
import { useCallback } from 'react';
import { useLanguageStore, translateCarName, Language } from './i18n';
// Cache for translations to avoid repeated API calls
const translationCache: Record<string, Record<string, string>> = {};
export function useTranslate() {
const { language } = useLanguageStore();
const [translations, setTranslations] = useState<Record<string, string>>({});
const [loading, setLoading] = useState(false);
// Get cache key for current language
const cacheKey = `trans_${language}`;
// Load translations from cache on mount
useEffect(() => {
if (translationCache[cacheKey]) {
setTranslations(translationCache[cacheKey]);
}
}, [cacheKey]);
// Translate a single text
// Translate a single text using static dictionary
const translate = useCallback((text: string | undefined | null): string => {
if (!text) return '';
if (language === 'ko') return text; // Korean is source, no translation needed
// Try static translations FIRST (for fuel, transmission, car names, etc.)
const staticTranslation = translateCarName(text, language as Language);
if (staticTranslation !== text) {
return staticTranslation;
}
// Then check API cache for other translations
const cached = translationCache[cacheKey]?.[text];
if (cached) return cached;
return text; // Fallback to original if no translation found
}, [language, cacheKey]);
// Bulk load translations for multiple texts
const loadTranslations = useCallback(async (texts: string[], category?: string) => {
if (language === 'ko') return; // No need to translate Korean
// Filter out already cached texts
const uncachedTexts = texts.filter(
t => t && !translationCache[cacheKey]?.[t]
);
if (uncachedTexts.length === 0) return;
setLoading(true);
try {
// Map language code to API expected format
const langCode = language === 'mn' ? 'mn' : language === 'ru' ? 'ru' : 'en';
const result = await translationsApi.bulkLookup(uncachedTexts, langCode, category);
// Update cache
if (!translationCache[cacheKey]) {
translationCache[cacheKey] = {};
}
Object.assign(translationCache[cacheKey], result.translations);
setTranslations({ ...translationCache[cacheKey] });
} catch (err) {
console.error('Failed to load translations:', err);
} finally {
setLoading(false);
}
}, [language, cacheKey]);
return translateCarName(text, language as Language);
}, [language]);
// Translate car object fields
const translateCar = useCallback((car: {
@@ -89,8 +31,8 @@ export function useTranslate() {
};
}, [translate]);
// Preload translations for a list of cars
const preloadCarTranslations = useCallback(async (cars: Array<{
// Kept for API compatibility - static translations are synchronous, so this is a no-op
const preloadCarTranslations = useCallback(async (_cars: Array<{
car_name?: string;
fuel?: string;
transmission?: string;
@@ -98,37 +40,13 @@ export function useTranslate() {
maker?: { name: string };
model?: { name: string };
}>) => {
const textsToTranslate: string[] = [];
cars.forEach(car => {
if (car.car_name) textsToTranslate.push(car.car_name);
if (car.fuel) textsToTranslate.push(car.fuel);
if (car.transmission) textsToTranslate.push(car.transmission);
if (car.color) textsToTranslate.push(car.color);
if (car.maker?.name) textsToTranslate.push(car.maker.name);
if (car.model?.name) textsToTranslate.push(car.model.name);
});
// Remove duplicates
const uniqueTexts = Array.from(new Set(textsToTranslate));
if (uniqueTexts.length > 0) {
await loadTranslations(uniqueTexts);
}
}, [loadTranslations]);
// No-op: static dictionary translations are synchronous
}, []);
return {
translate,
translateCar,
loadTranslations,
preloadCarTranslations,
loading,
loading: false,
};
}
// Clear translation cache (useful when translations are updated)
export function clearTranslationCache() {
Object.keys(translationCache).forEach(key => {
delete translationCache[key];
});
}