my_lang 1.0.0+1
my_lang: ^1.0.0+1 copied to clipboard
A library that provides flexible and easy-to-use localization support for Flutter applications.
my_lang
is a library that provides flexible and easy-to-use localization support for Flutter applications.
Features #
- Multi-language support using JSON files.
- Easy to integrate and use.
- Dynamic language switching in the app.
- Automatically reads JSON files and generates Dart files without manually entering each key.
Installation #
Add the following to your pubspec.yaml
:
my_lang: ^latest_version
flutter:
assets:
- assets/i18n/
JSON Language File Structure #
Create JSON files inside the assets/i18n/
directory:
en.json
{
"welcomeBack": "Welcome Back",
"welcomeBackNameApp": "Welcome @nameUser Back @nameApp"
}
vi.json
{
"welcomeBack": "Chào mừng trở lại",
"welcomeBackNameApp": "Chào mừng @nameUser trở lại @nameApp"
}
Initializing the Library #
import 'package:my_lang/my_lang.dart';
import 'package:flutter/widgets.dart';
MyLang myLang = MyLang();
const listLocale = [
Locale('en'),
Locale('vi'),
];
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await myLang.setUp(listLocale: listLocale);
runApp(MyApp());
}
Creating the interpreter.dart
File #
Create the interpreter.dart
file containing the OurLang
class:
import 'package:my_lang/my_lang.dart';
extension OurLang on MyLang {
String get welcomeBack => translate('welcomeBack');
String? welcomeBackNameApp(String nameUser, String nameApp) =>
translate('welcomeBackNameApp', params: {
'nameUser': nameUser,
'nameApp': nameApp,
});
}
Usage #
print(myLang.welcomeBack); // "Welcome Back" or "Chào mừng trở lại"
print(myLang.welcomeBackNameApp("John", "MyApp"));
Changing Language in the App #
myLang.loadFileJson(locale: myLang.locale.languageCode == 'en'
? const Locale("vi")
: const Locale("en"));
Generating OurLang
Automatically #
Run the following command to automatically generate interpreter.dart
:
dart pub global activate my_lang
my_lang
my_lang -i assets/i18n/en.json -o lib/interpreter.dart -c YourLang
💡 Tip: Quickly copy a file path using these shortcuts on Android Studio:
- MacBook:
Command (⌘) + Option (⌥) + C
- Windows:
Ctrl + Shift + C
Two languages at the same time #
import 'package:my_lang/my_lang.dart';
import 'package:flutter/widgets.dart';
MyLang myLang = MyLang();
MyLang myLang2 = MyLang();
const listLocale = [
Locale('en'),
Locale('vi'),
];
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await myLang.setUp(listLocale: listLocale);
await myLang2.setUp(
listLocale: listLocale,
keySaveLocale: "otherKeyForSharedPreference"
);
runApp(MyApp());
}
Note:
ThekeySaveLocale
must be different for each instance.
This key is used to store the selected language inSharedPreferences
.
Usage example: #
You can use both language instances as normal:
print(myLang.welcomeBack);
print(myLang2.welcomeBack);
Contribution #
If you have any suggestions or find any issues, feel free to open an issue or submit a pull request on GitHub. If you want to know what i do in package, checking my document here https://wong-coupon.gitbook.io/flutter/ui/multi-language