flappy_translator internationalized
a flutter internationalized strings generator. the idea is to automate the static strings generation from a csv file. this way, anybody could make a csv file with all the translations and automatically generate corresponding dart code.
getting started
in order to use the flappy_translator package, you will have to provide your translatinos in a csv file (formatted with comma separator).
create a csv and export it in “comma separated” format
here is our csv example :
exported file myfile.csv internationalized
keys,fr,en,es
apptitle,ma super application,my awesome application,mi gran application
subtitle,un sous titre,a subtitle,un subtitulò
description,un texte avec une variable : %1$s,a text with a variable : %1$s,un texto con una variable : %1$s
littletest,"voici, pour l'exemple, ""un test"" avec la variable %age$d","here is, for the example, ""a test"" with variable %age$d","aqui esta, por ejemplo, ""una prueba"" con la variable %age$d"
add dependency
dependencies:
flutter_localizations:
sdk: flutter
dev_dependencies:
flappy_translator:
run package
flutter pub get
flutter pub run flappy_translator test.csv path/to/destination
use the i18n generated file
the package used your cv file in order to generate a file named i18n.dart
in path/to/destination
you provided.
once you have this file in your project, all you have to do is :
- add the i18ndelegate to your delegates
class myapp extends statelesswidget {
@override
widget build(buildcontext context) {
return materialapp(
localizationsdelegates: [
const i18ndelegate(),
globalmateriallocalizations.delegate,
globalwidgetslocalizations.delegate,
],
supportedlocales: [
const locale('en', ''),
const locale('fr', ''),
const locale('es', ''),
],
home: home(),
);
}
}
- use your generated i18n class ! 🙂
class home extends statelesswidget {
@override
widget build(buildcontext context) {
return scaffold(
body: safearea(
child: center(
child: column(
children: <widget>[
text(i18n.of(context).apptitle),
text(i18n.of(context).description(var1: 2)),
text(i18n.of(context).littletest(age: 32)),
],
),
),
),
);
}
}
rules and functionnalities
default language
the first
language’s column of your csv file will be considered as the default
one.
that means :
- if other languages does not have translation for specific words, it will take the corrresponding one in the default language.
- the first column must be totally filled ! it will not work otherwise.
add variables in strings
we added the possibility to handle variables in the strings.
this means respecting some rules :
- in order to be able to recognize them, you must write them this way :
- %myvariable$d (
d
stands for an int type) - %myvariable$s (
s
stands for a string type)
- if your variable’s name start with a number, the generated name will be
varmyvariable
otherwise, the generated variable name would be the name you provided.
%1$d
becomesvar1
%age$d
becomesage
- variables are optional in the generated dart code
let’s take the example of the description
string in the csv we used.
the generated function signature will be :
string description({string var1,})
if the variables are not provided, the string will be given without replacing the variables placeholders.
Comments are closed.