flappy_translator 1.2.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 83

flappy_translator #

A Flutter internationalized strings generator which automatically imports localization strings from a CSV file.

This is especially useful as any team member can work on a CSV file, with the translations imported into the project with the use of a simple terminal command. This contrasts starkly to the default i18n approach in which dart files need to be manually for new keys and languages.

Getting Started #

In order to use the flappy_translator package, please provide your translations in a CSV file - separators , and ; have been tested to work.

Create a CSV file #

Consider that we have a table of localizations for the following languages:

keysfrenesde_CH
appTitleMa super applicationMy awesome applicationMi gran applicationMeine tolle App
subtitleUn sous titreA subtitleUn subtitulòEin Untertitel
descriptionUn texte avec une variable : %1$sText with a variable: %1$sUn texto con una variable : %1$sText mit einer Variable: %1$s
littleTest"Voici, pour l'exemple, ""un test"" avec la variable %age$d""Here is, for the example, ""a test"" with the variable %age$d""Aqui esta, por ejemplo, ""una prueba"" con la variable %age$d""Hier ist, zum Beispiel, ""ein Test"" mit der Variable %age$d"

This spreadsheet would be exported as the following CSV file:

keys,fr,en,es,de_CH
appTitle,Ma super application,My awesome application,Mi gran application,Meine tolle App
subtitle,Un sous titre,A subtitle,Un subtitulò,Ein Untertitle
description,Un texte avec une variable : %1$s,Text with a variable: %1$s,Un texto con una variable : %1$s,Text mit einer Variable: %1$s
littleTest,"Voici, pour l'exemple, ""un test"" avec la variable %age$d","Here is, for the example, ""a test"" with the variable %age$d","Aqui esta, por ejemplo, ""una prueba"" con la variable %age$d","Hier ist, zum Beispiel, ""ein Test"" mit der Variable %age$d"

Add dependency #

dependencies:
  flutter_localizations:
    sdk: flutter
    
dev_dependencies: 
  flappy_translator: 

Define Settings #

Settings for flappy_translator can be optionally set in your project's pubspec.yaml file:

flappy_translator:
  input_file_path: "test.csv"
  output_dir: "lib"
  file_name: "i18n"
  class_name: "I18n"
  delimiter: ","
  start_index: 1
  depend_on_context: true
  use_single_quotes: false
  replace_no_break_spaces: false
SettingDefaultDescription
input_file_pathN/AA path to the input CSV file.
output_dirlibA directory to generate the output file.
file_namei18nA filename for the generated file.
class_nameI18nA class name for the generated file.
delimiter,A delimited to separate columns in the input CSV file.
start_index1The column index where translations begin (i.e. column index of main language.)
depend_on_contexttrueWhether the generated localizations should depend on BuildContext
use_single_quotesfalseWhether the generated file should use single or double quotes for strings.
replace_no_break_spacesfalseWhether no break spaces (\u00A0) should be replaced with normal spaces (\u0020).

Run package #

Make sure that your current working directory is the project root.

A file path to the CSV file must be supplied, either as a setting in pubspec.yaml or as a command line argument (CLA), while the output directory can also be optionally supplied as a CLA.

flutter pub get
flutter pub run flappy_translator <test.csv> <output dir>

Use the i18n generated file #

The package used your CV file in order to generate a file named file_name in output_dir you provided. The following example uses the default class_name I18n with a dependency on BuildContext:

  1. 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: I18nDelegate.supportedLocals,
      home: Home(),
    );
  }
}
  1. 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)),
            ],
          ),
        ),
      ),
    );
  }
}

Update iOS Info.plist #

For iOS, ios/Runner/Info.plist needs to be updated with an array of the languages that the app supports:

<key>CFBundleLocalizations</key>
<array>
  <string>fr</string>
  <string>en</string>
  <string>es</string>
  <string>de</string>
</array>

For more information, see Internationalizing Flutter apps.

Rules and functionalities #

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 corresponding one in the default language.

  • The first column must be totally filled ! It will not work otherwise.

Handling different languages for one country #

You have the possibility, since version 1.4 to write something like de_CH. It will take the Swiss's German language.

Add variables in Strings #

We added the possibility to handle variables in the Strings. This means respecting some rules :

  1. 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)
  1. 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 becomes var1
  • %age$d becomes age
  1. 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.

[1.0.0] - 21/08/2019

  • Initial Release

[1.0.1] - 21/08/2019

  • Fix documentation
  • Fix template's file path

[1.0.2] - 21/08/2019

  • Fix template by handling a String instead of file

[1.0.3] - 21/08/2019

  • Fix error on CSV not well formed
  • Fix error when using Dart reserved keywords

[1.0.4] - 18/10/2019

  • Handle possibility to have multiple languages for one country

[1.1.0] - 02/12/2019

  • Add lot of configurations in pubspec.yaml :
    • Ability to define generated filename and classname
    • Ability to use other delimiter than ","
    • Ability to have other columns between keys and values (one for description for example)
    • No need for BuildContext
    • Ability to use double or simple quotes on generated file
  • Additionally :
    • All generated parameters are now @required
    • Added currentLocale to generated file
    • Better Readme.md

[1.2.0] - 06/12/2019

  • Add setting to handle "no break" spaces by replacing them by normal spaces

example/README.md

example #

A new Flutter application.

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

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


dependencies:
  flappy_translator: ^1.2.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support 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:flappy_translator/flappy_translator.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
72
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
86
Overall:
Weighted score of the above. [more]
83
Learn more about scoring.

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

  • Dart: 2.7.0
  • pana: 0.13.4
  • Flutter: 1.12.13+hotfix.5

Health suggestions

Format lib/flappy_translator.dart.

Run flutter format to format lib/flappy_translator.dart.

Format lib/parsing/csv_parser.dart.

Run flutter format to format lib/parsing/csv_parser.dart.

Maintenance suggestions

The package description is too short. (-14 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.3.0 <3.0.0
csv ^4.0.3 4.0.3
dart_style ^1.3.3 1.3.3
flutter 0.0.0
meta ^1.1.7 1.1.8
yaml ^2.2.0 2.2.0
Transitive dependencies
_fe_analyzer_shared 1.0.3
analyzer 0.39.4
args 1.5.2
async 2.4.0
charcode 1.1.2
collection 1.14.11 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.16.1
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.1.0
path 1.6.4
pedantic 1.9.0
pub_semver 1.4.2
sky_engine 0.0.99
source_span 1.6.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
watcher 0.9.7+13
Dev dependencies
flutter_test