diff --git a/example/translation-demo-nextjs/app/page.tsx b/example/translation-demo-nextjs/app/page.tsx index 8bf3d3b..e06250e 100644 --- a/example/translation-demo-nextjs/app/page.tsx +++ b/example/translation-demo-nextjs/app/page.tsx @@ -1,7 +1,21 @@ +"use client"; import Link from "next/link"; import Image from "next/image"; +import { Button } from "@/components/ui/button"; +import { Github, Copy, Check } from "lucide-react"; +import { useState } from "react"; export default function Home() { + const [copied, setCopied] = useState(false); + + const handleCopyCommand = () => { + navigator.clipboard.writeText("npm i translation-widget"); + setCopied(true); + setTimeout(() => { + setCopied(false); + }, 2000); + }; + return (
{/* Spiral Arrow - top right */} @@ -19,6 +33,36 @@ export default function Home() {

Jigsaw's translation widget enables your website to reach a global audience effortlessly. Explore our live demos below to see how Jigsaw can integrate with blogs, e-commerce, and more—delivering instant, high-quality translations and a world-class user experience.

+
+ + +
+ npm i translation-widget + +
+
+
; - private translationService: TranslationService; + private translationService?: TranslationService; private currentLanguage: string; private widget: HTMLDivElement; - private elements: WidgetElements; + private elements : WidgetElements; private autoDetectLanguage: boolean; private isTranslated: boolean = false; private userLanguage: string; @@ -47,11 +47,6 @@ export class TranslationWidget { // Validate public api key const apiValidationResult = validatePublicApiKey(publicKey); - if (!apiValidationResult.isValid) { - throw new Error(apiValidationResult.message); - } - - this.translationService = new TranslationService(publicKey); this.autoDetectLanguage = this.config.autoDetectLanguage || false; this.currentLanguage = this.config.pageLanguage; this.userLanguage = getUserLanguage(); @@ -65,6 +60,13 @@ export class TranslationWidget { languageItems: null, loadingIndicator: null, }; + if (!apiValidationResult.isValid) { + console.error("Error initializing TranslationWidget: ", apiValidationResult.message); + // return and stop the execution of the constructor + return; + } + this.translationService = new TranslationService(publicKey); + this.initialize(); TranslationWidget.instance = this; } @@ -564,13 +566,13 @@ export class TranslationWidget { }); if (allBatchTexts.length > 0) { - const allTranslatedTexts = await Promise.all(allBatchTexts.map((texts) => this.translationService.translateBatchText(texts, targetLang))); + const allTranslatedTexts = await Promise.all(allBatchTexts.map((texts) => this.translationService?.translateBatchText(texts, targetLang))); // Filter out failed batches and create a mapping of successful translations const successfulBatches: Array<{ translations: string[]; nodes: { element: HTMLElement; text: string }[] }> = []; allTranslatedTexts.forEach((translations, batchIndex) => { - if (translations !== null && translations.length > 0) { + if (translations && translations.length > 0) { successfulBatches.push({ translations, nodes: allBatchNodes[batchIndex]