easy_json_translate 1.0.0 copy "easy_json_translate: ^1.0.0" to clipboard
easy_json_translate: ^1.0.0 copied to clipboard

A pure Flutter widget-based package dynamically bridging localized JSON files into the app without generating Dart boilerplate.

Easy JSON Translate #

A pure Flutter package designed to quickly inject your application with internationalization using JSON files. Dynamically loads .json files from assets/lang automatically so you don't have to manually maintain large translation dart files, and automatically handles locale persistence without requiring external dependencies like GetX.

Features #

  • Parse translations from JSON easily using EasyJsonTranslate.
  • Manage language selection state easily with ChangeNotifier.
  • Automatic language persistency using the official shared_preferences.
  • Automatically maps complex locales (e.g., es_ES vs es).
  • Easy syntax: 'my_string'.trJson and 'hello @name'.trParams({'name': 'John'}).

Getting started #

In pubspec.yaml, add this package and create an assets/lang directory for your JSON files:

dependencies:
  easy_json_translate: ^latest

flutter:
  assets:
    - assets/lang/

Add your translation files (e.g. assets/lang/en.json, assets/lang/es.json).

Usage #

1. Initialization #

Call EasyJsonTranslate.init() before running your app.

import 'package:flutter/material.dart';
import 'package:easy_json_translate/easy_json_translate.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // 1. Initialize translations and locale
  await EasyJsonTranslate.init();
  
  runApp(const MyApp());
}

2. Wrap your App #

Use AnimatedBuilder to listen to the EasyJsonTranslate instance for real-time localized updates. Important: You must also pass flutter_localizations delegates so that standard MaterialApp widgets know how to render the new languages!

import 'package:flutter_localizations/flutter_localizations.dart';

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: EasyJsonTranslate(),
      builder: (context, child) {
        return MaterialApp(
          locale: EasyJsonTranslate().currentLocale,
          supportedLocales: EasyJsonTranslate().supportedLocales,
          // Required for standard Material/Cupertino translations
          localizationsDelegates: const [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          home: const HomePage(),
        );
      }
    );
  }
}

3. Translate Strings #

Simply attach .trJson to any string matching your JSON keys!

class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('home_title'.trJson), // Uses String Extension
      ),
      body: Center(
        child: Column(
          children: [
            Text('greeting_message'.trParams({'name': 'User'})),
            ElevatedButton(
              onPressed: () {
                // Change locale dynamically
                EasyJsonTranslate().changeLocale(const Locale('es', 'ES'));
              },
              child: const Text("Change Language"),
            )
          ]
        )
      ),
    );
  }
}

4. Get Supported Languages #

You can easily get a list of the supported language codes dynamically loaded from your JSON files:

// Returns a list of strings like ['en', 'es'] or ['en_US', 'es_ES']
List<String> codes = EasyJsonTranslate().supportedLanguageCodes;

// Returns a list of Locale objects
List<Locale> locales = EasyJsonTranslate().supportedLocales;
0
likes
160
points
106
downloads

Documentation

API reference

Publisher

verified publishergrandezsoft.com

Weekly Downloads

A pure Flutter widget-based package dynamically bridging localized JSON files into the app without generating Dart boilerplate.

Repository (GitHub)
View/report issues

Topics

#i18n #localization #json #translation

License

MIT (license)

Dependencies

flutter, shared_preferences

More

Packages that depend on easy_json_translate