jaspr_localizations library

Jaspr Localizations - Internationalization and localization for Jaspr applications.

A comprehensive localization solution for Jaspr applications that provides locale management, automatic UI rebuilding on locale changes, and ARB file processing for translations.

Features

  • Locale Management: Built-in locale switching with JasprLocalizations
  • Platform Detection: Automatic language detection from browser or system
  • ARB Processing: Flutter-aligned ARB file processing with code generation
  • Reactive Updates: Automatic UI rebuilding when locale changes
  • Type Safety: Generated classes provide compile-time safety for translations

Quick Start

1. Add ARB Files

Create ARB (Application Resource Bundle) files in lib/l10n/:

lib/l10n/
  ├── app_en.arb  (template - required)
  ├── app_es.arb
  ├── app_fr.arb
  └── app_de.arb

Example app_en.arb:

{
  "@@locale": "en",
  "welcomeMessage": "Welcome to our app!",
  "greeting": "Hello, {name}!",
  "@greeting": {
    "placeholders": {
      "name": {
        "type": "String"
      }
    }
  }
}

2. Configure l10n.yaml

Create l10n.yaml in your project root:

arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: l10n.dart
output-class: AppLocalizations

3. Wrap Your App

Use JasprLocalizations to add localization to your app:

import 'package:jaspr/jaspr.dart';
import 'package:jaspr_localizations/jaspr_localizations.dart';
import 'package:example/generated/l10n.dart';

@client
class App extends StatelessComponent {
  @override
  Component build(BuildContext context) {
    return JasprLocalizations(
      supportedLocales: [
        Locale('en', 'US'),
        Locale('es', 'ES'),
        Locale('fr', 'FR'),
      ],
      delegates: [AppLocalizations.delegate],
      builder: (context, locale) {
        return MyHomePage();
      },
    );
  }
}

4. Access Localized Strings

class MyComponent extends StatelessComponent {
  @override
  Component build(BuildContext context) {
    final l10n = AppLocalizations.of(context);

    return div([
      text(l10n.welcomeMessage),
      text(l10n.greeting('Alice')),
    ]);
  }
}

5. Change Locale at Runtime

// Switch to Spanish
JasprLocalizationProvider.setLanguage(context, 'es', 'ES');

// Or use Locale object
JasprLocalizationProvider.setLocale(context, Locale('fr', 'FR'));

Advanced Usage

Platform-Aware Language Detection

Detect the user's preferred language from browser or system:

import 'package:jaspr_localizations/jaspr_localizations.dart';

void main() {
  // Get current locale from browser/platform
  final locale = getCurrentLocale();

  runApp(
    JasprLocalizations(
      supportedLocales: supportedLocales,
      initialLocale: locale,  // Use detected locale
      delegates: [AppLocalizations.delegate],
      builder: (context, locale) => MyApp(),
    ),
  );
}

Scoped Locale Override

Display specific content in a different locale:

JasprLocalizations.withLocale(
  locale: Locale('es', 'ES'),
  child: PreviewWidget(),
)

Listen to Locale Changes

class LocaleListener extends StatefulComponent {
  @override
  State createState() => _LocaleListenerState();
}

class _LocaleListenerState extends State<LocaleListener> {
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    final locale = JasprLocalizationProvider.of(context).currentLocale;
    print('Locale changed to: ${locale.toLanguageTag()}');
  }

  @override
  Component build(BuildContext context) {
    return div([text('Locale: ${JasprLocalizationProvider.of(context).currentLocale}')]);
  }
}

Core Components

See Also

Classes

JasprLocalizationProvider
An inherited component that supplies locale information to the widget tree.
JasprLocalizations
A widget that manages locale state and rebuilds the UI when the locale changes.
Locale
Identifies a specific language and region.
LocaleChangeNotifier
Manages the current locale and notifies listeners of changes.
LocalizationsDelegate<T>
A delegate for loading localized resources.

Functions

getCurrentLanguageCode() → String
Gets the current language code from the browser or platform.
getCurrentLocale() → Locale
Gets the current locale from the browser or platform.
languageCodeToLocale(String languageCode) → Locale
Converts a language code string to a Locale object.