localize_and_translate 4.1.1
localize_and_translate: ^4.1.1 copied to clipboard
Flutter localization in easy steps, simple ways to localize and translate your app
localize_and_translate #
Flutter localization in easy steps
Screenshots #
Tutorial #
Video #
- Arabic : https://www.youtube.com/watch?v=nfDYussovfQ
- English : soon..
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/en.json
- assets/lang/ar.json
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 withLocalizedApp()
- 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);
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: Drawer(),
appBar: AppBar(
title: Text('appTitle'.tr()),
// centerTitle: true,
),
body: Container(
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
SizedBox(height: 50),
Text(
'textArea'.tr(),
textAlign: TextAlign.center,
style: TextStyle(fontSize: 35),
),
OutlineButton(
onPressed: () {
translator.setNewLanguage(
context,
newLanguage: translator.currentLanguage == 'ar' ? 'en' : 'ar',
remember: true,
restart: true,
);
},
child: Text('buttonTitle'.tr()),
),
],
),
),
);
}
}
❤️ this #
Project Created & Maintained By #
#
Software Engineer | In ❤️ with Flutter
Note : All Contibutions Are Welcomed #
Contributions #
translate()
accept keys : by mo-ah-dawood- Change template from plugin to package : by mo-ah-dawood