intl_manager 0.0.4

  • Readme
  • Changelog
  • Example
  • Installing
  • 64

intl_manager #

A library tool that help you manager i18n strings in flutter,include convert from android "strings-xx.xml". Before to use,you need to kown what is LocalizationsDelegate and what is intl_translation.

Flutter use arb file to manager i18n strings. some times you need to refactor an old android project use flutter.

intl_manager can help you converte android strings-xx.xml to flutter's arb file,and auto generate Dart class include all the strings define.You just need to extend the Dart class and registe with LocalizationsDelegate.

Flutter's internationalization reference: https://github.com/flutter/website/tree/master/examples/internationalization/intl_example/lib

Install #

dev_dependencies:
  intl_manager: version

Use build args #

--scan-dir the path where to scan android style strings-xx.xml files

--out-dir ARB and dart file output path

--gen-class the dart class name of auto generated

--file-name the dart file name of auto generated.default is:"strings_define.dart" (defaults to "strings_define.dart")

--dev-locale use which locale content to generate default dart class

build: flutter packages pub run intl_manager:build --scan-dir=xx --out-dir=yy --gen-class=zz

Use json config 'intl_manager.json' #

{
  "scan-dir": "assets/i18n",
  "out-dir": "lib/i18n/gen",
  "gen-class": "AppStringsDefine",
  "dev-locale": "zh"
}

build: flutter packages pub run intl_manager:build

[0.0.1]

  • Add android's Strings-xx.xml convert to flutter style

[0.0.2]

  • Fix 'intl' package's version(^0.15.8 -> ^0.15.7)
  • Format dart code style

[0.0.3]

  • Fix outDir not exists bug
  • Add Example code

[0.0.4]

  • Add \b, \t, \n, \a, \r support

example/README.md

add dependencies #

dependencies:
  ...
  flutter_localizations:
    sdk: flutter
  intl: ^0.15.7
  intl_translation: ^0.17.3

dev_dependencies:
  ...
  intl_manager: ^0.0.2

copy android's strings-xx.xml #

copy or create android style strings-xx.xml file in flutter project flutter project dir:

├── res
│   ├── strings
│   │   ├── strings-en.xml
│   │   └── strings-zh.xml

create intl_manager build config file #

create config file 'intl_manager.json' in flutter project folder.

{
  "scan-dir": "res/strings",
  "out-dir": "lib/i18n/gen",
  "file-name": "app_strings_define.dart",
  "gen-class": "AppStringsDefine",
  "dev-locale": "en"
}

build strings #

flutter packages pub run intl_manager:build

this command will generated arb and dart files.

├── lib
│   ├── i18n
│   │   └── gen
│   │       ├── app_strings_define.dart
│   │       ├── intl_en.arb
│   │       ├── intl_zh.arb
│   │       ├── messages_all.dart
│   │       ├── messages_en.dart
│   │       └── messages_zh.dart

define strings API #

import 'package:flutter/cupertino.dart';
import 'package:intl/intl.dart';
import 'package:example/i18n/gen/app_strings_define.dart';
import 'gen/messages_all.dart';

///this delegate need register in you main.dart file[main]
class AppStringsDelegate extends LocalizationsDelegate<AppStrings> {
  const AppStringsDelegate();

  @override
  bool isSupported(Locale locale) {
    return AppStrings.getSupportedLanguageCodes().contains(locale.languageCode);
  }

  @override
  Future<AppStrings> load(Locale locale) {
    return AppStrings.load(locale);
  }

  @override
  bool shouldReload(LocalizationsDelegate<AppStrings> old) {
    return false;
  }
}

///project api for i18n strings, extends from 'intl_manager' generated strings define
class AppStrings extends AppStringsDefine{
  static Future<AppStrings> load(Locale locale) {
    final String name = locale.toString();
    final String localeName = Intl.canonicalizedLocale(name);
    return initializeMessages(localeName).then((_) {
      Intl.defaultLocale = localeName;
      return AppStrings();
    });
  }

  static AppStrings of(BuildContext context) {
    return Localizations.of<AppStrings>(context, AppStrings);
  }

  //wrap AppStringsDefine method
  static List<String> getSupportedLanguageCodes(){
    return AppStringsDefine.getSupportedLanguageCodes();
  }

  //wrap AppStringsDefine method
  static List<Locale> createSupportedLocale(bool appendCountryCode){
    return AppStringsDefine.createSupportedLocale(appendCountryCode);
  }
}

register your define #

1.register localizationsDelegates with AppStringsDelegate. 2.provider supportedLocales with AppStrings.createSupportedLocale(false)

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: [
        const AppStringsDelegate(),
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
      ],
      supportedLocales:  AppStrings.createSupportedLocale(false),
      onGenerateTitle: (BuildContext context) => AppStrings.of(context).appTitle,
      ...
    );
  }
}

strings usage #

appBar: AppBar(title: Text(AppStrings.of(context).appTitle))

change locale in running time #

1.reload string define: AppStrings.load(nextLocale).then((_){ setState(() { _locale = nextLocale; }); }); 2.reload UI by Localizations.override

@override
  Widget build(BuildContext context) {
    var scaffold =  Scaffold(
    ...
      body: Center(
        child: Column(
            RaisedButton(
              onPressed: (){
                //change locale
                Locale nextLocale;
                if(_locale.languageCode=='en'){
                  nextLocale = Locale('zh', '');
                }else{
                  nextLocale = Locale('en', '');
                }
                //
                AppStrings.load(nextLocale).then((_){
                  setState(() {
                    _locale = nextLocale;
                  });
                });
              },
              child: Text(AppStrings.of(context).changeLocale),
            )
          ],
        ),
      ),
    );
    return Localizations.override(context: context, child: scaffold,
      locale: _locale,
    );
  }

Use this package as a library

1. Depend on it

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


dependencies:
  intl_manager: ^0.0.4

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:intl_manager/intl_manager.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
36
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
64
Learn more about scoring.

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

  • Dart: 2.5.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health issues and suggestions

Document public APIs. (-1 points)

44 out of 44 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Format lib/src/builder/builder.dart.

Run flutter format to format lib/src/builder/builder.dart.

Format lib/src/builder/code_file_maker.dart.

Run flutter format to format lib/src/builder/code_file_maker.dart.

Format lib/src/builder/xml2arb.dart.

Run flutter format to format lib/src/builder/xml2arb.dart.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (intl).

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.1.0 <3.0.0
args >=0.12.1 <2.0.0 1.5.2
flutter 0.0.0
intl ^0.15.7 0.15.8 0.16.0
intl_translation ^0.17.3 0.17.7
path ^1.6.2 1.6.4
shell ^1.0.1 1.0.1
xml ^3.3.1 3.5.0
Transitive dependencies
analyzer 0.38.5 0.39.0
async 2.4.0
charcode 1.1.2
collection 1.14.11 1.14.12
convert 2.1.1
crypto 2.1.3
csslib 0.16.1
dart_style 1.3.2
file 5.1.0
front_end 0.1.27 0.1.28
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
kernel 0.3.27 0.3.28
meta 1.1.7
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.1.0
pedantic 1.8.0+1
petitparser 2.4.0
platform 2.2.1
process 3.0.12
pub_semver 1.4.2
sky_engine 0.0.99
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
watcher 0.9.7+12
yaml 2.2.0
Dev dependencies
flutter_test