Internationalization of Flutter Application Using .json Files

Tiempo de lectura: 3 minutos

Reading time: 3 minutes

Internationalizing a flutter application is easy by following the following steps as I show below:

First, we need to add the necessary dependencies in the pubspec.yaml file:

  flutter_localizations:
    sdk: flutter
  flutter_cupertino_localizations: ^1.0.1
Next, we create the directory structure with the .json files

From the root of our project, we create a directory named “assets”, and inside it, we add another directory named “i18n”. In this directory, we create as many .json files as languages we want to translate our platform or application into. Here’s an example:

Next, we add the paths to access the information from the .json files in the pubspec.yaml:

assets:
- assets/i18n/en.json
- assets/i18n/es.json

Now, we need to create two important classes for internationalizing our application:

class Translate {
  late final Locale locale;

  Translate(this.locale);

  static Translate? of(BuildContext context) {
    return Localizations.of<Translate>(context, Translate);
  }

  static const LocalizationsDelegate<Translate> delegate = _TranslateDelegate();

  late Map<String, String> _localizedStrings;

  Future<bool> load() async {
    //json file path
    String jsonString =
        await rootBundle.loadString('assets/i18n/${locale.languageCode}.json');

    // final jsonString = await rootBundle.loadString('assets/i18n/es.json');
    final data = json.decode(jsonString);
    Map<String, dynamic> jsonMap = json.decode(jsonString);
    print(data);

    _localizedStrings = jsonMap.map((key, value) {
      return MapEntry(key, value.toString());
    });
    return true;
  }

  String translate(String key) {
    return _localizedStrings[key]!;
  }
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------

class _TranslateDelegate extends LocalizationsDelegate<Translate> {
  const _TranslateDelegate();

  @override
  bool isSupported(Locale locale) {
    return ['en', 'es'].contains(locale.languageCode);
  }

  @override
  Future<Translate> load(Locale locale) async {
    Translate localizations = new Translate(locale);
    await localizations.load();
    return localizations;
  }

  @override
  bool shouldReload(_TranslateDelegate old) => false;
}

Next, to configure the platform, inside the Material App widget, we add the following:

 supportedLocales: [
        Locale("en"),
        Locale("es"),
      ],
      localizationsDelegates: [
        Translate.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate
      ],

Finally, let’s see how to display our first translation example with a test text:

We create a key in the .json files, both for English and Spanish:

Reading time: 3 minutes

Internationalizing a Flutter application is easy by following the steps below:

First, we need to add the necessary dependencies in the pubspec.yaml file:

  flutter_localizations:
    sdk: flutter
  flutter_cupertino_localizations: ^1.0.1
Next, we create the directory structure with the .json files

From the root of our project, create a directory named “assets”, and inside it, add another directory named “i18n”. In this directory, create as many .json files as languages you want to translate your platform or application into. Here’s an example:

Next, add the paths to access the information from the .json files in the pubspec.yaml:

assets:
- assets/i18n/en.json
- assets/i18n/es.json

Now, we need to create two important classes for internationalizing our application:

class Translate {
  late final Locale locale;

  Translate(this.locale);

  static Translate? of(BuildContext context) {
    return Localizations.of<Translate>(context, Translate);
  }

  static const LocalizationsDelegate<Translate> delegate = _TranslateDelegate();

  late Map<String, String> _localizedStrings;

  Future<bool> load() async {
    //json file path
    String jsonString =
        await rootBundle.loadString('assets/i18n/${locale.languageCode}.json');

    // final jsonString = await rootBundle.loadString('assets/i18n/es.json');
    final data = json.decode(jsonString);
    Map<String, dynamic> jsonMap = json.decode(jsonString);
    print(data);

    _localizedStrings = jsonMap.map((key, value) {
      return MapEntry(key, value.toString());
    });
    return true;
  }

  String translate(String key) {
    return _localizedStrings[key]!;
  }
}

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------

class _TranslateDelegate extends LocalizationsDelegate<Translate> {
  const _TranslateDelegate();

  @override
  bool isSupported(Locale locale) {
    return ['en', 'es'].contains(locale.languageCode);
  }

  @override
  Future<Translate> load(Locale locale) async {
    Translate localizations = new Translate(locale);
    await localizations.load();
    return localizations;
  }

  @override
  bool shouldReload(_TranslateDelegate old) => false;
}

Next, to configure the platform, inside the Material App widget, we add the following:

 supportedLocales: [
        Locale("en"),
        Locale("es"),
      ],
      localizationsDelegates: [
        Translate.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate
      ],

Finally, let’s see how to display our first translation example with a test text:

We create a key in

Leave a Comment