rosetta_generator 0.0.3+2

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 65

rosetta_generator #

Pub

Generates Helper classes for localization files, which can be used with flutter_localizations library.

Configuration #

  1. Add rosetta to pubspec.yaml under the dependencies: section. The latest version is Pub
dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  rosetta: ^latest_version
  1. Add build_runner and rosetta_generator under the dev_dependencies: section of the pubspec.yaml file. The latest version is Pub
dev_dependencies:  
  build_runner: '>=0.10.3 <1.2.0'
  rosetta_generator: ^latest_version
  1. Add (or modify) the build.yaml file in the same folder as the pubspec.yaml and include the rosetta builder.
targets:
  $default:
    builders:
      rosetta:

Usage #

In your library add the following import:

import 'package:rosetta/rosetta.dart';

Create a class containing two static members which will be used later for localization:

class Translation {
  static LocalizationsDelegate<Translation> delegate;

  static Translation of(BuildContext context) {
    return Localizations.of(context, Translation);
  }
}

Annotate the class with the rosetta Stone annotation. The path parameter should point to a directory containing the [languageCode].json localization files.

@Stone(path: 'i18n')
class Translation {
  static LocalizationsDelegate<Translation> delegate;

  static Translation of(BuildContext context) {
    return Localizations.of(context, Translation);
  }
}

Include the part directive indicating the file that will be generated (typically the same file with a .g extension before .dart):

part 'rosetta_generator_example.g.dart';

Run build_runner:

flutter packages pub run build_runner build

Note: On first attempt to run this command you might encounter a conflict error. If so, please add the --delete-conflicting-outputs argument to your command:

flutter packages pub run build_runner build --delete-conflicting-outputs

(This additional argument allows the command to overwrite the .g.dart file if necessary.)

You can also use the watch command instead of build. This will generate your file when it's saved.

flutter packages pub run build_runner watch

This process will generate 3 classes (let's assume that the annotated class was called Translation as in the example above):

  • _$Keys: Contains all your keys as static fields, this is currently for internal use.
  • _$TranslationDelegate: This is an implementation of LocalizationsDelegate<Translation>, this should be passed to MaterialApp or CupertinoApp as a localization delegate. Also should be passed to the static delegate attribute of your original class.
  • _$TranslationHelper: An abstract class, which is meant to be mixed in to your annotated class. Contains functions to access the localized strings for each key (ex.: String get emptyList => _translate(_$Keys.emptyList);).

If you apply the generated classes you will end up something like this:

@Stone(path: 'i18n')
class Translation with _$TranslationHelper { // Generated mixin class
  static LocalizationsDelegate<Translation> delegate = _$TranslationDelegate(); // Generated delegate

  static Translation of(BuildContext context) {
    return Localizations.of(context, Translation);
  }
}

You can now start using your localization logic:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text(
          	/// Returns a string repesented with a key "hello_there" in the localization files.
            Translation.of(context).helloThere,
          ),
        ),
      ),
      localizationsDelegates: [
      	/// Returns the generated delegate, which will setup the [Translation] instances.
        Translation.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: [
        const Locale(SupportedLocales.english),
      ],
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
    );
  }
}

The generated code backing the above functionality looks something like this (changes according to translation input):

class _$TranslationDelegate extends LocalizationsDelegate<Translation> {
  
  @override
  bool isSupported(Locale locale) => ["en"].contains(locale.languageCode);

  @override
  bool shouldReload(LocalizationsDelegate<Translation> old) => false;

  @override
  Future<Translation> load(Locale locale) async {
    var translations = Translation();
    await translations.load(locale);
    return translations;
  }
}

class _$Keys {
  static final String helloThere = "hello_there";

  static final String seeYouSoon = "see_you_soon";
}

abstract class _$TranslationHelper {
  Map<String, String> _translations;

  Future<void> load(Locale locale) async {
    var jsonStr =
        await rootBundle.loadString("i18n/${locale.languageCode}.json");
    Map<String, dynamic> jsonMap = json.decode(jsonStr);
    _translations = jsonMap
        .map<String, String>((key, value) => MapEntry(key, value as String));
  }

