Giter Site home page Giter Site logo

as2f's Introduction

AS2F - Android Strings To Flutter

Overview

Kotlin based generator for converting android xml string resources into one i18n dart class that can be used in flutter projects.

Inspired by flutter_i18n project by long1eu except no need to use IntelliJ or arb files.

Usage in Dart/Flutter Project

dev_dependencies:
 as2f: ^0.1.2

Then run the following command.

flutter pub run as2f:codegen

NOTE: You can add --run-once and --dry-run parameters to the above command.

NOTE: You can try running pub run as2f:codegen in the example folder to play with the carrots_sample ๐Ÿฅ•๐Ÿฅ•

This fetches the jar from jitpack containing the code generator and executes it in a watch mode. It will bootstrap as2f.yaml file for you. This file describes where your Android string files are and where the generated class should go.

- name: android_strings # base name
  source: someplace/main/res # lookup place for all values-lang/strings.xml files
  target: lib/gen/i18n.dart # place where your i18n class should go

The structure of the generated class is much like the one by flutter_i18n meaning you can just follow their setup intructions:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      onGenerateTitle: (BuildContext context) => S.of(context).app_name,
      localizationsDelegates: const <LocalizationsDelegate<WidgetsLocalizations>>[
            S.delegate,
            // You need to add them if you are using the material library.
            // The material components usses this delegates to provide default 
            // localization      
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,               
      ],
      supportedLocales: S.delegate.supportedLocales,
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

If you want to have nice formatting, make sure you have dart-sdk in you PATH, it's included with flutter sdk, so you can e.g.:

export PATH="$PATH:/path/to/flutter/bin/cache/dart-sdk/bin"

Supported Features

  • Normal Strings
  • Strings with arguments
  • Plurals (only integer part)

Code Organization

  • cli - Command Line Tool based on patrol, uses as2f as a module
  • as2f - All the code generation logic & unit tests, contributions welcome.
  • bin - dart script wrapper around jar file allowing you to run pub run as2f:codegen right from your project

as2f's People

Contributors

vishna avatar xinyuez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

xinyuez eyeem

as2f's Issues

support genders

it's not supported in Android xml strings (no mention in the docs) but there could be some naming convention to make it work in generated dart code since intl package does support genders

handle special dart keys

if you put as a key in the xml file one of the following values:

  • with
  • new
  • other
  • do

the generated class will no longer compile, quick workaround for this would be to put trailing underscore

StringId - a dynamic key lookup

In some cases you might want to be able to use strings in more dynamic manner, e.g. backend driven a/b test.

Since dart on flutter offers no reflection we could generate extension method that could achieve that.

class StringKeys {
  StringKeys._();
  static const carrot = "carrot";
  static const apple = "apple";
}

extension StringKeysExtension on S {
  dynamic operator [](String key) {
    switch(key) {
      case StringKeys.carrot: return carrot;
      case StringKeys.apple: return apple;
      default: return null;
    }
  }
}

You could then use it in your code:

final apple1 = strings.apple;
final apple2 = strings["apple"];
final apple3 = strings[StringKeys.apple];

Beware the returned apple might not be a string value but a function in which case you need to know what arguments you want to use.

Can't make it work

Hi
I tried this generator but couldn't make it work on Windows 10 with VSCode.
In the VSCode terminal when executing the flutter packages pub run as2f:codegen command I first had the following error message:

Trying to download as2f-699a11e9e6.jar ...
CreateProcessW failed 2
Failed to download https://jitpack.io/com/github/vishna/as2f/cli/699a11e9e6/cli-699a11e9e6-all.jar
Please download the jar manually and save it to .jarCache/as2f-699a11e9e6.jar.
Once this is complete just rerun pub command.
pub finished with exit code 1

So I manually downloaded the jar file and re-ran the command. I then had the following error message:

? as2f.yaml not found, creating one for you...

with the generator hanging on forever (killed it after 2 or 3 min with CTRL+C) and the as2f.yaml was never generated.
So I manually created the as2f.yaml and filled it with the necessary informations and rerun the command but the generator is still hanging forever (had to manually kill it once again) with no error meassage this time. Any idea of what might be the issue? (I used the strings.xml files from the carrots example for this test so the issue is not with the content of this files)

Stopped working

I tested the example with:
flutter pub run as2f:codegen --run-once

is returning:

โš™๏ธ as2f.yaml loaded
โœ๏ธ /Users/xxxx/AndroidStudioProjects/flutter/as2f/example/carrots_sample
๐Ÿ‘‹ KTHXBAI

however the carrots.dart file is not being created. I'm using it in my project and it also stopped working.

flutter --version
Flutter 1.12.13+hotfix.8 โ€ข channel stable โ€ข https://github.com/flutter/flutter.git
Framework โ€ข revision 0b8abb4724 (7 weeks ago) โ€ข 2020-02-11 11:44:36 -0800
Engine โ€ข revision e1e6ced81d
Tools โ€ข Dart 2.7.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.