Skip to content

Commit 3e5078c

Browse files
authored
fix: setting the correct locale to contexts with navigatorKey (#1724)
* Add localization with navigatorKey * Update CHANGELOG * Update tests * Update tests * Format * Remove unused import * Apply review improvements * Update CHANGELOG
1 parent 051e97a commit 3e5078c

File tree

5 files changed

+56
-2
lines changed

5 files changed

+56
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Fixes
66

7+
- Fixes setting the correct locale to contexts with navigatorKey ([#1724](https://github.com/getsentry/sentry-dart/pull/1724))
8+
- If you have a selected locale in e.g MaterialApp, this fix will retrieve the correct locale for the event context.
79
- 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))
810
- Unsupported types with Expando ([#1690](https://github.com/getsentry/sentry-dart/pull/1690))
911

flutter/lib/src/event_processor/flutter_enricher_event_processor.dart

+11-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ class FlutterEnricherEventProcessor implements EventProcessor {
107107
}
108108

109109
SentryCulture _getCulture(SentryCulture? culture) {
110-
final languageTag = _window?.locale.toLanguageTag();
110+
final windowLanguageTag = _window?.locale.toLanguageTag();
111+
final screenLocale = _retrieveWidgetLocale(_options.navigatorKey);
112+
final languageTag = screenLocale?.toLanguageTag() ?? windowLanguageTag;
111113

112114
// Future enhancement:
113115
// _window?.locales
@@ -256,4 +258,12 @@ class FlutterEnricherEventProcessor implements EventProcessor {
256258
return app;
257259
}
258260
}
261+
262+
Locale? _retrieveWidgetLocale(GlobalKey<NavigatorState>? navigatorKey) {
263+
final BuildContext? context = navigatorKey?.currentContext;
264+
if (context != null) {
265+
return Localizations.maybeLocaleOf(context);
266+
}
267+
return null;
268+
}
259269
}

flutter/lib/src/sentry_flutter_options.dart

+3
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,7 @@ class SentryFlutterOptions extends SentryOptions {
286286
// ignore: invalid_use_of_internal_member
287287
super.profilesSampleRate = value;
288288
}
289+
290+
/// The [navigatorKey] is used to add information of the currently used locale to the contexts.
291+
GlobalKey<NavigatorState>? navigatorKey;
289292
}

flutter/pubspec.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ dev_dependencies:
2929
flutter_lints: ^2.0.0
3030
collection: ^1.16.0
3131
remove_from_coverage: ^2.0.0
32+
flutter_localizations:
33+
sdk: flutter
3234

3335
flutter:
3436
plugin:

flutter/test/event_processor/flutter_enricher_event_processor_test.dart

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import 'dart:async';
22

33
import 'package:flutter/foundation.dart';
4+
45
// backcompatibility for Flutter < 3.3
56
// ignore: unnecessary_import
67
import 'package:flutter/material.dart';
8+
import 'package:flutter_localizations/flutter_localizations.dart';
79
import 'package:flutter_test/flutter_test.dart';
810
import 'package:package_info_plus/package_info_plus.dart';
911
import 'package:sentry_flutter/sentry_flutter.dart';
@@ -104,6 +106,38 @@ void main() {
104106
expect(culture?.timezone, isNotNull);
105107
});
106108

109+
testWidgets(
110+
'GIVEN MaterialApp WHEN setting locale and sentryNavigatorKey THEN enrich event culture with selected locale',
111+
(WidgetTester tester) async {
112+
GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
113+
114+
await tester.pumpWidget(MaterialApp(
115+
navigatorKey: navigatorKey,
116+
home: Material(),
117+
localizationsDelegates: const [
118+
GlobalMaterialLocalizations.delegate,
119+
GlobalCupertinoLocalizations.delegate,
120+
],
121+
supportedLocales: const [
122+
Locale('en', 'US'),
123+
Locale('de', 'DE'),
124+
],
125+
locale: const Locale('de', 'DE'),
126+
));
127+
128+
final enricher = fixture.getSut(
129+
binding: () => tester.binding,
130+
optionsBuilder: (options) {
131+
options.navigatorKey = navigatorKey;
132+
return options;
133+
},
134+
);
135+
136+
final event = await enricher.apply(SentryEvent());
137+
138+
expect(event?.contexts.culture?.locale, 'de-DE');
139+
});
140+
107141
testWidgets('app context in foreground', (WidgetTester tester) async {
108142
final enricher = fixture.getSut(
109143
binding: () => tester.binding,
@@ -380,16 +414,19 @@ class Fixture {
380414
PlatformChecker? checker,
381415
bool hasNativeIntegration = false,
382416
bool reportPackages = true,
417+
SentryFlutterOptions Function(SentryFlutterOptions)? optionsBuilder,
383418
}) {
384419
final platformChecker = checker ??
385420
MockPlatformChecker(
386421
hasNativeIntegration: hasNativeIntegration,
387422
);
423+
388424
final options = SentryFlutterOptions(
389425
dsn: fakeDsn,
390426
checker: platformChecker,
391427
)..reportPackages = reportPackages;
392-
return FlutterEnricherEventProcessor(options);
428+
final customizedOptions = optionsBuilder?.call(options) ?? options;
429+
return FlutterEnricherEventProcessor(customizedOptions);
393430
}
394431

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

0 commit comments

Comments
 (0)