flutter_sheet_localization_generator 1.1.0

  • Readme
  • Changelog
  • Example
  • Installing
  • new64

Flutter Google Sheet localizations generator. #

Generates a localizations delegate from an online Google Sheet file.

Install #

Add the following to your pubspec.yaml:

dependencies:
  flutter_sheet_localization: ^1.0.0

dev_dependencies:
  flutter_sheet_localization_generator: ^1.0.0
  build_runner: ^1.3.1

Usage #

1. Create a Google Sheet #

Create a sheet with your translations (following the bellow format, an example sheet is available here) :

example

Make sure that your sheet is shared :

share

Extract from the link the DOCID and SHEETID values : https://docs.google.com/spreadsheets/d/<DOCID>/edit#gid=<SHEETID>) :

2. Declare a localization delegate #

Declare the following AppLocalizationsDelegate class with the SheetLocalization annotation pointing to your sheet in a lib/localization.dart file :

import 'package:flutter/widgets.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_sheet_localization/flutter_sheet_localization.dart';

part 'localizations.g.dart';

@SheetLocalization("DOCID", "SHEETID") // <- See 1. to get DOCID and SHEETID
class AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
  const AppLocalizationsDelegate();

  @override
  bool isSupported(Locale locale) =>
      AppLocalizations.languages.containsKey(locale);
  @override
  Future<AppLocalizations> load(Locale locale) =>
      SynchronousFuture<AppLocalizations>(AppLocalizations(locale));
  @override
  bool shouldReload(AppLocalizationsDelegate old) => false;
}

3. Generate your localizations #

Run the following command to generate a lib/localization.g.dart file :

flutter packages pub run build_runner build

4. Configure your app #

Update your Flutter app with your newly created delegate :

MaterialApp(
    locale: AppLocalizations.languages.keys.first, // <- Current locale
    localizationsDelegates: [
    const AppLocalizationsDelegate(), // <- Your custom delegate
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
    ],
    supportedLocales:
        AppLocalizations.languages.keys.toList(), // <- Supported locales
    // ...
);

5. Display your labels #

final labels = AppLocalizations.of(context);
print(labels.dates.month.february);
print(labels.templated.hello(firstName: "World"));
print(labels.templated.contact(Gender.male, lastName: "John"));

Google Sheet format #

You can see an example sheet here.

Global format #

The file should have :

  • A first header row
    • Column 0 : "Key"
    • then each supported language code ("en", "fr", ...)
  • Following rows for labels
    • Column 0 : the label key (can be a hierarchy, separated by dots)
    • then each translation based on language code of the column

Conditionals #

It is pretty common to have variants of a label based on a condition (for example: Genders, Plurals, ...).

Simply duplicate your entries and end them with (<ConditionName>.<ConditionCase).

Example :

| example.man(Gender.male) | homme | | example.man(Plural.female) | woman |

See example for more details.

Plurals #

The conditionals can be used the same way for plurals :

Example :

| example.man(Plural.zero) | hommes | man | | example.man(Plural.one) | homme | man | | example.man(Plural.multiple) | hommes | men |

From your Dart code, you can then define a function :

Plural plural(int count) {
  if (count == 0) return Plural.zero;
  if (count == 1) return Plural.one;
  return Plural.multiple;
}

See example for more details.

Dynamic labels #

You can insert a {{KEY}} template into a translation value to have dynamic labels.

A Dart function will be generated to be used from your code.

/// Sheet
values.hello, "Hello {{first_name}}!"

/// Code
print(labels.values.hello(firstName: "World"));

Why ? #

I find the Flutter internationalization tools not really easy to use, and I wanted a simple tool for sharing translations. Most solutions also use string based keys, and I wanted to generate pure dart code to improve permormance.

1.1.0 #

  • Added conditional labels

1.0.0 #

  • Initial version

example/README.md

example2 #

A new Flutter project.

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:
  flutter_sheet_localization_generator: ^1.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_sheet_localization_generator/flutter_sheet_localization_generator.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
27
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
64
Learn more about scoring.

We analyzed this package on Oct 9, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: other

Primary library: package:flutter_sheet_localization_generator/flutter_sheet_localization_generator.dart with components: io, build, mirrors.

Health suggestions

Fix lib/src/parser.dart. (-0.50 points)

Analysis of lib/src/parser.dart reported 1 hint:

line 35 col 13: Use isNotEmpty instead of length

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.5.0 <3.0.0
analyzer ^0.38.4 0.38.5
build ^1.2.0 1.2.0
code_builder ^3.2.0 3.2.0
collection ^1.14.11 1.14.12
csv ^4.0.3 4.0.3
dart_style ^1.2.7 1.3.1
flutter_sheet_localization ^1.0.0 1.0.0
http ^0.12.0 0.12.0+2
meta ^1.1.7 1.1.7
recase ^2.0.1 2.0.1
source_gen ^0.9.0 0.9.4+5
Transitive dependencies
args 1.5.2
async 2.4.0
built_collection 4.2.2
built_value 6.7.1
charcode 1.1.2
convert 2.1.1
crypto 2.1.3
csslib 0.16.1
fixnum 0.10.9
front_end 0.1.27
glob 1.1.7
html 0.14.0+3
http_parser 3.1.3
kernel 0.3.27
logging 0.11.3+2
matcher 0.12.5
package_config 1.1.0
path 1.6.4
pedantic 1.8.0+1
pub_semver 1.4.2
quiver 2.0.5
source_span 1.5.5
stack_trace 1.9.3
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+12
yaml 2.2.0
Dev dependencies
build_runner ^1.0.0
build_test ^0.10.3