Skip to content

Commit

Permalink
Local example
Browse files Browse the repository at this point in the history
  • Loading branch information
Matteo Pietro Dazzi committed Jul 16, 2020
1 parent ad5137a commit a5bb434
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 2 deletions.
1 change: 1 addition & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
build/

.flutter-plugins
.flutter-plugins-dependencies
73 changes: 73 additions & 0 deletions example/lib/local_example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n_delegate.dart';
import 'package:flutter_i18n/loaders/decoders/json_decode_strategy.dart';
import 'package:flutter_i18n/loaders/file_translation_loader.dart';
import 'package:flutter_i18n/loaders/local_translation_loader.dart';
import 'package:flutter_i18n/widgets/I18nText.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
final basePath = await findBasePath();
await downloadTranslationContent(basePath);
final flutterI18nDelegate = FlutterI18nDelegate(
translationLoader: LocalTranslationLoader(
basePath: basePath,
decodeStrategies: [JsonDecodeStrategy()],
),
);

runApp(Test(flutterI18nDelegate));
}

Future<String> findBasePath() async {
final appDocDir = await getApplicationDocumentsDirectory();
return '${appDocDir.path}/langs';
}

Future downloadTranslationContent(final String basePath) async {
final file = File('$basePath/en.json');
final translationContentResponse = await http
.get('https://opensource.adobe.com/Spry/data/json/object-02.js');
await file.create(recursive: true);
await file.writeAsString(translationContentResponse.body, flush: true);
}

class Test extends StatefulWidget {
Test(this.flutterI18nDelegate);

final FlutterI18nDelegate flutterI18nDelegate;

@override
_TestState createState() => _TestState();
}

class _TestState extends State<Test> {
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: [
widget.flutterI18nDelegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
home: Builder(
builder: (context) => Scaffold(
appBar: AppBar(title: Text('Test')),
body: Column(children: <Widget>[
Container(
color: Colors.blue,
padding: const EdgeInsets.all(40),
child: I18nText('type'),
),
]),
),
),
);
}
}
8 changes: 8 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'basic_example.dart' as basicExample;
import 'namespace_example.dart' as namespaceExample;
import 'network_example.dart' as networkExample;
import 'local_example.dart' as localeExample;

Future main() async {
WidgetsFlutterBinding.ensureInitialized();
Expand Down Expand Up @@ -48,6 +49,13 @@ class MyApp extends StatelessWidget {
},
child: Text("Run `namespace` example"),
),
RaisedButton(
key: Key('localeExample'),
onPressed: () {
localeExample.main();
},
child: Text("Run `locale` example"),
)
],
));
})));
Expand Down
47 changes: 45 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.16.3"
version: "0.17.0"
flutter_localizations:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -146,7 +146,7 @@ packages:
source: hosted
version: "0.14.0+3"
http:
dependency: transitive
dependency: "direct main"
description:
name: http
url: "https://pub.dartlang.org"
Expand Down Expand Up @@ -292,6 +292,34 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.4"
path_provider:
dependency: "direct main"
description:
name: path_provider
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.11"
path_provider_linux:
dependency: transitive
description:
name: path_provider_linux
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.1+2"
path_provider_macos:
dependency: transitive
description:
name: path_provider_macos
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.4+3"
path_provider_platform_interface:
dependency: transitive
description:
name: path_provider_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
pedantic:
dependency: transitive
description:
Expand All @@ -313,6 +341,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.1"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
pool:
dependency: transitive
description:
Expand Down Expand Up @@ -507,6 +542,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.7.3"
xdg_directories:
dependency: transitive
description:
name: xdg_directories
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.0"
xml:
dependency: transitive
description:
Expand All @@ -530,3 +572,4 @@ packages:
version: "2.2.1"
sdks:
dart: ">=2.7.0 <3.0.0"
flutter: ">=1.12.13+hotfix.5 <2.0.0"
2 changes: 2 additions & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ dependencies:
flutter_i18n:
path: ../
cupertino_icons: ^0.1.2
http:
path_provider:

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit a5bb434

Please sign in to comment.