Skip to content

Commit

Permalink
fix: setting the correct locale to contexts with navigatorKey (#1724)
Browse files Browse the repository at this point in the history
* Add localization with navigatorKey

* Update CHANGELOG

* Update tests

* Update tests

* Format

* Remove unused import

* Apply review improvements

* Update CHANGELOG
  • Loading branch information
buenaflor authored Nov 15, 2023
1 parent 051e97a commit 3e5078c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Fixes

- Fixes setting the correct locale to contexts with navigatorKey ([#1724](https://github.com/getsentry/sentry-dart/pull/1724))
- If you have a selected locale in e.g MaterialApp, this fix will retrieve the correct locale for the event context.
- Flutter renderer information was removed on dart:io platforms since it didn't add the correct value ([#1723](https://github.com/getsentry/sentry-dart/pull/1723))
- Unsupported types with Expando ([#1690](https://github.com/getsentry/sentry-dart/pull/1690))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ class FlutterEnricherEventProcessor implements EventProcessor {
}

SentryCulture _getCulture(SentryCulture? culture) {
final languageTag = _window?.locale.toLanguageTag();
final windowLanguageTag = _window?.locale.toLanguageTag();
final screenLocale = _retrieveWidgetLocale(_options.navigatorKey);
final languageTag = screenLocale?.toLanguageTag() ?? windowLanguageTag;

// Future enhancement:
// _window?.locales
Expand Down Expand Up @@ -256,4 +258,12 @@ class FlutterEnricherEventProcessor implements EventProcessor {
return app;
}
}

Locale? _retrieveWidgetLocale(GlobalKey<NavigatorState>? navigatorKey) {
final BuildContext? context = navigatorKey?.currentContext;
if (context != null) {
return Localizations.maybeLocaleOf(context);
}
return null;
}
}
3 changes: 3 additions & 0 deletions flutter/lib/src/sentry_flutter_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,7 @@ class SentryFlutterOptions extends SentryOptions {
// ignore: invalid_use_of_internal_member
super.profilesSampleRate = value;
}

/// The [navigatorKey] is used to add information of the currently used locale to the contexts.
GlobalKey<NavigatorState>? navigatorKey;
}
2 changes: 2 additions & 0 deletions flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ dev_dependencies:
flutter_lints: ^2.0.0
collection: ^1.16.0
remove_from_coverage: ^2.0.0
flutter_localizations:
sdk: flutter

flutter:
plugin:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import 'dart:async';

import 'package:flutter/foundation.dart';

// backcompatibility for Flutter < 3.3
// ignore: unnecessary_import
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
Expand Down Expand Up @@ -104,6 +106,38 @@ void main() {
expect(culture?.timezone, isNotNull);
});

testWidgets(
'GIVEN MaterialApp WHEN setting locale and sentryNavigatorKey THEN enrich event culture with selected locale',
(WidgetTester tester) async {
GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

await tester.pumpWidget(MaterialApp(
navigatorKey: navigatorKey,
home: Material(),
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('en', 'US'),
Locale('de', 'DE'),
],
locale: const Locale('de', 'DE'),
));

final enricher = fixture.getSut(
binding: () => tester.binding,
optionsBuilder: (options) {
options.navigatorKey = navigatorKey;
return options;
},
);

final event = await enricher.apply(SentryEvent());

expect(event?.contexts.culture?.locale, 'de-DE');
});

testWidgets('app context in foreground', (WidgetTester tester) async {
final enricher = fixture.getSut(
binding: () => tester.binding,
Expand Down Expand Up @@ -380,16 +414,19 @@ class Fixture {
PlatformChecker? checker,
bool hasNativeIntegration = false,
bool reportPackages = true,
SentryFlutterOptions Function(SentryFlutterOptions)? optionsBuilder,
}) {
final platformChecker = checker ??
MockPlatformChecker(
hasNativeIntegration: hasNativeIntegration,
);

final options = SentryFlutterOptions(
dsn: fakeDsn,
checker: platformChecker,
)..reportPackages = reportPackages;
return FlutterEnricherEventProcessor(options);
final customizedOptions = optionsBuilder?.call(options) ?? options;
return FlutterEnricherEventProcessor(customizedOptions);
}

PageRoute<dynamic> route(RouteSettings? settings) => PageRouteBuilder<void>(
Expand Down

0 comments on commit 3e5078c

Please sign in to comment.