HasTranslations mixin

Mixin that adds multi-language translation support to models

Store and retrieve translated content for multiple locales. Common use case: multi-language CMS, product descriptions, etc.

Example:

class Product extends KhademModel<Product> with HasTranslations {
  @override
  List<String> get translatableFields => ['name', 'description'];

  @override
  String get defaultLocale => 'en';
}

// Set translations:
final product = Product();
product.setTranslation('name', 'en', 'Product Name');
product.setTranslation('name', 'ar', 'اسم المنتج');
product.setTranslation('description', 'en', 'A great product');

// Get translations:
print(product.getTranslation('name', 'ar')); // "اسم المنتج"
print(product.getTranslation('name', 'fr')); // null
print(product.getTranslationWithFallback('name', 'fr')); // "Product Name" (falls back to default)

// Get all translations for a locale:
final arTranslations = product.getAllForLocale('ar');
print(arTranslations); // {'name': 'اسم المنتج'}

// Store in JSON column:
final json = product.toJsonTranslations();
// Save to database: UPDATE products SET translations = json

Properties

defaultLocale String
Override to specify the default/fallback locale
no setter
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
translatableFields List<String>
Override to specify which fields are translatable
no setter
translations Map<String, Map<String, String>>
Storage for all translations Structure: { 'locale': { 'field': 'value' } }
getter/setter pair

Methods

clearTranslations() → void
Clear all translations
getAllForLocale(String locale) Map<String, String>
Get all translations for a specific locale
getAvailableLocales() List<String>
Get all available locales
getMissingTranslations(String locale) List<String>
Get missing translation fields for a locale
getTranslation(String field, String locale) String?
Get translation for a specific field and locale
getTranslationCount(String locale) int
Get translation count for a locale
getTranslationWithFallback(String field, String locale) String?
Get translation with fallback to default locale
hasLocale(String locale) bool
Check if any translations exist for a locale
hasTranslation(String field, String locale) bool
Check if a translation exists for a field in a locale
isTranslationComplete(String locale) bool
Check if translations are complete for a locale
loadTranslations(Map<String, dynamic> raw) → void
Load translations from JSON/database
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
removeLocale(String locale) → void
Remove all translations for a locale
removeTranslation(String field, String locale) → void
Remove a specific field translation from a locale
setTranslation(String field, String locale, String value) → void
Set translation for a specific field and locale
setTranslationsForLocale(String locale, Map<String, String> values) → void
Set multiple translations for a locale at once
toJsonTranslations() Map<String, dynamic>
Convert translations to JSON for database storage
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited