tr method

String tr({
  1. BuildContext? context,
  2. Map<String, dynamic>? args,
})

Implementation

String tr({BuildContext? context, Map<String, dynamic>? args}) {
  // Try to use the provided context
  if (context != null) {
    final localizations = AppLocalizations.of(context);
    if (localizations != null) {
      return localizations.translate(this, args: args);
    }
  }

  // Try to use the global context from Localingo
  final globalContext = Localingo.navigatorKey.currentContext;
  if (globalContext != null) {
    final localizations = AppLocalizations.of(globalContext);
    if (localizations != null) {
      return localizations.translate(this, args: args);
    }
  }

  // Fallback: return the key itself if no context is available
  debugPrint(
      'WARNING: No context available for translation of key "$this". Returning key.');
  return this;
}