Multilingual
Multilingual is a powerful Flutter localization plugin that simplifies managing multiple languages in your app. It allows dynamic text translation, runtime language switching, and text replacement.
Features
- Dynamic Translation: Use
.trans()to easily translate text based on the current locale. - Custom Text Replacement: Replace specific text dynamically using
.trans({'key': 'value'});. - Key Existence Check: Verify if a specific key exists with
context.isTrans('key');. - Support for Multiple Languages: Seamlessly manage translations for multiple locales.
Usage
Basic Setup
Wrap your app with the Multilingual widget and define the supported languages.
Add yourWidget inside the builder of MultilingualChild.
Using Flutter's default MaterialApp:
class DefaultApp extends StatefulWidget {
const DefaultApp({super.key});
@override
State<DefaultApp> createState() => _DefaultAppState();
}
class _DefaultAppState extends State<DefaultApp> {
@override
Widget build(BuildContext context) {
return Multilingual(
languages: {
Locale('ko', 'KR'): {'lan': '한국어', 'text_button': '언어변경'},
Locale('en', 'US'): {'lan': 'English', 'text_button': 'Language change'},
Locale('ja', 'JP'): {'lan': '日本語', 'text_button': '言語変更'},
},
builder: (context) {
return MaterialApp(
localizationsDelegates: MultilingualController.localizationsDelegates,
supportedLocales: MultilingualController.supportedLocales,
locale: MultilingualController.locale,
home: MultilingualChild(
builder: (context) {
return HomeScreen();
}
),
);
}
);
}
}
Using MaterialApp.router provided by go_router:
class RouterApp extends StatefulWidget {
const RouterApp({super.key});
@override
State<RouterApp> createState() => _RouterAppState();
}
class _RouterAppState extends State<RouterApp> {
@override
Widget build(BuildContext context) {
return Multilingual(
languages: {
Locale('ko', 'KR'): {'lan': '한국어', 'text_button': '언어변경'},
Locale('en', 'US'): {'lan': 'English', 'text_button': 'Language change'},
Locale('ja', 'JP'): {'lan': '日本語', 'text_button': '言語変更'},
},
builder: (context) {
return MaterialApp.router(
routerConfig: _router,
localizationsDelegates: MultilingualController.localizationsDelegates,
supportedLocales: MultilingualController.supportedLocales,
locale: MultilingualController.locale,
);
}
);
}
}
final _router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => MultilingualChild(
builder: (context) => HomeScreen()
),
),
],
);
Your Widget:
class RouterApp extends StatefulWidget {
const RouterApp({super.key});
@override
State<RouterApp> createState() => _RouterAppState();
}
class _RouterAppState extends State<RouterApp> {
@override
Widget build(BuildContext context) {
return Multilingual(
languages: {
Locale('ko', 'KR'): {'lan': '한국어', 'text_button': '언어변경'},
Locale('en', 'US'): {'lan': 'English', 'text_button': 'Language change'},
Locale('ja', 'JP'): {'lan': '日本語', 'text_button': '言語変更'},
},
builder: (context) {
return MaterialApp.router(
routerConfig: _router,
localizationsDelegates: MultilingualController.localizationsDelegates,
supportedLocales: MultilingualController.supportedLocales,
locale: MultilingualController.locale,
);
}
);
}
}
final _router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => MultilingualChild(
builder: (context) => HomeScreen()
),
),
],
);
Note: Do not use `const` with `yourWidget`. Using `const` will prevent the screen from updating.
Creating Your App
Pass the MultilingualController to your app and use .trans() for translations:
Checking Key Existence
Use context.isTrans('key') to check if a translation key exists:
if (context.isTrans('key')) {
print('Translation key exists!');
} else {
print('Translation key does not exist.');
}
Custom Text Replacement
Replace specific text dynamically:
Text('Original text'.trans({'Original text': 'Replaced text'}));
Example Output
- Default Language: Korean (
ko_KR)- Button Text:
언어변경
- Button Text:
- Switch to English (
en_US)- Button Text:
Language change
- Button Text:
- Switch to Japanese (
ja_JP)- Button Text:
言語変更
- Button Text:
Contributing
Feel free to contribute to Multilingual by submitting issues or pull requests on the repository.
License
This project is licensed under the MIT License.