i18nstr 0.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 40

i18nstr #

一个flutter国际化插件

特性 #

可用于web、Android ,iOS未测试。

可实时读取i18n目录下的语言文件,如zh_CN.json。

约定:语言文件放在i18n文件夹下

开始、使用方法 #

具体参看样例

i18n/zh_CN.json ,在主要的、默认的语言文件中,可加入supportedLocales,其他语言文件是否添加这个,无所谓了。 :

{
  "supportedLocales": "zh_CN,en_US,ja_JP",
  "chinese": "中文",
  "english": "英语",
  "title": "国际化字符串插件测试",
  "ip": "172.16.40.101:1112",
  "system": "xx系统",
  "pushedTimes": "按键 {0} - {1} 次",
  "animal": "动物: ::dog:: ::cat:: ",
  "a": {
    "firstPage": "首页"
  },
  "c": {
    "commit": "录入  ",
    "info": "信息",
    "metaInfo": "基础信息",
    "URL_saveRow": "http://<<ip>>/entity/"
  }
}

在pubspec.yaml中:

dependencies:
    i18nstr: x.x.x
flutter:
   assets:
    - ./i18n/

在main.dart中:

//设定 初始化默认语言,
I18nDelegate _i18nDelegate = new I18nDelegate(Locale('zh', 'CN'));
//设定null,初始化语言是 手机语言设置中的支持语言列表第一个 
//或new I18nDelegate(null);
class MyApp extends StatefulWidge

class _MyAppState extends State<MyApp> 
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      supportedLocales: I18nDelegate.supportedLocales,
      localizationsDelegates: [
        _i18nDelegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      home: HomePage(this.switchLanguage),
    );
  }

//回调函数
  void switchLanguage() {
    setState(() {});
  }

_str = I18nstr.of(context); 不能放在上面第一个StatefulWidge中,否则寻找不到_str。 所以又建了个StatefulWidge。

class _HomePageState extends State<HomePage> {
  I18nstr _str;

@override
  Widget build(BuildContext context) {
    _str = I18nstr.of(context);

    return Scaffold(
      appBar: AppBar(
        title: Text(_str.valueOf('title')),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text('${_str.valueOf('system')}'),
          Text('嵌套功能 a.firstPage : ${_str.valueOf('a.firstPage')}'),
          Text('序号替换功能  按键 {0} - {1} 次, : ${_str.valueOf('pushedTimes', args: [50, 80])}'),
          Text('map值替换功能  key,value : ${_str.valueOf('animal', namedArgs: {'dog': '小狗', 'cat': '小猫'})}'),
          Text('变量功能 http://<<ip>>/entity/ :${_str.valueOf('c.URL_saveRow')}'),


            RaisedButton.icon(
                icon: Icon(Icons.save, size: 25.0),
                //padding: EdgeInsets.all(15.0),
                label: Text('${_str.valueOf('english')}'),
                color: Colors.blue,
                textColor: Colors.white,
                onPressed: () {
                  setState(() {
                    _i18nDelegate = new I18nDelegate(Locale('en', 'US'));
                    widget.callback();
                  });
                },
              ),

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

0.0.1 #

  • TODO: Describe initial release.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:i18nstr/i18nstr.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

I18nDelegate _i18nDelegate = new I18nDelegate(Locale('zh', 'CN'));
//或new I18nDelegate(null);

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
  }

  void switchLanguage() {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      supportedLocales: I18nDelegate.supportedLocales,
      //修改i18nDelegate,setState,可以更改UI语言文字
      localizationsDelegates: [
        _i18nDelegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      home: HomePage(this.switchLanguage),
    );
  }
}

class HomePage extends StatefulWidget {
  Function callback;
  HomePage(@required this.callback);
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  I18nstr _str;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    _str = I18nstr.of(context);

    return Scaffold(
      appBar: AppBar(
        title: Text(_str.valueOf('title')),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text('${_str.valueOf('system')}'),
          Text('嵌套功能 a.firstPage : ${_str.valueOf('a.firstPage')}'),
          Text('序号替换功能  按键 {0} - {1} 次, : ${_str.valueOf('pushedTimes', args: [50, 80])}'),
          Text('map值替换功能  key,value : ${_str.valueOf('animal', namedArgs: {'dog': '小狗', 'cat': '小猫'})}'),
          Text('变量功能 http://<<ip>>/entity/ :${_str.valueOf('c.URL_saveRow')}'),

          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              RaisedButton.icon(
                icon: Icon(Icons.save, size: 25.0),
                //padding: EdgeInsets.all(15.0),
                label: Text('${_str.valueOf('english')}'),
                color: Colors.blue,
                textColor: Colors.white,
                onPressed: () {
                  setState(() {
                    _i18nDelegate = new I18nDelegate(Locale('en', 'US'));
                    widget.callback();
                  });
                },
              ),
              RaisedButton.icon(
                icon: Icon(Icons.save, size: 25.0),
                //padding: EdgeInsets.all(15.0),
                label: Text('${_str.valueOf('chinese')}'),
                color: Colors.blue,
                textColor: Colors.white,
                onPressed: () {
                  setState(() {
                    _i18nDelegate = new I18nDelegate(Locale('zh', 'CN'));
                    widget.callback();
                  });
                },
              ),
            ],
          )
        ],
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  i18nstr: ^0.0.1

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

We analyzed this package on Jul 10, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform linux

Because of import path [package:i18nstr/i18nstr.dart] that declares support for platforms: android, ios

Package does not support Flutter platform macos

Because of import path [package:i18nstr/i18nstr.dart] that declares support for platforms: android, ios

Package does not support Flutter platform web

Because of import path [package:i18nstr/i18nstr.dart] that declares support for platforms: android, ios

Package does not support Flutter platform windows

Because of import path [package:i18nstr/i18nstr.dart] that declares support for platforms: android, ios

Package not compatible with SDK dart

because of import path [i18nstr] that is in a package requiring null.

Health suggestions

Format lib/i18nstr.dart.

Run flutter format to format lib/i18nstr.dart.

Maintenance issues and suggestions

The description contains too many non-ASCII characters. (-20 points)

The site uses English as its primary language. The value of the description field in your package's pubspec.yaml field should primarily contain characters used in English.

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

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.7.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.2
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test