AI-powered automatic translation library for React applications with intelligent caching and zero-maintenance workflow.
Chrome's translator is great for casual browsing, but your library provides enterprise-grade i18n that browser translation can't match:
- Chrome: Translates everything inconsistently, breaks layouts, translates technical terms/IDs
- Your Lib: Only translates what you specify, preserves UI structure, skips technical content
- Chrome: Free but lower quality, inconsistent across browsers (Safari/Firefox differ)
- Your Lib: Professional APIs (DeepL, Google Cloud) with superior accuracy and context
- Chrome: Client-side only, search engines see original language
- Your Lib: Pre-rendered translations for SEO, proper
langattributes for screen readers
- Chrome: User must manually click "Translate"
- Your Lib: Automatic language detection, saved preferences, built-in UI selector
- Chrome: Can mistranslate brand names, legal terms, product names
- Your Lib: Whitelist/blacklist terms, custom translations for critical content
Chrome: Visit site → See English → Click translate → Page reloads → Broken layout
Your Lib: Visit site → Auto-detect Hindi preference → Show Hindi → Perfect layout
- Chrome: Just translates text
- Your Lib: Locale-aware dates/numbers, currency conversion, RTL layouts, locale sorting
Chrome Translator:
- "Add to Cart" → ✓ Translated
- "$99.99" → ✗ Stays in dollars
- "SKU: XYZ-123" → ✗ Might translate SKU
- Layout breaks, forms confused
Your Library:
- "Add to Cart" → ✓ Translated
- "$99.99" → ✓ Converted to ₹8,299
- "SKU: XYZ-123" → ✓ Unchanged
- Perfect layout, proper locale formatting
Target Users: SaaS platforms, e-commerce, dashboards, enterprise apps needing proper multi-language support with full developer control.
- Single API Call at Startup - All translations fetched once and cached
- Auto-Extract Translatable Text - Build-time plugin scans your code automatically
- Smart Caching - LocalStorage/IndexedDB persistence across sessions
- Zero Maintenance - No manual translation files to manage
- Multiple Translation Providers - Support for Google Translate, DeepL, AWS Translate
- Offline-Ready - Works offline after initial translation load
- Dynamic Language Switching - Change languages on the fly
- Type-Safe - Full TypeScript support
- 90% Cost Reduction - Single batch call vs. per-text translation
- Professional Quality - Enterprise-grade accuracy and context awareness
npm install @polyglot/react
# or
yarn add @polyglot/react// vite.config.js
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { extractTranslatableText } from "@polyglot/react/plugin";
export default defineConfig({
plugins: [
react(),
extractTranslatableText({
include: ["src/**/*.{jsx,tsx}"],
output: "src/translations/texts.json",
exclude: ["src/**/*.test.{jsx,tsx}"], // Optional
}),
],
});// src/App.jsx
import { TranslateProvider, createTranslator } from "@polyglot/react";
import textsToTranslate from "./translations/texts.json";
const translator = createTranslator({
// Source language (your app's default language)
sourceLang: "en",
// Target language for translation
targetLang: "hi", // or 'es', 'fr', 'de', etc.
// Translation provider configuration
provider: "google", // 'google', 'deepl', or 'aws'
apiKey: import.meta.env.VITE_TRANSLATE_API_KEY,
// Texts extracted by build plugin
textToTranslate,
// Cache settings
cache: {
enabled: true,
storage: "localStorage", // 'localStorage' or 'indexedDB'
ttl: 7 * 24 * 60 * 60 * 1000, // 7 days in milliseconds
},
// Optional: Fallback behavior
fallbackToOriginal: true,
});
function App() {
return (
<TranslateProvider translator={translator}>
<YourApp />
</TranslateProvider>
);
}
export default App;import { useTranslate } from "@polyglot/react";
function Header() {
const t = useTranslate();
return (
<header>
<h1>{t("Welcome to Our App!")}</h1>
<button>{t("Get Started")}</button>
</header>
);
}- Vite plugin scans all
.jsx/.tsxfiles - Extracts text content from JSX elements
- Generates
texts.jsonwith all translatable strings
Example Generated File:
["Welcome to Our App!", "Get Started", "Login", "Sign Up", "Contact Us"]- Library reads
texts.json - Makes single batch API call to translation service
- Caches all translations in browser storage
- Serves translations instantly from cache
- Components call
t('original text') - Library looks up translation in cache
- Returns translated text or falls back to original
import { useTranslator } from "@polyglot/react";
function LanguageSwitcher() {
const { changeLanguage, currentLang, isLoading } = useTranslator();
const handleLanguageChange = async (lang) => {
await changeLanguage(lang);
};
return (
<select
value={currentLang}
onChange={(e) => handleLanguageChange(e.target.value)}
disabled={isLoading}
>
<option value="en">English</option>
<option value="hi">हिंदी</option>
<option value="es">Español</option>
<option value="fr">Français</option>
</select>
);
}Simple, powerful, and purpose-built for this library:
const translator = createTranslator({
provider: "polyglot",
apiKey: "YOUR_POLYGLOT_API_KEY", // Get from https://usepolyglot.dev
// ... other config
});Benefits:
- ✅ Built-in caching and monitoring
- ✅ Simple pricing and billing
- ✅ No complex cloud console setup
- ✅ Free tier available
const translator = createTranslator({
provider: "google",
apiKey: "YOUR_GOOGLE_API_KEY",
// ... other config
});const translator = createTranslator({
provider: "deepl",
apiKey: "YOUR_DEEPL_API_KEY",
// ... other config
});const translator = createTranslator({
provider: "aws",
credentials: {
accessKeyId: "YOUR_ACCESS_KEY",
secretAccessKey: "YOUR_SECRET_KEY",
region: "us-east-1",
},
// ... other config
});const translator = createTranslator({
provider: "custom",
customAPIOptions: {
baseUrl: "http://localhost:8080",
headers: {
"X-API-Key": "your_api_key",
},
},
// ... other config
});- Fast and simple
- ~5-10MB limit
- Best for smaller apps
cache: {
storage: 'localStorage',
ttl: 7 * 24 * 60 * 60 * 1000 // 7 days
}- Larger storage capacity
- Better for extensive translations
- Asynchronous operations
cache: {
storage: 'indexedDB',
ttl: 30 * 24 * 60 * 60 * 1000 // 30 days
}For user-generated or API content that isn't in your source code:
import { useTranslateDynamic } from "@polyglot/react";
function UserComment({ comment }) {
const translateDynamic = useTranslateDynamic();
const [translated, setTranslated] = useState(comment);
useEffect(() => {
translateDynamic(comment).then(setTranslated);
}, [comment]);
return <p>{translated}</p>;
}function Welcome({ username }) {
const t = useTranslate();
return <h1>{t("Welcome, {{name}}!", { name: username })}</h1>;
}function ItemCount({ count }) {
const t = useTranslate();
return <p>{t("{{count}} item", { count, plural: true })}</p>;
// Output: "1 item" or "5 items" (auto-translated with correct plural form)
}- Initial Load: ~500ms for batch translation (one-time)
- Subsequent Loads: <5ms (served from cache)
- Cache Size: Typically 50-200KB for average apps
- API Costs: Single batch call vs. per-text calls = 90% cost reduction
# .env
VITE_TRANSLATE_API_KEY=your_api_key_here
VITE_TRANSLATE_PROVIDER=google
VITE_DEFAULT_LANGUAGE=enMock translations in tests:
import { MockTranslateProvider } from "@polyglot/react/testing";
test("renders welcome message", () => {
render(
<MockTranslateProvider translations={{ Welcome: "स्वागत है" }}>
<Header />
</MockTranslateProvider>
);
expect(screen.getByText("स्वागत है")).toBeInTheDocument();
});extractTranslatableText({
// Files to scan
include: ["src/**/*.{jsx,tsx}"],
// Files to ignore
exclude: ["**/*.test.{jsx,tsx}", "**/node_modules/**"],
// Output file path
output: "src/translations/texts.json",
// Extract from specific JSX patterns
patterns: {
jsxText: true, // <div>Text here</div>
jsxAttribute: ["title", "placeholder", "aria-label"], // <input placeholder="..." />
stringLiterals: false, // const text = "Hello" (opt-in)
},
// Minimum text length to extract
minLength: 2,
// Ignore texts matching regex
ignore: [/^[0-9]+$/, /^[A-Z_]+$/], // Numbers, constants
// Debug mode
verbose: true,
});Creates translator instance with configuration.
Hook to access translation function in components.
Hook to access translator instance and control methods.
Hook for translating dynamic runtime content.
Context provider component for app-wide translations.
Contributions welcome! Please read our Contributing Guide.
MIT © 2025
- 📧 Email: support@@polyglot/react.com
- 💬 Discord: Join our community
- 🐛 Issues: GitHub Issues
- Support for more translation providers (Microsoft, Yandex)
- CLI tool for managing translations
- React Native support
- Webpack plugin support
- Translation quality feedback system
- Automatic language detection
- Server-side rendering (SSR) support
Made with ❤️ for developers who hate maintaining translation files