diff --git a/packages/wyatt_i18n/example/lib/main.dart b/packages/wyatt_i18n/example/lib/main.dart index 82ac6a5b..d5b685b0 100644 --- a/packages/wyatt_i18n/example/lib/main.dart +++ b/packages/wyatt_i18n/example/lib/main.dart @@ -15,6 +15,7 @@ // along with this program. If not, see . import 'package:flutter/material.dart'; +import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:wyatt_i18n/wyatt_i18n.dart'; @@ -28,51 +29,36 @@ class App extends StatelessWidget { static const String title = 'Wyatt i18n Example'; @override - Widget build(BuildContext context) => MaterialApp( - title: title, - theme: ThemeData( - primarySwatch: Colors.blue, + Widget build(BuildContext context) { + final I18nDataSource dataSource = NetworkDataSourceImpl( + baseUri: Uri.parse( + 'https://git.wyatt-studio.fr/Wyatt-FOSS/wyatt-packages/raw/commit/75f561a19e0484e67e511dbf29601ec5f58544aa/packages/wyatt_i18n/example/assets/', + ), + ); + + final I18nRepository repository = + I18nRepositoryImpl(dataSource: dataSource); + + return MaterialApp( + title: title, + theme: ThemeData( + primarySwatch: Colors.blue, + ), + localizationsDelegates: [ + I18nDelegate(repository: repository), + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate + ], + home: Scaffold( + appBar: AppBar( + title: const Text(title), ), - home: Scaffold( - appBar: AppBar( - title: const Text(title), - ), - body: Center( - child: FutureBuilder( - future: Future(() async { - // test - const I18nDataSource dataSource = AssetsFileDataSourceImpl(); - - final I18nRepository repository = - I18nRepositoryImpl(dataSource: dataSource); - - final test1 = (await repository.load(locale: 'en')).ok; - // final test2 = (await repository.loadFrom( - // Uri.parse('assets/i18n.en.yaml'), - // parser: const YamlParser(), - // )) - // .ok; - - // print(test1?.locale); - // print(test1?.data); - // print(test1?.data['btnAddFile']); - - final text = repository.get('youHavePushed', { - 'count': 8, - }); - - print(text.ok); - - - // print(test2?.locale); - // print(test2?.data); - // print(test2?.data['btnAddFile']); - - return 'test'; - }), - builder: (context, snapshot) => const Text('WyattI18n'), - ), + body: Builder( + builder: (ctx) => Center( + child: Text(ctx.i18n('youHavePushed', {'count': 654})), ), ), - ); + ), + ); + } } diff --git a/packages/wyatt_i18n/example/pubspec.yaml b/packages/wyatt_i18n/example/pubspec.yaml index 65ff8685..fd74a367 100644 --- a/packages/wyatt_i18n/example/pubspec.yaml +++ b/packages/wyatt_i18n/example/pubspec.yaml @@ -1,15 +1,16 @@ name: wyatt_i18n_example description: A new Flutter project. version: 1.0.0 -publish_to: 'none' +publish_to: "none" environment: sdk: ">=2.19.0 <3.0.0" dependencies: - flutter: {sdk: flutter} + flutter: { sdk: flutter } + flutter_localizations: { sdk: flutter } wyatt_i18n: path: "../" dev_dependencies: - flutter_test: {sdk: flutter} + flutter_test: { sdk: flutter } wyatt_analysis: hosted: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub version: ^2.4.0 @@ -17,4 +18,4 @@ dev_dependencies: flutter: uses-material-design: true assets: - - assets/ \ No newline at end of file + - assets/ diff --git a/packages/wyatt_i18n/lib/src/core/core.dart b/packages/wyatt_i18n/lib/src/core/core.dart index 575e445b..0e6487e1 100644 --- a/packages/wyatt_i18n/lib/src/core/core.dart +++ b/packages/wyatt_i18n/lib/src/core/core.dart @@ -16,4 +16,5 @@ export 'enums/format.dart'; export 'exceptions/exceptions.dart'; +export 'extensions/build_context_extension.dart'; export 'utils/utils.dart'; diff --git a/packages/wyatt_i18n/lib/src/core/extensions/build_context_extension.dart b/packages/wyatt_i18n/lib/src/core/extensions/build_context_extension.dart new file mode 100644 index 00000000..007134fc --- /dev/null +++ b/packages/wyatt_i18n/lib/src/core/extensions/build_context_extension.dart @@ -0,0 +1,36 @@ +// Copyright (C) 2023 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import 'package:flutter/widgets.dart'; +import 'package:wyatt_i18n/wyatt_i18n.dart'; + +/// Provides extensions for [BuildContext]. +extension BuildContextExtension on BuildContext { + /// Returns the current locale of the app. + String get locale => Localizations.localeOf(this).languageCode; + + /// Returns the current [I18n] instance. + /// + /// Throws a [NotLoadedException] if the [I18n] instance is not loaded. + I18n get i18n { + final i18n = Localizations.of(this, I18n); + if (i18n == null) { + throw NotLoadedException(); + } + + return i18n; + } +} diff --git a/packages/wyatt_i18n/lib/src/presentation/i18n_delegate.dart b/packages/wyatt_i18n/lib/src/presentation/i18n_delegate.dart new file mode 100644 index 00000000..8eae1508 --- /dev/null +++ b/packages/wyatt_i18n/lib/src/presentation/i18n_delegate.dart @@ -0,0 +1,51 @@ +// Copyright (C) 2023 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import 'package:flutter/cupertino.dart'; +import 'package:wyatt_i18n/wyatt_i18n.dart'; + +/// {@template i18n_delegate} +/// A [LocalizationsDelegate] that loads the [I18n] instance. +/// {@endtemplate} +class I18nDelegate extends LocalizationsDelegate { + /// {@macro i18n_delegate} + I18nDelegate({ + required this.repository, + }); + + /// The [I18nRepository] that will be used to load the i18n file. + final I18nRepository repository; + + /// The current locale. + Locale? currentLocale; + + /// Since we are using the [I18nRepository] to load the i18n file, + /// we don't need to check if the locale is supported. + /// We can just return `true`. + @override + bool isSupported(Locale locale) => true; + + @override + Future load(Locale locale) async { + await repository.load(locale: locale.languageCode); + + return I18n(i18nRepository: repository); + } + + @override + bool shouldReload(I18nDelegate old) => + currentLocale != null || currentLocale != old.currentLocale; +} diff --git a/packages/wyatt_i18n/lib/src/src.dart b/packages/wyatt_i18n/lib/src/src.dart index 0839d5b4..c4e2497a 100644 --- a/packages/wyatt_i18n/lib/src/src.dart +++ b/packages/wyatt_i18n/lib/src/src.dart @@ -17,3 +17,4 @@ export 'core/core.dart'; export 'data/data.dart'; export 'domain/domain.dart'; +export 'presentation/i18n_delegate.dart';