  String _translate(String key) => _translations[key];

  String get helloThere => _translate(_$Keys.helloThere);
  String get seeYouSoon => _translate(_$Keys.seeYouSoon);
}

Initial version #

Basic functionality implemented. Features are on the way.

example/rosetta_generator_example.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:rosetta/rosetta.dart';
import 'dart:ui';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'dart:convert';

part 'rosetta_generator_example.g.dart';

@Stone(path: 'i18n')
class Translation with _$TranslationHelper {
  static LocalizationsDelegate<Translation> delegate = _$TranslationDelegate();

  static Translation of(BuildContext context) {
    return Localizations.of(context, Translation);
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  rosetta_generator: ^0.0.3+2

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:rosetta_generator/rosetta_generator.dart';
  
Version Uploaded Documentation Archive
0.1.2 Feb 25, 2019 Go to the documentation of rosetta_generator 0.1.2 Download rosetta_generator 0.1.2 archive
0.1.1 Feb 10, 2019 Go to the documentation of rosetta_generator 0.1.1 Download rosetta_generator 0.1.1 archive
0.1.0 Jan 24, 2019 Go to the documentation of rosetta_generator 0.1.0 Download rosetta_generator 0.1.0 archive
0.0.3+3 Jan 18, 2019 Go to the documentation of rosetta_generator 0.0.3+3 Download rosetta_generator 0.0.3+3 archive
0.0.3+2 Jan 18, 2019 Go to the documentation of rosetta_generator 0.0.3+2 Download rosetta_generator 0.0.3+2 archive
0.0.3+1 Jan 17, 2019 Go to the documentation of rosetta_generator 0.0.3+1 Download rosetta_generator 0.0.3+1 archive
0.0.3 Jan 16, 2019 Go to the documentation of rosetta_generator 0.0.3 Download rosetta_generator 0.0.3 archive
0.0.2 Jan 16, 2019 Go to the documentation of rosetta_generator 0.0.2 Download rosetta_generator 0.0.2 archive
0.0.1 Jan 16, 2019 Go to the documentation of rosetta_generator 0.0.1 Download rosetta_generator 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
42
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
70
Overall:
Weighted score of the above. [more]
65
Learn more about scoring.

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

  • Dart: 2.3.1
  • pana: 0.12.16

Platforms

Detected platforms:

References Flutter, but has conflicting libraries: package:rosetta_generator/rosetta_generator.dart (components: io, isolate, build, mirrors).

Maintenance issues and suggestions

Fix platform conflicts. (-20 points)

References Flutter, but has conflicting libraries: package:rosetta_generator/rosetta_generator.dart (components: io, isolate, build, mirrors).

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
analyzer ^0.32.6 0.32.6 0.36.3
build >=0.12.6 <1.1.0 1.0.2 1.1.4
code_builder ^3.1.3 3.2.0
dart_style ^1.1.3 1.1.3 1.2.7
path ^1.6.2 1.6.2
recase ^2.0.0+1 2.0.1
rosetta ^0.0.1+1 0.0.1+4 0.1.2
source_gen ^0.9.1 0.9.1+3 0.9.4+2
Transitive dependencies
args 1.5.1
async 2.2.0
built_collection 4.2.2
built_value 6.5.0
charcode 1.1.2
collection 1.14.11
convert 2.1.1
crypto 2.0.6
csslib 0.16.0
fixnum 0.10.9
front_end 0.1.4+2 0.1.18
glob 1.1.7
html 0.14.0+2
kernel 0.3.4+2 0.3.18
logging 0.11.3+2
matcher 0.12.5
meta 1.1.7
package_config 1.0.5
plugin 0.2.0+3
quiver 2.0.3
source_span 1.5.5
stack_trace 1.9.3
string_scanner 1.0.4
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+10
yaml 2.1.15
Dev dependencies
build_runner ^1.1.0
flutter