From 9dc7b8940ad0dab2b23688b210150d388197d06a Mon Sep 17 00:00:00 2001 From: Patrick Stewart Date: Mon, 30 Dec 2024 02:25:57 -0700 Subject: [PATCH] add: adding support localizable --- .../support/lib/src/traits/localizable.dart | 56 ++++++++++++++++++ .../support/test/traits/localizable_test.dart | 58 +++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 packages/support/lib/src/traits/localizable.dart create mode 100644 packages/support/test/traits/localizable_test.dart diff --git a/packages/support/lib/src/traits/localizable.dart b/packages/support/lib/src/traits/localizable.dart new file mode 100644 index 0000000..9960a2d --- /dev/null +++ b/packages/support/lib/src/traits/localizable.dart @@ -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(String? locale, T Function() callback) { + if (locale == null) { + return callback(); + } + + // Get the current locale + final config = container.make(); + 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'); +} diff --git a/packages/support/test/traits/localizable_test.dart b/packages/support/test/traits/localizable_test.dart new file mode 100644 index 0000000..5c57ce3 --- /dev/null +++ b/packages/support/test/traits/localizable_test.dart @@ -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')); + }); + }); +}