LocalUtils extension

Extension that adds localization capabilities to String objects.

This extension provides a convenient way to translate strings throughout the SDK using the built-in localization system. It supports both context-based translation and custom global translators for flexible localization scenarios.

Features

  • Context-Based Translation: Uses Flutter's BuildContext for automatic locale detection
  • Global Translator Support: Allows custom translation functions for special cases
  • Fallback Handling: Returns the original string if translation fails
  • Easy Integration: Simple method call on any string object
  • Flexible Usage: Works with both local and global translation systems

Usage Example

// Basic usage with context
Text('hello_world'.translate(context))

// With custom global translator
Text('welcome_message'.translate(
  context,
  globalTranslator: (key) => customTranslate(key),
))

// In widget build methods
@override
Widget build(BuildContext context) {
  return Column(
    children: [
      Text('payment_title'.translate(context)),
      Text('payment_description'.translate(context)),
      ElevatedButton(
        onPressed: () {},
        child: Text('pay_button'.translate(context)),
      ),
    ],
  );
}

Translation Priority

The extension follows this priority order for translation:

  1. Global Translator: If provided, uses the custom translation function
  2. Context-Based: Falls back to AppLocalizations.of(context)
  3. Original String: If no translation is available, returns the original string

Supported Locales

The SDK supports the following locales:

  • English: Default language with left-to-right text direction
  • Arabic: Right-to-left language support for Arabic-speaking regions

Best Practices

When using this extension:

  • Always provide meaningful translation keys (e.g., 'payment_success' not 'msg1')
  • Use consistent naming conventions for translation keys
  • Test translations in both supported locales
  • Provide fallback text for critical user-facing strings
  • Consider using global translators for dynamic content
on

Methods

translate(BuildContext context, {String globalTranslator(String p1)?}) String

Available on String, provided by the LocalUtils extension

Translates the string using the provided context or global translator.