Types
TranslationResult
Typdefinition für Übersetzungsergebnisse, die von translate()-Methoden zurückgegeben werden
Übersicht
TranslationResult steht für das Ergebnis von Übersetzungsvorgängen.
type TranslationResult = RequestSuccess | TranslationError;Uniontypen
RequestSuccess
type RequestSuccess = TypedResult & {
locale: string;
reference: TranslationResultReference;
};Übersetzungsfehler
type TranslationError = {
error: string;
code: number;
reference?: TranslationResultReference;
};Beispiele
Grundlegende Fehlerbehandlung
import { GT, TranslationResult } from 'generaltranslation';
const gt = new GT({ apiKey: 'your-api-key' });
const result: TranslationResult = await gt.translate('Hallo');
if ('error' in result) {
console.error(`Übersetzung fehlgeschlagen: ${result.error}`);
} else {
console.log(`Übersetzung: ${result.translation}`);
}Type Guards
function isTranslationError(result: TranslationResult): result is TranslationError {
return 'error' in result;
}
if (isTranslationError(result)) {
// Fehler behandeln
} else {
// Erfolgsfall behandeln
}Verwandte Typen
TranslateManyResult- Ergebnisse von Batchübersetzungen
Wie ist diese Anleitung?