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

msayed-net/localize_and_translate: Flutter localization in easy steps

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

开源软件名称(OpenSource Name):

msayed-net/localize_and_translate

开源软件地址(OpenSource Url):

https://github.com/msayed-net/localize_and_translate

开源编程语言(OpenSource Language):

Dart 81.4%

开源软件介绍(OpenSource Introduction):

localize_and_translate

Flutter localization in easy steps

Pub Example

Fork Star Watch

Screenshots

screenshot 1 screenshot 2

Tutorial

Video

Methods

Method Job
init() initialize things, before runApp()
'word'.tr() word translation - string extension
translate('word') word translation
translate('word',{"key":"value"}) word translation with replacement arguments
setNewLanguage(context,newLanguage:'en',restart: true, remember: true,) change language
isDirectionRTL() is Direction RTL check
currentLanguage Active language code
locale Active Locale
locals() Locales list
delegates Localization Delegates

Installation

  • add .json translation files as assets
  • For example : 'assets/lang/ar.json' | 'assets/lang/en.json'
  • structure should look like
{
    "appTitle": "تطبيق تجريبى", 
    "buttonTitle": "English", 
    "textArea": "هذا مجرد نموذج للتأكد من اداء الأداة"
}
  • define them as assets in pubspec.yaml
flutter:
  assets:
    - assets/lang/

Initialization

  • Add imports to main.dart
  • Make main() async and do the following
  • Ensure flutter activated WidgetsFlutterBinding.ensureInitialized()
  • Initialize await translator.init(); with neccassry parameters
  • Inside runApp() wrap entry class with LocalizedApp()
  • Note : make sure you define it's child into different place "NOT INSIDE"
import 'package:flutter/material.dart';
import 'package:localize_and_translate/localize_and_translate.dart';

main() async {
  // if your flutter > 1.7.8 :  ensure flutter activated
  WidgetsFlutterBinding.ensureInitialized();

  await translator.init(
    localeType: LocalizationDefaultType.device,
    languagesList: <String>['ar', 'en'],
    assetsDirectory: 'assets/lang/',
  );

  runApp(
    LocalizedApp(
      child: MyApp(),
    ),
  );
}
  • LocalizedApp() child example -> MaterialApp()
class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Home(),
      localizationsDelegates: translator.delegates, // Android + iOS Delegates
      locale: translator.locale, // Active locale
      supportedLocales: translator.locals(), // Locals list
    );
  }
}

Usage

  • use translate('appTitle')
  • use setNewLanguage(context, newLanguage: 'ar', remember: true, restart: true);

Example

Example

Contributors

Contributors List




鲜花

握手

雷人

路过

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

请发表评论

全部评论

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

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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