diff --git a/packages/wyatt_i18n/lib/src/core/exceptions/exceptions.dart b/packages/wyatt_i18n/lib/src/core/exceptions/exceptions.dart
index b91aaf48..7947b0a3 100644
--- a/packages/wyatt_i18n/lib/src/core/exceptions/exceptions.dart
+++ b/packages/wyatt_i18n/lib/src/core/exceptions/exceptions.dart
@@ -58,7 +58,13 @@ class MalformedValueException extends ClientException {
: super('Key `$key` references a malformed value. ($value)');
}
+/// Exception thrown when the parser fails.
class ParserException extends ClientException {
ParserException(String message, StackTrace? stackTrace)
: super('$message\n\n$stackTrace');
}
+
+/// Exception thrown when the i18n is not loaded.
+class NotLoadedException extends ClientException {
+ NotLoadedException() : super('I18n not loaded.');
+}
diff --git a/packages/wyatt_i18n/lib/src/data/repositories/i18n_repository_impl.dart b/packages/wyatt_i18n/lib/src/data/repositories/i18n_repository_impl.dart
index 4026581b..400f2333 100644
--- a/packages/wyatt_i18n/lib/src/data/repositories/i18n_repository_impl.dart
+++ b/packages/wyatt_i18n/lib/src/data/repositories/i18n_repository_impl.dart
@@ -15,18 +15,27 @@
// along with this program. If not, see .
import 'package:wyatt_architecture/wyatt_architecture.dart';
-import 'package:wyatt_i18n/src/core/utils/i18n_parser.dart';
import 'package:wyatt_i18n/wyatt_i18n.dart';
import 'package:wyatt_type_utils/wyatt_type_utils.dart' hide Option;
+/// {@template i18n_repository_impl}
+/// The default implementation of [I18nRepository].
+/// {@endtemplate}
class I18nRepositoryImpl extends I18nRepository {
+ /// {@macro i18n_repository_impl}
I18nRepositoryImpl({
required this.dataSource,
}) : super();
+ /// The data source used to load the i18n file.
final I18nDataSource dataSource;
+
+ /// The current i18n instance.
I18n _i18n = const I18n.empty();
+ @override
+ I18n get i18n => _i18n;
+
Future _parse(
String content,
Parser> parser, {
@@ -129,4 +138,19 @@ class I18nRepositoryImpl extends I18nRepository {
throw KeyNotFoundException(key, arguments);
}
}
+
+ @override
+ Result getI18n() =>
+ Result.conditional(!_i18n.isEmpty, _i18n, NotLoadedException());
+
+ @override
+ Result getLocale() =>
+ Result.conditional(!_i18n.isEmpty, _i18n.locale, NotLoadedException());
+
+ @override
+ Result setI18n(I18n i18n) {
+ _i18n = i18n;
+
+ return const Ok(null);
+ }
}
diff --git a/packages/wyatt_i18n/lib/src/domain/entities/i18n.dart b/packages/wyatt_i18n/lib/src/domain/entities/i18n.dart
index 04fdbbf5..3ededd44 100644
--- a/packages/wyatt_i18n/lib/src/domain/entities/i18n.dart
+++ b/packages/wyatt_i18n/lib/src/domain/entities/i18n.dart
@@ -39,4 +39,6 @@ class I18n extends Entity {
bool containsKey(String key) => data.containsKey(key);
dynamic operator [](String key) => data[key];
+
+ bool get isEmpty => data.isEmpty && unparsedData.isEmpty && locale.isEmpty;
}
diff --git a/packages/wyatt_i18n/lib/src/domain/repositories/i18n_repository.dart b/packages/wyatt_i18n/lib/src/domain/repositories/i18n_repository.dart
index 7716713a..c5a1d837 100644
--- a/packages/wyatt_i18n/lib/src/domain/repositories/i18n_repository.dart
+++ b/packages/wyatt_i18n/lib/src/domain/repositories/i18n_repository.dart
@@ -19,10 +19,18 @@ import 'package:wyatt_i18n/src/core/utils/parser.dart';
import 'package:wyatt_i18n/src/domain/entities/i18n.dart';
import 'package:wyatt_type_utils/wyatt_type_utils.dart';
+/// {@template i18n_repository}
/// Base class for i18n repositories.
+///
+/// This class is used to manage i18n files.
+/// {@endtemplate}
abstract class I18nRepository extends BaseRepository {
+ /// {@macro i18n_repository}
const I18nRepository() : super();
+ /// The current i18n file.
+ I18n get i18n;
+
/// Loads the i18n file from the source.
/// If [strict] is `true`, it will throw an NoLocaleException if the
/// `@@locale` key is not found in the i18n file, otherwise it will
@@ -44,8 +52,26 @@ abstract class I18nRepository extends BaseRepository {
});
/// Gets the translation for the given [key].
+ ///
+ /// If [arguments] is not `null`, it will replace the placeholders in the
+ /// translation with the given arguments.
Result get(
String key, [
Map arguments = const {},
]);
+
+ /// Sets the current i18n instance.
+ ///
+ /// This method is used to set the current i18n instance.
+ Result setI18n(I18n i18n);
+
+ /// Gets the current i18n instance.
+ ///
+ /// This method is used to get the current i18n instance.
+ Result getI18n();
+
+ /// Gets the current locale.
+ ///
+ /// This method is used to get the current locale.
+ Result getLocale();
}