modular_l10n

Pub Version Pub Points Popularity License

Runtime utilities for modular Flutter localization with full RTL support.


✨ Features

Feature Description
🌍 RTL Support Built-in detection for Arabic, Hebrew, Urdu, and other RTL languages
🔄 LocaleProvider Easy runtime locale switching with InheritedWidget
🛠️ Locale Utilities Parse, match, and get display names for locales
📱 Cross-Platform Works on Android, iOS, Web, macOS, Windows, and Linux

📦 Installation

Add to your pubspec.yaml:

dependencies:
  modular_l10n: ^1.0.0

Then run:

flutter pub get

🚀 Quick Start

1. Wrap Your App with LocaleProvider

import 'package:flutter/material.dart';
import 'package:modular_l10n/modular_l10n.dart';
import 'generated/l10n/l10n.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Locale _locale = const Locale('en');

  @override
  Widget build(BuildContext context) {
    return LocaleProvider(
      initialLocale: _locale,
      onLocaleChanged: (locale) {
        setState(() => _locale = locale);
      },
      child: MaterialApp(
        locale: _locale,
        supportedLocales: S.supportedLocales,
        localizationsDelegates: const [
          S.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate,
        ],
        // Handle text direction for RTL languages
        builder: (context, child) {
          return Directionality(
            textDirection: LocaleUtils.getTextDirection(_locale),
            child: child!,
          );
        },
        home: const HomePage(),
      ),
    );
  }
}

2. Change Locale from Anywhere

class LanguageSelector extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return PopupMenuButton<Locale>(
      icon: const Icon(Icons.language),
      onSelected: (locale) {
        // Change locale using context extension
        context.setLocale(locale);
      },
      itemBuilder: (context) => [
        const PopupMenuItem(
          value: Locale('en'),
          child: Text('English'),
        ),
        const PopupMenuItem(
          value: Locale('ar'),
          child: Text('العربية'),
        ),
      ],
    );
  }
}

3. Check Current Locale

// Get current locale
Locale current = context.currentLocale;

// Check if current locale is Arabic
if (context.isLocale('ar')) {
  // Handle Arabic-specific logic
}

🌐 RTL Support

Check if Locale is RTL

import 'package:modular_l10n/modular_l10n.dart';

// Using static method
if (LocaleUtils.isRtl(locale)) {
  // Handle RTL layout
}

// Using extension
if (locale.isRtl) {
  // Handle RTL layout
}

Get Text Direction

// Using static method
TextDirection direction = LocaleUtils.getTextDirection(locale);

// Using extension
TextDirection direction = locale.textDirection;

Supported RTL Languages

The package automatically detects these RTL languages:

Code Language
ar Arabic
fa Persian/Farsi
he Hebrew
ur Urdu
ps Pashto
sd Sindhi
yi Yiddish
ku Kurdish (Sorani)
ug Uyghur
dv Divehi

🛠️ Locale Utilities

Parse Locale String

// Supports multiple formats
Locale locale1 = LocaleUtils.parseLocale('en');      // Locale('en')
Locale locale2 = LocaleUtils.parseLocale('en_US');   // Locale('en', 'US')
Locale locale3 = LocaleUtils.parseLocale('en-US');   // Locale('en', 'US')

Find Best Matching Locale

final supportedLocales = [Locale('en'), Locale('ar'), Locale('de')];
final deviceLocale = Locale('ar', 'EG');

Locale? match = LocaleUtils.findBestMatch(deviceLocale, supportedLocales);
// Returns Locale('ar')

Get Locale Display Names

Locale locale = Locale('ar');

// English name
String displayName = locale.displayName;     // "Arabic"

// Native name
String nativeName = locale.nativeName;       // "العربية"

// Or using static methods
String displayName = LocaleUtils.getDisplayName(locale);
String nativeName = LocaleUtils.getNativeName(locale);

📖 API Reference

LocaleProvider

A widget that provides locale state to the widget tree.

LocaleProvider({
  required Locale initialLocale,
  required Widget child,
  ValueChanged<Locale>? onLocaleChanged,
})

LocaleProviderState

Method Description
currentLocale Get the current locale
setLocale(Locale) Change the current locale
isLocale(String) Check if current locale matches a language code

BuildContext Extensions

Extension Description
context.currentLocale Get current locale from LocaleProvider
context.setLocale(Locale) Set locale via LocaleProvider
context.isLocale(String) Check if current locale matches

LocaleUtils

Method Description
isRtl(Locale) Check if locale is RTL
getTextDirection(Locale) Get TextDirection for locale
parseLocale(String) Parse locale string to Locale
findBestMatch(Locale, List<Locale>) Find best matching locale
getDisplayName(Locale) Get English display name
getNativeName(Locale) Get native display name

Locale Extensions

Extension Description
locale.isRtl Check if locale is RTL
locale.textDirection Get TextDirection
locale.displayName Get English display name
locale.nativeName Get native display name

🔗 VS Code Extension

This package is designed to work with the Modular Flutter Localization VS Code extension.

The extension provides:

  • Automatic code generation from ARB files
  • Watch mode for instant regeneration
  • Commands to add keys and create modules
  • Modular organization by feature

📁 Complete Example

See the example directory for a complete sample application.

🐛 Issues & Contributions

Found a bug or have a feature request?

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.


Made with ❤️ by Utanium

Libraries

modular_l10n
A modular approach to Flutter localization.