smart_translate_text

A Flutter package that automatically translates your text widgets based on a single, globally selected language.

It wraps common text widgets like Text, AutoSizeText, animated text, and RichText so that you can localize dynamic content without maintaining manual string maps for every language.

Features

  • Live language switching using a global LanguageService
  • In-memory translation cache to avoid repeated network calls
  • Uses translator (GoogleTranslator)
  • Drop-in widgets for:
    • Text
    • AutoSizeText
    • Typewriter-style animated text
    • RichText (per-span translation)
  • Optional shimmer loading placeholders while translations are fetched

Installation

Add the package to your pubspec.yaml:

dependencies:
  smart_translate_text: ^0.0.2

Then run:

flutter pub get

Quick start

import 'package:smart_translate_text/smart_translate_text.dart';

void main() {
  // Set default app language once at startup (e.g. from saved settings)
  LanguageService.setDefault('en'); // English

  runApp(const MyApp());
}

// Somewhere in your widget tree
Column(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: const [
    LangText('Hello, how are you?'),
    LangAutoSizeText('This text is auto-resized and translated.'),
    LangAnimatedText('Typewriter style translated text'),
  ],
);

Changing the language anywhere in the app will update all registered widgets:

// For example after user picks a language from settings
LanguageService.change('fr'); // Switch to French globally

Widgets

LangText

Drop-in replacement for Text that translates the provided String using the current language from LanguageService.

  • Shows optional shimmer placeholder while loading
  • Respects style, maxLines, overflow, textAlign, softWrap, etc.
const LangText(
  'Welcome to our app!',
  style: TextStyle(fontSize: 18),
);

LangAutoSizeText

Wrapper around AutoSizeText that keeps all sizing behaviour while automatically translating the text.

const LangAutoSizeText(
  'Responsive and translated headline',
  maxLines: 2,
  minFontSize: 12,
);

LangAnimatedText

Typewriter-style animated text that first translates the source string and then animates it.

const LangAnimatedText(
  'Typing translated message',
  speed: Duration(milliseconds: 40),
);

LangRichText

Translates each TextSpan.text individually inside a RichText widget while preserving styling and nested spans.

LangRichText(
  textSpans: const [
    TextSpan(text: 'Hello '),
    TextSpan(text: 'world', style: TextStyle(fontWeight: FontWeight.bold)),
    TextSpan(text: '!'),
  ],
);

Language management

The package exposes a simple global service:

class LanguageService {
  static final ValueNotifier<String> selectedLanguage;

  static void setDefault(String langCode);
  static void change(String langCode);
  static void ensureInitialized();
  static String get current;
}
  • Call LanguageService.setDefault('en') or any other code on app startup.
  • Call LanguageService.change(code) when the user changes language in your settings UI.
  • Widgets automatically listen to selectedLanguage and re-translate when it changes.

Custom translation engine

By default the package uses GoogleTranslator from the translator package. You can plug in your own translation backend by assigning TranslationService.customEngine:

import 'package:auto_translate_text/auto_translate_text.dart';

Future<String> myCustomEngine(String text, String targetLang) async {
  // Call your own API here and return the translated text
  return text; // Fallback behaviour for this example
}

void main() {
  TranslationService.customEngine = myCustomEngine;
  runApp(const MyApp());
}

Notes & limitations

  • This package relies on network-based translation, so latency and availability depend on the underlying translation service.
  • For performance, results are cached per language in memory. Clear the cache when needed:
TranslationService.clearCache();

License

This project is licensed under the MIT License – see the LICENSE file for details.

Libraries

auto_translate_text
A Flutter package that auto-translates Text, AutoSizeText, AnimatedText and RichText widgets.
smart_translate_text
A Flutter package that auto-translates Text, AutoSizeText, AnimatedText and RichText widgets.