app_local 1.1.0
app_local: ^1.1.0 copied to clipboard
A Flutter package for easy app localization using JSON files, supporting device default language detection and dynamic language switching without app restart
app_local #
A lightweight, powerful Flutter package that enables instant app localization with dynamic language switching, persistent settings, and native device language support - all with minimal setup required.
Features #
- π Easy app localization
- π Dynamic locale switching
- πΎ Automatic locale persistence
- π± Phone default language support
- π Simple integration with
LocaleText
widget - β‘ Minimal setup required
Example #
Getting Started #
1. Setup Assets #
- Create an
assets/lang
folder in your project root - Add your locale JSON files (e.g.,
en.json
,ar.json
)
Example locale files:
// en.json
{
"welcome": "Welcome to the App",
"change_lang": "Change the language"
}
// ar.json
{
"welcome": "Ω
Ψ±ΨΨ¨Ψ§ Ψ¨Ω ΩΩ Ψ§ΩΨͺΨ·Ψ¨ΩΩ",
"change_lang": "ΨͺΨΊΩΩΨ± ΩΨΊΨ© Ψ§ΩΨͺΨ·Ψ¨ΩΩ"
}
2. Add Dependencies #
dependencies:
flutter:
sdk: flutter
app_local: ^latest_version
# Add assets path
flutter:
uses-material-design: true
assets:
- assets/lang/
3. Initialize #
Add this to your main.dart
:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Locales.init(
localeNames: ['en', 'ar'], // Your supported locales
localPath: "assets/lang/", // Path to your locale files
phoneLocale: true, // Use phone's default locale
);
runApp(MyApp());
}
Wrap your MaterialApp
with LocaleBuilder
:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return LocaleBuilder(
builder: (locale) => MaterialApp(
title: 'App Local',
localizationsDelegates: Locales.delegates,
supportedLocales: Locales.supportedLocales,
locale: locale,
home: HomeScreen(),
),
);
}
}
Usage #
Display Translated Text #
Using widget:
LocaleText('welcome');
Using string:
// Using static method
Locales.string(context, 'welcome');
// Using extension
context.localeString('welcome');
Change Locale #
// Using static method
Locales.change(context, 'ar');
// Using extension
context.changeLocale('ar');
// Switch to device locale
Locales.change(context, 'auto');
Get Current Locale #
// Using static method
Locales.currentLocale(context);
// Using extension
context.currentLocale;
iOS #
Add the following to your Info.plist
file if you want to support device default language:
<key>CFBundleLocalizations</key>
<array>
// Add your supported locales here
<string>en</string>
<string>ar</string>
</array>
Platform Support #
Platform | Support |
---|---|
Android | β |
iOS | β |
macOS | β |
Web | β |
Windows | β |
Linux | β |
Default Native Language #
Platform | Support |
---|---|
Android | β |
iOS | β |
macOS | β |
Web | π« |
Windows | π« |
Linux | π« |
soon support web, windows, linux