Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes setting the correct locale to contexts with navigatorKey #1724

Merged
merged 9 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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();
final languageTag = screenLocale?.toLanguageTag() ?? windowLanguageTag;

// Future enhancement:
// _window?.locales
Expand Down
13 changes: 13 additions & 0 deletions flutter/lib/src/localizations.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:flutter/cupertino.dart';
import 'package:meta/meta.dart';

GlobalKey<NavigatorState> sentryNavigatorKey = GlobalKey<NavigatorState>();

@internal
Locale? retrieveWidgetLocale() {
final BuildContext? context = sentryNavigatorKey.currentContext;
if (context != null) {
return Localizations.localeOf(context);
}
return null;
}
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,13 +1,16 @@
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';
import 'package:sentry_flutter/src/event_processor/flutter_enricher_event_processor.dart';
import 'package:sentry_flutter/src/localizations.dart';

import '../mocks.dart';

Expand Down Expand Up @@ -73,6 +76,32 @@ void main() {
expect(culture?.timezone, isNotNull);
});

testWidgets(
'GIVEN MaterialApp WHEN setting locale and sentryNavigatorKey THEN enrich event culture with selected locale',
(WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
navigatorKey: sentryNavigatorKey,
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,
);

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