feat(i18n): add i18n delegate + example
continuous-integration/drone/push Build is failing

This commit was merged in pull request #164.
This commit is contained in:
2023-02-28 15:06:49 +01:00
parent fc9812fc1f
commit 3a59230bce
6 changed files with 124 additions and 48 deletions
@@ -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;
}
}
@@ -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;
}
+1
View File
@@ -17,3 +17,4 @@
export 'core/core.dart';
export 'data/data.dart';
export 'domain/domain.dart';
export 'presentation/i18n_delegate.dart';