AutoLangx
AutoLangx is a powerful Flutter package that automatically translates all text in your app dynamically using Google Cloud Translation API - just like having Google Translate built into your app!
No .arb files, no .json files, no complicated setup. Just add .autoTr() to any text and it becomes translatable instantly.
✨ Features
- 🌍 Dynamic Real-time Translation - Translate text at runtime using Google Cloud Translation API
- 🚀 Zero Configuration - No .arb, .json, or .env files needed
- 💾 Smart Caching - Automatic local caching with SharedPreferences to reduce API calls
- 📱 Offline Support - Works offline for previously cached translations
- 🔄 Instant Language Switching - Change app language with a single function call
- 🌐 Unlimited Languages - Support for all languages available in Google Translate
- ⚡ Simple API - Just use
.autoTr()extension on any string - 🎯 No State Management Required - Uses ChangeNotifier for reactivity
📦 Installation
Add this to your package's pubspec.yaml file:
dependencies:
auto_langx: ^1.0.0
Then run:
flutter pub get
🔑 Setup Google Cloud Translation API
- Go to Google Cloud Console
- Create a new project or select existing one
- Enable the Cloud Translation API
- Go to APIs & Services > Credentials
- Click Create Credentials > API Key
- Copy your API key
🚀 Usage
1. Initialize AutoLangx
Initialize the package in your main() function:
import 'package:flutter/material.dart';
import 'package:auto_langx/auto_langx.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize with your Google API key
await AutoLangxController.instance.init("YOUR_GOOGLE_API_KEY");
runApp(MyApp());
}
2. Use .autoTr() Extension
Simply add .autoTr() to any string to make it translatable:
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: "Welcome".autoTr(),
),
body: Column(
children: [
"Hello, how are you?".autoTr(),
"This text will be translated automatically".autoTr(
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
],
),
);
}
}
3. Change Language
Switch the entire app language with a single function call:
// Change to Urdu
await AutoLangxController.instance.changeLanguage('ur');
// Change to Spanish
await AutoLangxController.instance.changeLanguage('es');
// Change to French
await AutoLangxController.instance.changeLanguage('fr');
// Back to English
await AutoLangxController.instance.changeLanguage('en');
All .autoTr() widgets automatically update when language changes!
4. Language Selector Example
ElevatedButton(
onPressed: () async {
await AutoLangxController.instance.changeLanguage('ur');
},
child: Text('اردو'),
),
ElevatedButton(
onPressed: () async {
await AutoLangxController.instance.changeLanguage('en');
},
child: Text('English'),
),
5. Clear Cache (Optional)
// Clear all cached translations
await AutoLangxController.instance.clearCache();
🌍 Supported Languages
All languages supported by Google Cloud Translation API, including:
- English (en)
- Urdu (ur)
- Arabic (ar)
- Spanish (es)
- French (fr)
- German (de)
- Chinese (zh)
- Japanese (ja)
- Korean (ko)
- Hindi (hi)
- And 100+ more!
💡 How It Works
- First Call: Text is sent to Google Cloud Translation API
- Caching: Translation is automatically cached locally using SharedPreferences
- Subsequent Calls: Cached translation is used (works offline!)
- Language Change: All widgets with
.autoTr()automatically rebuild with new translations - Persistence: Last selected language is saved and restored on app restart
📝 License
MIT License - see the LICENSE file for details
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
🐛 Issues
Found a bug? Please open an issue
Made with ❤️ for the Flutter community
Libraries
- auto_langx
- AutoLangx - Automatic Translation Package for Flutter