feat(i18n): add getter/setter
This commit is contained in:
parent
75f561a19e
commit
cbbde8db85
@ -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.');
|
||||
}
|
||||
|
@ -15,18 +15,27 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
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<I18n> _parse(
|
||||
String content,
|
||||
Parser<String, Map<String, dynamic>> parser, {
|
||||
@ -129,4 +138,19 @@ class I18nRepositoryImpl extends I18nRepository {
|
||||
throw KeyNotFoundException(key, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Result<I18n, AppException> getI18n() =>
|
||||
Result.conditional(!_i18n.isEmpty, _i18n, NotLoadedException());
|
||||
|
||||
@override
|
||||
Result<String, AppException> getLocale() =>
|
||||
Result.conditional(!_i18n.isEmpty, _i18n.locale, NotLoadedException());
|
||||
|
||||
@override
|
||||
Result<void, AppException> setI18n(I18n i18n) {
|
||||
_i18n = i18n;
|
||||
|
||||
return const Ok(null);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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<String, AppException> get(
|
||||
String key, [
|
||||
Map<String, dynamic> arguments = const {},
|
||||
]);
|
||||
|
||||
/// Sets the current i18n instance.
|
||||
///
|
||||
/// This method is used to set the current i18n instance.
|
||||
Result<void, AppException> setI18n(I18n i18n);
|
||||
|
||||
/// Gets the current i18n instance.
|
||||
///
|
||||
/// This method is used to get the current i18n instance.
|
||||
Result<I18n, AppException> getI18n();
|
||||
|
||||
/// Gets the current locale.
|
||||
///
|
||||
/// This method is used to get the current locale.
|
||||
Result<String, AppException> getLocale();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user