A runtime library that supports the code generated by monolith_localization at runtime. It dynamically loads localization resources managed on a module basis in Flutter applications and provides type-safe access.

Features

  • Dynamic Locale Switching: Support for language changes during application runtime
  • Memory Efficiency: Load only the required language resources
  • Flutter Integration: Transparent integration with standard internationalization features
  • Type-safe Access: Runtime type checking through generated Mixin classes

Getting started

This package works in conjunction with code generated by monolith_localization. Developers typically do not directly manipulate this package.

# pubspec.yaml
dependencies:
  monolith_localization_runtime: ^1.0.0

Usage

Integration example with generated code:

// packages/domain/user/lib/gen/strings.dart
import 'package:monolith_localization_runtime/monolith_localization_runtime.dart';

final class _Strings with L10nStringsMixin {
  // Automatic implementation by runtime
}

@internal
final strings = _Strings();

class UserValidator {
  String? validateEmail(String? email) {
    if (email == null || email.isEmpty) {
      return strings.validation_email_required;
    }
    
    if (!_isValidEmail(email)) {
      return strings.validation_email_invalid;
    }
    
    return null;
  }
}

Application integration:

// app/lib/main.dart
import 'package:flutter/material.dart';
import 'package:monolith_localization_runtime/monolith_localization_runtime.dart';

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: L10nHelper.localizationsDelegates,
      supportedLocales: L10nHelper.supportedLocales,
      home: HomePage(),
    );
  }
}

Dynamic language switching:

class LanguageSettings extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        ElevatedButton(
          onPressed: () => L10nHelper.setLocale(context, Locale('ja')),
          child: Text('日本語'),
        ),
        ElevatedButton(
          onPressed: () => L10nHelper.setLocale(context, Locale('en')),
          child: Text('English'),
        ),
      ],
    );
  }
}

Additional information

This library ensures that module-based localization code generated by monolith_localization operates efficiently in actual Flutter applications.