项目作者: Terran-Source

项目描述 :
A Flutter plugin to enable support for internationalization (i18n) or different language with json files
高级语言: Dart
项目地址: git://github.com/Terran-Source/flutter-applocale.git
创建时间: 2020-03-21T10:18:48Z
项目社区:https://github.com/Terran-Source/flutter-applocale

开源协议:MIT License

下载


applocale

A Flutter plugin to enable support for internationalization (i18n) or different language with json files.

Usage

A simple usage example:

Project Structure

project_structure

lang.json contents

  1. //en.json
  2. {
  3. "title": "Awesome!",
  4. "hello": "Hello",
  5. "message": "This is English!!!",
  6. "subDetail": {
  7. "greeting": "{hello} {name}!!!",
  8. "runtimeText": "I have proof, you can replace {replacement}"
  9. }
  10. }
  11. //bn.json
  12. {
  13. "title": "অভূতপূর্ব!",
  14. "hello": "নমস্কার",
  15. "message": "ইহা বাংলা!!!",
  16. "subDetail": {
  17. "runtimeText": "আমি জানি যে {replacement}কে যে কোনও দিন চলে যেতে হবে।"
  18. }
  19. }

Add the language directory as assets in pubspec.yaml

  1. # pubspec.yaml
  2. # add dependencies
  3. dependencies:
  4. applocale: <latest-version>
  5. flutter:
  6. # add the whole directory containing language json files as an asset
  7. assets:
  8. - i18n/

Now the code

  1. // main.dart
  2. import 'package:flutter/material.dart';
  3. import 'package:applocale/applocale.dart';
  4. import 'package:flutter_localizations/flutter_localizations.dart';
  5. // define supported Language lists
  6. Map<String, String> get _supportedLanguages => {
  7. "en": "English",
  8. "en_us": "English(USA)",
  9. "bn": "Bengali",
  10. };
  11. String get _defaultLanguage => "en";
  12. List<String> get _getSupportedLanguages =>
  13. _supportedLanguages.entries.map((l) => l.key).toList();
  14. void main(List<String> args) => runApp(FlutterDemoApp());
  15. class FlutterDemoApp extends StatefulWidget {
  16. @override
  17. _FlutterDemoApp createState() => _FlutterDemoApp();
  18. }
  19. class _FlutterDemoApp extends State<FlutterDemoApp> {
  20. // initialize _localeDelegate
  21. LocaleDelegate _localeDelegate = LocaleDelegate.init(
  22. _getSupportedLanguages,
  23. // * optional, if it's same as the first one in the supportedLanguages
  24. defaultLanguage: _defaultLanguage,
  25. );
  26. @override
  27. void initState() {
  28. super.initState();
  29. }
  30. @override
  31. Widget build(BuildContext context) => MaterialApp(
  32. supportedLocales: _localeDelegate.supportedLocales, // Step I
  33. localizationsDelegates: [
  34. _localeDelegate, // Step II
  35. GlobalMaterialLocalizations.delegate,
  36. GlobalWidgetsLocalizations.delegate
  37. ],
  38. title: 'Flutter Demo',
  39. home: FlutterDemo(),
  40. );
  41. }
  42. class FlutterDemo extends StatelessWidget {
  43. @override
  44. Widget build(BuildContext context) {
  45. // since LocaleDelegate is already initialized & ready
  46. var appLocale = AppLocale.of(context); // Step III
  47. // In case some additional values can be set now. This is an one time
  48. // activity
  49. appLocale.updateValue({'name': 'জয়ন্তী'});
  50. return Scaffold(
  51. appBar: AppBar(
  52. title: Text(appLocale.localValue('title')),
  53. ),
  54. body: ListView(
  55. children: <Widget>[
  56. Center(
  57. child: Text(appLocale.localValue('subDetail.greeting')),
  58. ),
  59. Center(
  60. child: Text(appLocale.localValue(
  61. 'subDetail.runtimeText',
  62. {'replacement': 'Individual'}, // runtime interpolation
  63. )),
  64. ),
  65. Center(
  66. child: Text(appLocale.localValue('message')),
  67. ),
  68. ],
  69. ),
  70. );
  71. }
  72. }

Project Structure

App with EnglishChange system languageApp with Bengali

App with English > Change system language > App with Bengali

App with English

Features and bugs

Please file feature requests and bugs at the issue tracker.