https://github.com/terran-source/flutter-applocale
A Flutter plugin to enable support for internationalization (i18n) or different language with json files
https://github.com/terran-source/flutter-applocale
dart flutter flutter-plugin i18n internationalization language-support localization
Last synced: 3 months ago
JSON representation
A Flutter plugin to enable support for internationalization (i18n) or different language with json files
- Host: GitHub
- URL: https://github.com/terran-source/flutter-applocale
- Owner: Terran-Source
- License: mit
- Created: 2020-03-21T10:18:48.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-01-27T20:19:45.000Z (over 3 years ago)
- Last Synced: 2024-10-31T21:36:18.106Z (7 months ago)
- Topics: dart, flutter, flutter-plugin, i18n, internationalization, language-support, localization
- Language: Dart
- Homepage: https://pub.dev/packages/applocale
- Size: 1.13 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# applocale
A Flutter plugin to enable support for internationalization (i18n) or different language with json files.## Usage
A simple usage example:
### Project Structure
#### lang.json contents
```json
//en.json
{
"title": "Awesome!",
"hello": "Hello",
"message": "This is English!!!",
"subDetail": {
"greeting": "{hello} {name}!!!",
"runtimeText": "I have proof, you can replace {replacement}"
}
}//bn.json
{
"title": "অভূতপূর্ব!",
"hello": "নমস্কার",
"message": "ইহা বাংলা!!!",
"subDetail": {
"runtimeText": "আমি জানি যে {replacement}কে যে কোনও দিন চলে যেতে হবে।"
}
}
```#### Add the language directory as assets in pubspec.yaml
```yaml
# pubspec.yaml
# add dependencies
dependencies:
applocale:flutter:
# add the whole directory containing language json files as an asset
assets:
- i18n/```
#### Now the code
```dart
// main.dart
import 'package:flutter/material.dart';import 'package:applocale/applocale.dart';
import 'package:flutter_localizations/flutter_localizations.dart';// define supported Language lists
Map get _supportedLanguages => {
"en": "English",
"en_us": "English(USA)",
"bn": "Bengali",
};
String get _defaultLanguage => "en";
List get _getSupportedLanguages =>
_supportedLanguages.entries.map((l) => l.key).toList();void main(List args) => runApp(FlutterDemoApp());
class FlutterDemoApp extends StatefulWidget {
@override
_FlutterDemoApp createState() => _FlutterDemoApp();
}class _FlutterDemoApp extends State {
// initialize _localeDelegate
LocaleDelegate _localeDelegate = LocaleDelegate.init(
_getSupportedLanguages,
// * optional, if it's same as the first one in the supportedLanguages
defaultLanguage: _defaultLanguage,
);@override
void initState() {
super.initState();
}@override
Widget build(BuildContext context) => MaterialApp(
supportedLocales: _localeDelegate.supportedLocales, // Step I
localizationsDelegates: [
_localeDelegate, // Step II
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
title: 'Flutter Demo',
home: FlutterDemo(),
);
}class FlutterDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
// since LocaleDelegate is already initialized & ready
var appLocale = AppLocale.of(context); // Step III
// In case some additional values can be set now. This is an one time
// activity
appLocale.updateValue({'name': 'জয়ন্তী'});return Scaffold(
appBar: AppBar(
title: Text(appLocale.localValue('title')),
),
body: ListView(
children: [
Center(
child: Text(appLocale.localValue('subDetail.greeting')),
),
Center(
child: Text(appLocale.localValue(
'subDetail.runtimeText',
{'replacement': 'Individual'}, // runtime interpolation
)),
),
Center(
child: Text(appLocale.localValue('message')),
),
],
),
);
}
}
```### Project Structure
*App with English* > *Change system language* > *App with Bengali*

## Features and bugs
Please file feature requests and bugs at the [issue tracker][tracker].
[tracker]: https://github.com/Terran-Source/applocale/issues