flutter_pipwave_localization 0.0.1+2
flutter_pipwave_localization: ^0.0.1+2 copied to clipboard
Powerful and flexible localization package for Pipwave Flutter projects, featuring ICU message formatting, runtime locale switching, asset and dynamic translation file support.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_pipwave_localization/flutter_pipwave_localization.dart';
void main() {
runApp(const LocalizationExampleApp());
}
class LocalizationExampleApp extends StatelessWidget {
const LocalizationExampleApp({super.key});
@override
Widget build(BuildContext context) {
return LocalizationProvider(
startLocale: const Locale('en'),
assetPath: 'assets/translations',
downloadedPath: 'example/translations',
supportedLocales: const [Locale('en'), Locale('es'), Locale('fr')],
child: const MyApp(),
);
}
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'PW Localization Example',
theme: ThemeData(primarySwatch: Colors.blue, useMaterial3: true),
home: const HomePage(),
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
CustomLocalizationDelegate(
locale: const Locale('en'),
supportedLocales: const [Locale('en'), Locale('es'), Locale('fr')],
),
],
supportedLocales: const [Locale('en'), Locale('es'), Locale('fr')],
locale: const Locale('en'),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _itemCount = 0;
void _incrementCounter() {
setState(() {
_itemCount++;
});
}
void _switchLanguage(String languageCode) {
// Access the localization service to change locale
LocalizationProvider.of(context)
.initialize(
assetPath: 'assets/translations',
downloadedPath: 'translations',
defaultLanguage: 'en',
locale: Locale(languageCode),
)
.then((value) {
setState(() {
_itemCount = 0;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(context.tr('app.title')),
actions: [
PopupMenuButton<String>(
onSelected: _switchLanguage,
itemBuilder: (context) => [
const PopupMenuItem(value: 'en', child: Text('English')),
const PopupMenuItem(value: 'es', child: Text('Español')),
const PopupMenuItem(value: 'fr', child: Text('Français')),
],
),
],
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
context.tr('welcome.message', args: {'name': 'John'}),
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 20),
Text(
context.tr('items.countInLine', args: {'count': _itemCount}),
style: Theme.of(context).textTheme.headlineMedium,
),
Text(
context.tr('items.countInObject', args: {'count': _itemCount}),
style: Theme.of(context).textTheme.headlineMedium,
),
const SizedBox(height: 20),
Text(
context.tr('common.loading'),
style: Theme.of(context).textTheme.bodyLarge,
),
const SizedBox(height: 40),
ElevatedButton(
onPressed: _incrementCounter,
child: Text(context.tr('actions.increment')),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
// Demonstrate downloading language files (if server is available)
LocalizationService.instance
.downloadLanguageFiles('https://api.example.com/translations', [
'en',
'es',
'fr',
])
.catchError((error) {
// Handle error gracefully - in a real app you'd show user feedback
debugPrint('Download failed: $error');
});
},
tooltip: context.tr('actions.download'),
child: const Icon(Icons.download),
),
);
}
}