feat(i18n): add i18n delegate + example
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
fc9812fc1f
commit
3a59230bce
@ -15,6 +15,7 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
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<String>(
|
||||
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})),
|
||||
),
|
||||
),
|
||||
);
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -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/
|
||||
- assets/
|
||||
|
@ -16,4 +16,5 @@
|
||||
|
||||
export 'enums/format.dart';
|
||||
export 'exceptions/exceptions.dart';
|
||||
export 'extensions/build_context_extension.dart';
|
||||
export 'utils/utils.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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
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<I18n>(this, I18n);
|
||||
if (i18n == null) {
|
||||
throw NotLoadedException();
|
||||
}
|
||||
|
||||
return i18n;
|
||||
}
|
||||
}
|
51
packages/wyatt_i18n/lib/src/presentation/i18n_delegate.dart
Normal file
51
packages/wyatt_i18n/lib/src/presentation/i18n_delegate.dart
Normal file
@ -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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
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<I18n> {
|
||||
/// {@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<I18n> load(Locale locale) async {
|
||||
await repository.load(locale: locale.languageCode);
|
||||
|
||||
return I18n(i18nRepository: repository);
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldReload(I18nDelegate old) =>
|
||||
currentLocale != null || currentLocale != old.currentLocale;
|
||||
}
|
@ -17,3 +17,4 @@
|
||||
export 'core/core.dart';
|
||||
export 'data/data.dart';
|
||||
export 'domain/domain.dart';
|
||||
export 'presentation/i18n_delegate.dart';
|
||||
|
Loading…
x
Reference in New Issue
Block a user