Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

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

dependencies: 
    flutter_localizations: 
        sdk: flutter 
dev_dependencies:
    gen_lang: 0.1.3

usage

pub run gen_lang:generate

a below table shown all supported arguments:

argument description
–source-dir a source folder contains all string json files (defaults to “res/string”)
–output-dir a output folder stores all generated files (defaults to “lib/generated”)
–template-locale use 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.

example

{ 
    "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.

example

{ 
    "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.

example

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

...

materialapp(
      localizationsdelegates: [
        s.delegate,
        globalmateriallocalizations.delegate,
        globalwidgetslocalizations.delegate
      ],
      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


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.