gen_lang 0.1.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 89

gen_lang #

gen_lang is a dart library for internationalization. Extracts messages to generate dart files required by Intl.

Now, three steps for internationalization

  1. Preparing Json files
  2. Run gen_lang
  3. Use it in coding

Installation #

Add these libraries into pubspec.yaml

        sdk: flutter 
    gen_lang: 0.1.0

Usage #

pub run gen_lang:generate

A below table shown all supported arguments:

--source-dirA source folder contains all string json files (defaults to "res/string")
--output-dirA output folder stores all generated files (defaults to "lib/generated")
--template-localeUse string_{template-locale}.json as a template to search KEY. If string_en does not exist, this script will use the first string json file as a template (defaults to "en")

Source of Json Files #

By default, the json files are required to locate at res/string. Using --source-dir argument can change the default source path. These files must be named in this pattern string_{Locale}.json

Example of the files,

|--- lib 
|--- res 
     |--- string 
         |--- string_en.json 
         |--- string_zh_TW.json 
         |--- string_ja.json 

Supported Message Type #

Simple message #

Define a json key and message without parameters

    "simpleMessage": "This is a simple Message"

Message with parameters #

Define a message with parameters. Use ${parameterName} in a message.

    "messageWithParams": "Hi ${yourName}, Welcome you!"

Plural messages with parameters #

Define messages in a plural form. ${how many} is a reserved parameter in a plural message. This parameters support integer only. For Intl, Intl.plural() supports these plural keyword including 'Zero', 'One', 'Two', 'Few', 'Many' and 'Other'. Define a json key into this pattern {jsonKey} {pluralKeyword}. For 'Other', need to define 'POther' for the plural keyword.


    "pluralMessageZero": "Hi ${interviewerName}, I have no working experience.", 
    "pluralMessageOne": "Hi ${interviewerName}, I have one year working experience.", 
    "pluralMessageTwo": "Hi ${interviewerName}, I have two years of working experience.", 
    "pluralMessageFew": "Hi ${interviewerName}, I have few years of working experience.", 
    "pluralMessageMany": "Hi ${interviewerName}, I worked for a long time.", 
    "pluralMessagePOther": "Hi ${interviewerName}, I have ${howMany} years of working experience."

To know plural rules more, please read Intl's plural rules source code.

Gender message with parameters #

Define a message in gender form. ${targetGender} is a reserved parameter in a gender message. This parameters support string value with 'male', female' and 'other'. For Intl, Intl.gender() supports these keyword including 'Male', 'Female' and 'Other'. Define a json key into this pattern {jsonKey} {genderKeyword}. For 'Other', need to define 'GOther' for the gender keyword.


    "genderMessageMale": "Hi ${name}, He is boy.", 
    "genderMessageFemale": "Hi ${name}, She is girl", 
    "genderMessageGOther": "Hi ${name}, he/she is boy/girl." 

Usage of i18n.dart #

The script will generate two files into the output folder including i18n.dart and messages_all.dart. Import i18n.dart into your source code. Then finish internationalization tasks.


import 'package:gen_lang_example/generated/i18n.dart';


      localizationsDelegates: [
      supportedLocales: S.delegate.supportedLocales,

        S.of(context).simpleMessage; // Simple Message 
        S.of(context).messageWithParams('developer'); // Message with parameters
        S.of(context).pluralMessage(10, 'King Wu'); // Plural Message with parameters
        S.of(context).genderMessage('male', 'King Wu'); // Gender Message with parameters

Running Script in Example #

Go into example folder. Run the command

cd example
pub run gen_lang:generate

Then will generate i18n.dart and messages_all.dart

0.1.2 #

Fix #

  • Special Characters cannot be included into messages

0.1.1 #

Fix #

  • Update ReadMe

0.1.0 #

Feature #

  • Scan json files
  • Code generate i18n.dart and message_all.dart required by Intl library
  • Support simple message
  • Support message with parameters
  • Support plural message with parameters
  • Support gender message with parameters


import 'package:flutter/material.dart';
import 'package:gen_lang_example/generated/i18n.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  _MyAppState createState() => _MyAppState();

class _MyAppState extends State<MyApp> {

  int index = 0;
  List<Locale> localeList = [Locale('en'), Locale('ja'), Locale('zh', 'TW')];

  void initState() {

  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: [
      supportedLocales: S.delegate.supportedLocales,
      locale: localeList[index],
      home: Builder(
        builder: (BuildContext context) {
          return Scaffold(
            appBar: AppBar(
              title: Text(S.of(context).locale),
            body: Center(
              child: Column(
                children: <Widget>[
                  Text(S.of(context).pluralMessage(1, 'KingWu')),
                  Text(S.of(context).pluralMessage(10, 'KingWu')),
                  Text(S.of(context).genderMessage('male', 'KingWu')),
                  Text(S.of(context).genderMessage('female', 'KingWu')),
                  Text(S.of(context).genderMessage('other', 'KingWu')),
            floatingActionButton: FloatingActionButton(
              onPressed: () {
              child: Icon(Icons.add),


  void switchLang(){
    setState(() {
      if(index >= localeList.length){
        index = 0;


Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  gen_lang: ^0.1.2

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:gen_lang/core_18n.dart';
import 'package:gen_lang/extra_json_file_tool.dart';
import 'package:gen_lang/extra_json_message_tool.dart';
import 'package:gen_lang/generate_i18n_dart.dart';
import 'package:gen_lang/generate_message_all.dart';
import 'package:gen_lang/print_tool.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Apr 4, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6

Health issues and suggestions

Document public APIs. (-1 points)

99 out of 99 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/print_tool.dart. (-1.49 points)

Analysis of lib/print_tool.dart reported 3 hints:

line 6 col 17: Unnecessary new keyword.

line 11 col 17: Unnecessary new keyword.

line 16 col 17: Unnecessary new keyword.

Fix bin/generate.dart. (-0.50 points)

Analysis of bin/generate.dart reported 1 hint:

line 29 col 16: Unnecessary new keyword.

Fix lib/extra_json_file_tool.dart. (-0.50 points)

Analysis of lib/extra_json_file_tool.dart reported 1 hint:

line 86 col 19: Unnecessary new keyword.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
ansicolor ^1.0.2 1.0.2
args >=0.12.1 <2.0.0 1.6.0
path >=0.9.0 <2.0.0 1.6.4