Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 in the event context.
- Unsupported types with Expando ([#1690](https://github.com/getsentry/sentry-dart/pull/1690))

### Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:sentry/sentry.dart';

import '../localizations.dart';
import '../navigation/sentry_navigator_observer.dart';
import '../sentry_flutter_options.dart';

Expand Down Expand Up @@ -107,7 +108,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
11 changes: 11 additions & 0 deletions flutter/lib/src/localizations.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:flutter/cupertino.dart';
import 'package:meta/meta.dart';

@internal
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 used to retrieve the current context of the app.
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 @@ -73,6 +75,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 @@ -349,16 +383,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