trans_flutter 0.0.1
trans_flutter: ^0.0.1 copied to clipboard
Flutter package to provide easy translation of text in your app at compile time.
trans_flutter #
trans_flutter is a Flutter package designed to work alongside the fluttertrans CLI tool to facilitate the localization of Flutter applications. This package helps in managing and accessing translations generated by the CLI tool, making it easy to internationalize your Flutter app.
Features #
- Easy Integration: Seamlessly integrates with the
fluttertransCLI tool to handle translations. - Dynamic Localization: Provides utilities to dynamically change the app's locale and update the UI accordingly.
- Extension Methods: Adds convenient extension methods for translating strings and widgets.
Installation #
- Add
trans_flutterto yourpubspec.yaml:
dependencies:
flutter:
sdk: flutter
trans_flutter: ^1.0.0
- Ensure your
pubspec.yamlincludes theassets/translations/folder:
flutter:
assets:
- assets/translations/
-
Run
flutter pub getto install the package. -
Follow the instructions to install the fluttertrans CLI tool for generating translation files.
Usage #
Step 1: Generate Translations #
Just add .tr extension to your strings in your code and run the following command to generate translations:
example:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Translations',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Hello World'.tr), // This will translate the text 'Hello World'
),
body: Center(
child: Text('Welcome Message'.tr), // This will translate the text 'Welcome Message'
),
);
}
}
Run the following command in your terminal:
fluttertrans
For more information on how to use the cli, refer to the fluttertrans CLI tool.
Step 2: Load Translations in Your App #
Initialize the TranslateService
In your main.dart file, set the initial locale and load the translations:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await TranslateService.setLocale(const Locale('en')); // Set your initial locale
runApp(MyApp());
}
Using .tr for Strings
You can easily translate strings using the .tr extension method:
import 'package:flutter/material.dart';
import 'package:trans_flutter/trans_flutter.dart';
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Hello World'.tr), // This will translate the text 'Hello World'
),
body: Center(
child: Text('Welcome Message'.tr), // This will translate the text 'Welcome Message'
),
);
}
}
Using .translate with MaterialApp
To ensure the entire app rebuilds when the locale changes, use the .translate extension method with MaterialApp:
import 'package:flutter/material.dart';
import 'package:trans_flutter/trans_flutter.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await TranslateService.setLocale(const Locale('en')); // Set your initial locale
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Translations',
home: MyHomePage(),
).translate(); // Add the .translate extension method
}
}
Changing Locale at Runtime #
To change the locale dynamically, use the setLocale method:
import 'package:flutter/material.dart';
import 'package:trans_flutter/trans_flutter.dart';
class SettingsPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Settings'.tr),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
await TranslateService.setLocale(const Locale('es')); // Change to Spanish
},
child: Text('Change to Spanish'.tr),
),
),
);
}
}
Contributions #
Contributions are welcome! Feel free to open issues or submit pull requests on the GitHub repository.
License #
This project is licensed under the MIT License - see the LICENSE file for details.
trans_flutter makes it easy to internationalize your Flutter apps with minimal effort. With seamless integration and dynamic localization capabilities, you can ensure your app is ready for a global audience. Happy coding!