Skip to content

Commit

Permalink
Force the app language to be set by the router (where a `Localization…
Browse files Browse the repository at this point in the history
…` widget is available) (#4819)
  • Loading branch information
g123k authored Nov 17, 2023
1 parent 996a5fe commit bac58f9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
6 changes: 0 additions & 6 deletions packages/smooth_app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,9 @@ class _SmoothAppState extends State<SmoothApp> {
if (!_screenshots) {
await _userPreferences.init(_productPreferences);
}
await _initAppLanguage();
return true;
}

Future<void> _initAppLanguage() {
ProductQuery.setLanguage(context, _userPreferences);
return _productPreferences.refresh();
}

@override
Widget build(BuildContext context) {
return FutureBuilder<void>(
Expand Down
16 changes: 16 additions & 0 deletions packages/smooth_app/lib/pages/navigator/app_navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:go_router/go_router.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/data_models/product_preferences.dart';
import 'package:smooth_app/helpers/analytics_helper.dart';
import 'package:smooth_app/helpers/extension_on_text_helper.dart';
import 'package:smooth_app/pages/carousel_manager.dart';
Expand All @@ -16,6 +17,7 @@ import 'package:smooth_app/pages/product/edit_product_page.dart';
import 'package:smooth_app/pages/product/new_product_page.dart';
import 'package:smooth_app/pages/product/product_loader_page.dart';
import 'package:smooth_app/pages/scan/search_page.dart';
import 'package:smooth_app/query/product_query.dart';

/// A replacement for the [Navigator], where we internally use [GoRouter].
/// By itself the [GoRouter] attribute is not accessible, to allow us to easily
Expand Down Expand Up @@ -106,6 +108,10 @@ class _SmoothGoRouter {
GoRoute(
path: _InternalAppRoutes.HOME_PAGE,
builder: (BuildContext context, GoRouterState state) {
if (!_appLanguageInitialized) {
_initAppLanguage(context);
}

return _findLastOnboardingPage(context);
},
// We use sub-routes to allow the back button to work correctly
Expand Down Expand Up @@ -263,6 +269,16 @@ class _SmoothGoRouter {
);
}

bool _appLanguageInitialized = false;

/// Required to setup the whole app
Future<void> _initAppLanguage(BuildContext context) {
// Must be set first to ensure the method is only called once
_appLanguageInitialized = true;
ProductQuery.setLanguage(context, context.read<UserPreferences>());
return context.read<ProductPreferences>().refresh();
}

String _openExternalLink(String path) {
AnalyticsHelper.trackEvent(
AnalyticsEvent.genericDeepLink,
Expand Down

0 comments on commit bac58f9

Please sign in to comment.