Download CSV file and generates the localization keys from an online Google Sheet to working with easy_localization and easy_localization_loader
This tool inspired by flutter_sheet_localization_generator
Add to your pubspec.yaml
:
dependencies:
easy_localization: <last_version>
easy_localization_loader: <last_version>
dev_dependencies:
build_runner: <last_version>
easy_localization_generator: <last_version>
Create a sheet with your translations. Make sure that all dynamic-arg strings have names of arg. It will generate readable functions of translation to use. (following the bellow format, an example sheet is available here) :
Make sure that your sheet is shared.
Extract from the link the DOCID
value : https://docs.google.com/spreadsheets/d/<DOCID>/edit?usp=sharing
Declare the following _Strings
class with the SheetLocalization
annotation pointing to your sheet in a lib/localization/strings.dart
file
import 'dart:ui';
import 'package:easy_localization_generator/easy_localization_generator.dart';
part 'strings.g.dart';
@SheetLocalization(
docId: 'DOCID',
version: 1, // the `1` is the generated version.
//You must increment it each time you want to regenerate a new version of the labels.
outDir: 'assets/langs', //default directory save downloaded CSV file
outName: 'langs.csv', //default CSV file name
preservedKeywords: [
'few',
'many',
'one',
'other',
'two',
'zero',
'male',
'female',
],
)
class _Strings {}
Run the following command to generate a lib/localization/strings.g.dart
file :
flutter pub run build_runner build --delete-conflicting-outputs
Sample of strings.g.dart
Config step by step following this tutorial from README.md of easy_localization
Text(Strings.title)
Text(
Strings.msg(
name: 'Jack',
type: 'Hot',
),
),
- no named arg version
Text(Strings.amount(counter))
- named arg version (recommend)
Text(
Strings.clicked(
counter,
count: counter,
),
),
Because of the caching system of the build_runner, it can't detect if there is a change on the distant sheet and it can't know if a new generation is needed.
The version
parameter of the @SheetLocalization
annotation solves this issue.
Each time you want to trigger a new generation, simply increment that version number and call the build runner again.
I find the easy_localization has already Code generation, but it doesn't support working with Google Sheet and generate keys from CSV file. So, I make this simple generator tool.
OPTION 2: Easy Localization Generator with flutter_gen
I forked and then added some code, which make Easy Localization Generator can working with it. Please checkout here.