Solvro Translator Core

A lightweight, efficient translation framework for Dart and Flutter applications that provides a solid foundation for text localization with support for both local caching and remote translation services.

Features

  • Dual-layer translation system with local cache and remote service integration
  • Locale management for handling translation between different languages
  • Efficient caching to minimize network requests and improve performance
  • Extensible architecture with clear interfaces for custom implementations
  • Type-safe translation results through generic implementation
  • Validity checking to ensure translations remain current

Custom Usage

Implement the required managers and translation result classes:

  1. Create a concrete implementation of RemoteTranslatableManager for your translation service
  2. Create a concrete implementation of LocalTranslatableManager for your local storage solution
  3. Define custom TranslationResults classes for both local and remote translations

Usage

import "package:solvro_translator_core/solvro_translator_core.dart";

class MyLocalTranslation implements TranslationResults {
  @override
  final String translatedText;
  final DateTime timestamp;

  const MyLocalTranslation(this.translatedText, this.timestamp);
}

class MyRemoteTranslation implements TranslationResults {
  @override
  final String translatedText;

  const MyRemoteTranslation(this.translatedText);
}

// Implement local manager
class MyLocalManager implements LocalTranslatableManager<MyLocalTranslation, MyRemoteTranslation> {
  @override
  Future<MyLocalTranslation?> getTranslation(String md5Hash, SolvroLocale locale) async {
    // Mock implementation - in real usage, fetch from local storage
    return MyLocalTranslation("Cached: $hash", DateTime.now());
  }

  @override
  Future<void> saveTranslation(MyRemoteTranslation translation) async {
    // Mock implementation - in real usage, save to local storage
  }

  @override
  Future<void> deleteOldTranslations(Duration duration) async {
    // Mock implementation - in real usage, delete old translations from storage
  }
}

// Implement remote manager
class MyRemoteManager implements RemoteTranslatableManager<MyRemoteTranslation> {
  @override
  Future<MyRemoteTranslation> translate(String text, SolvroLocale from, SolvroLocale to) async {
    // Mock implementation - in real usage, call translation API
    return MyRemoteTranslation("Remote: $text");
  }
}

// Create a translator instance
final translator = SolvroTranslator<MyLocalTranslation, MyRemoteTranslation>.init(
  localTranslatableManager: MyLocalManager(),
  remoteTranslatableManager: MyRemoteManager(),
  validityCheck: (translation) => DateTime.now().difference(translation.timestamp).inDays < 30,
  sourceLocale: SolvroLocale.pl,
);

final translated = await translator.translate("Hello world", SolvroLocale.en);

Additional information

To contribute to this package or report issues, please visit the GitHub repository.

This package is maintained by Solvro.