add: adding support localizable
This commit is contained in:
parent
0819c9c8cd
commit
9dc7b8940a
2 changed files with 114 additions and 0 deletions
56
packages/support/lib/src/traits/localizable.dart
Normal file
56
packages/support/lib/src/traits/localizable.dart
Normal file
|
@ -0,0 +1,56 @@
|
|||
import 'package:platform_container/container.dart';
|
||||
import 'package:platform_support/src/fluent.dart';
|
||||
|
||||
/// A mixin that provides localization functionality.
|
||||
///
|
||||
/// This mixin allows classes to execute code with a specific locale while ensuring
|
||||
/// the original locale is restored afterward, similar to Laravel's Localizable trait.
|
||||
mixin Localizable {
|
||||
/// Run the callback with the given locale.
|
||||
///
|
||||
/// This method temporarily changes the application's locale, executes the callback,
|
||||
/// and then restores the original locale. This ensures that the locale change is
|
||||
/// localized to just the callback execution.
|
||||
///
|
||||
/// If no locale is provided (null), the callback is executed with the current locale.
|
||||
///
|
||||
/// Parameters:
|
||||
/// - [locale]: The locale to use during callback execution
|
||||
/// - [callback]: The function to execute with the specified locale
|
||||
///
|
||||
/// Returns:
|
||||
/// The result of executing the callback
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// withLocale('es', () {
|
||||
/// // Code here runs with Spanish locale
|
||||
/// return someValue;
|
||||
/// });
|
||||
/// ```
|
||||
T withLocale<T>(String? locale, T Function() callback) {
|
||||
if (locale == null) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
// Get the current locale
|
||||
final config = container.make<Fluent>();
|
||||
final original = config['locale'] as String;
|
||||
|
||||
try {
|
||||
// Set the new locale
|
||||
config['locale'] = locale;
|
||||
return callback();
|
||||
} finally {
|
||||
// Restore the original locale
|
||||
config['locale'] = original;
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the container instance.
|
||||
///
|
||||
/// This is a helper method to access the container. In a real application,
|
||||
/// you would typically get this from your application's service container.
|
||||
Container get container => throw UnimplementedError(
|
||||
'You must implement the container getter to use Localizable');
|
||||
}
|
58
packages/support/test/traits/localizable_test.dart
Normal file
58
packages/support/test/traits/localizable_test.dart
Normal file
|
@ -0,0 +1,58 @@
|
|||
import 'package:platform_container/container.dart';
|
||||
import 'package:platform_container/mirrors.dart';
|
||||
import 'package:platform_support/src/fluent.dart';
|
||||
import 'package:platform_support/src/traits/localizable.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
class TestLocalizable with Localizable {
|
||||
final Container _container;
|
||||
|
||||
TestLocalizable(this._container);
|
||||
|
||||
@override
|
||||
Container get container => _container;
|
||||
}
|
||||
|
||||
void main() {
|
||||
group('Localizable', () {
|
||||
late Container container;
|
||||
late TestLocalizable localizable;
|
||||
late Fluent config;
|
||||
|
||||
setUp(() {
|
||||
container = Container(MirrorsReflector());
|
||||
config = Fluent();
|
||||
config['locale'] = 'en';
|
||||
container.registerSingleton(config);
|
||||
localizable = TestLocalizable(container);
|
||||
});
|
||||
|
||||
test('executes callback with given locale', () {
|
||||
var result = localizable.withLocale('es', () {
|
||||
expect(config['locale'], equals('es'));
|
||||
return 'done';
|
||||
});
|
||||
expect(result, equals('done'));
|
||||
});
|
||||
|
||||
test('restores original locale after callback', () {
|
||||
localizable.withLocale('es', () {});
|
||||
expect(config['locale'], equals('en'));
|
||||
});
|
||||
|
||||
test('restores original locale even if callback throws', () {
|
||||
try {
|
||||
localizable.withLocale('es', () => throw Exception('test'));
|
||||
} catch (_) {}
|
||||
expect(config['locale'], equals('en'));
|
||||
});
|
||||
|
||||
test('executes callback with current locale if no locale provided', () {
|
||||
var result = localizable.withLocale(null, () {
|
||||
expect(config['locale'], equals('en'));
|
||||
return 'done';
|
||||
});
|
||||
expect(result, equals('done'));
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue