• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

davidsdearaujo/localization: Localization package

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

davidsdearaujo/localization

开源软件地址(OpenSource Url):

https://github.com/davidsdearaujo/localization

开源编程语言(OpenSource Language):

Dart 38.2%

开源软件介绍(OpenSource Introduction):

Localization

Package to simplify in-app translation.

Install

Use the Localization package together with **flutter_localization.

Add in your pubspec:

dependencies:
  flutter_localizations: 
    sdk: flutter
  localization: <last-version>

flutter:

  # json files directory
  assets:
    - lib/i18n/

Now, add the delegate in MaterialApp or CupertinoApp and define a path where the translation json files will be:

 @override
  Widget build(BuildContext context) {
    // set json file directory
    // default value is 'lib/i18n'
    LocalJsonLocalization.delegate.directory = 'lib/i18n';

    return MaterialApp(
      localizationsDelegates: [
        // delegate from flutter_localization
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        // delegate from localization package.
        LocalJsonLocalization.delegate,
      ],
      home: HomePage(),
    );
  }

Json files

The json file pattern must have the name of the locale and its content must be a json of key and value ONLY. Create the files in the directory configured (LocalJsonLocalization.delegate.directory):

lib/i18n/en_US.json
lib/i18n/es_ES.json
lib/i18n/pt_BR.json

See an example of a translation json file:

en_US.json

{
  "welcome-text": "This text is in english"
}

es_ES.json

{
  "welcome-text": "Este texto esta en español"
}

pt_BR.json

{
  "welcome-text": "Este texto está em português"
}

Using

For convenience, the i18n() method has been added to the String class via extension. So just add the translation json file key as string and use i18n() method to bring up the translation.

String text = 'welcome-text'.i18n();
print(text) // prints 'This text is in english'

We can also work with arguments for translation. Use %s notion:

{
  "welcome-text": "Welcome, %s"
}
String text = 'welcome-text'.i18n(['Peter']);
print(text); // Welcome, Peter

The %s notation can also be retrieved positionally. Just use %s0, %s1, %s2...

THAT`S IT!

Additional settings

After installation, the Localization package is fully integrated into Flutter and can reflect changes made natively by the SDK. Here are some examples of configurations that we will be able to do directly in MaterialApp or CupertinoApp.

Add supported languages

This setting is important to tell Flutter which languages your app is prepared to work with. We can do this by simply adding the Locale in the supportedLocales property:

return MaterialApp(
  supportedLocales: [
    Locale('en', 'US'),
    Locale('es', 'ES'),
    Locale('pt', 'BR'),
  ],
  ...
);

Locale resolution

Often we will not have to make some decisions about what to do when the device language is not supported by our app or work with a different translation for different countries (eg pt_BR(Brazil) and pt_PT(Portugal). For these and other decisions we can use the dsdsk property to create a resolution strategy:

return MaterialApp(
  localeResolutionCallback: (locale, supportedLocales) {
      if (supportedLocales.contains(locale)) {
        return locale;
      }

      // define pt_BR as default when de language code is 'pt'
      if (locale?.languageCode == 'pt') {
        return Locale('pt', 'BR');
      }

      // default language
      return Locale('en', 'US');
  },
  ...
);

Localization UI

localization-ui.png

We have an application to help you configure your translation keys. The project is also open-source, so be fine if you want to help it evolve!

Features and bugs

The Segmented State Standard is constantly growing. Let us know what you think of all this. If you agree, give a Star in that repository representing that you are signing and consenting to the proposed standard.

Questions and Problems

The issues channel is open for questions, to report problems and suggestions, do not hesitate to use this communication channel.

LET'S BE REFERENCES TOGETHER




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap