AppLang, Language Files Generator

If you don’t like working with JSON when adding multilingual support to your Flutter projects, you now have a good solution.

AppLang lets you easily generate the JSON files you need from a Dart file.

While adding English language support to my own application, Birikim, I realised that working with JSON was not a very good method and decided to develop a package for it.

I also didn’t like the fact that the translations were in different JSON files.

Now with AppLang we can add all translations to a Dart file and generate the JSON files we need from the source file or the asset loader class for easy_localiation.

So we no longer need to manually edit JSON files.

// ignore_for_file: non_constant_identifier_names, type_annotate_public_apis
import 'package:app_lang/app_lang.dart';
@AppLangConfig(json: true, easyLoader: true)
class AppLangSource {
  var user = (
    gender: (
      male: (en_US: "Hi man {} ;) ",),
      female: (en_US: "Hello girl :)",),
      other: (en_US: "Hello {}",),
    ),
    money: (
      zero: (en_US: "You not have money",),
      one: (en_US: "You have {} dollar",),
      many: (en_US: "You have {} dollars",),
      other: (en_US: "You have {} dollars",),
    ),
  );
  var incomeTransaction = (
    fields: (
      amount: (en_US: "Price",),
      paymentDate: (en_US: "Payment Date",),
    ),
    actions: (
      update: (en_US: "Edit",),
      detail: (en_US: "Detail",),
      addTag: (en_US: "Add Tag",),
    ),
    validations: (
      amount: (
        notNull: (en_US: "You must enter an amount",),
        lessThen: (en_US: "Amount must be less than 100",),
      ),
    ),
  );
  var expenseTransaction = (
    fields: (
      amount: (en_US: "Price",),
      paymentDate: (en_US: "Payment Date",),
    ),
  );
}

After creating a file consisting of records fields and marking it with @AppLangConfig, the only thing we need to do is to run build_runner.

The package will create files below in the same directory.

Import New Locale

You can import a new locale with a JSON file. When you import a new local, the files will generated again with new translations.

import 'package:app_lang/app_lang.dart';
@AppLangConfig(import: Import(locale: "en_US", path: "lib/lang/imports/en_US.json",json: true, easyLoader: true,))
class AppLangSource {     
     // Fields ...    
}

Remove Existing Locale

You can remove an existing locale from the sourcefile with remove parameter of AppLangConfig annotation.

When you remove a locale, all files will be generated again.

import 'package:app_lang/app_lang.dart';
@AppLangConfig(remove: "en_US", json: true, easyLoader: true)
class AppLangSource {
    // Fields ...
}

And More…

captionless image

You can check the page on pub.dev (https://pub.dev/packages/app_lang).

Happy coding.

Jun 8, 2024

© 2024

Email LinkedIn Medium