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

Add scaffold for localizations (l10n) in Sharezone #1798

Merged
merged 33 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
9f98365
initial commit
flowhorn Dec 22, 2024
b841cd2
Format files
nilsreichardt Dec 22, 2024
5667f0c
Add `.gen` suffix to generated localization files to indicate that th…
nilsreichardt Dec 22, 2024
4280f15
Add VS Code Task
nilsreichardt Dec 22, 2024
32939a7
Migrate to from `bloc` to `provider`
nilsreichardt Dec 22, 2024
d1afc7a
Use `sharezone_lints` instead of `flutter_lints`
nilsreichardt Dec 22, 2024
aa376d4
missing gateway to provider bloc added
flowhorn Dec 22, 2024
adf6fc9
Extract `AppLocaleProviderGateway` to into a separate file
nilsreichardt Dec 22, 2024
75b5833
Merge branch 'sharezone_localizations_demo' of https://github.com/flo…
nilsreichardt Dec 22, 2024
7e3077e
Add `getNativeName`, `getTranslatedName`, `toMap` and `fromMap` to `A…
nilsreichardt Dec 22, 2024
acbc2b6
Set locale to gateway in `AppLocaleProvider`
nilsreichardt Dec 22, 2024
f0b2eac
Format comment in `SharezoneLocalizationsContextExtension`
nilsreichardt Dec 22, 2024
021633b
Add languages to `app_en` and `app_de`
nilsreichardt Dec 22, 2024
5613aae
Add language page
nilsreichardt Dec 22, 2024
0a34df3
Add feature flag for l10n feature
nilsreichardt Dec 22, 2024
ab7a1f9
Rename `AppLocales` to `AppLocale`
nilsreichardt Dec 22, 2024
fc6e506
Remove `SharezoneAppLocales`
nilsreichardt Dec 22, 2024
c431931
Remove `AppLocaleBuilder` from `README.md`
nilsreichardt Dec 22, 2024
25709b8
Add comment to README
nilsreichardt Dec 22, 2024
78f0901
Make AppLocaleProviderGateway const
nilsreichardt Dec 22, 2024
1d30db9
Remove rxdart
nilsreichardt Dec 22, 2024
efbfe0a
Add license header
nilsreichardt Dec 22, 2024
a000ce1
Add `sharezone_localizations` to licenses_config.yaml
nilsreichardt Dec 22, 2024
43f5a6e
Switch l10n strings to CamelCase instead of snake_case
nilsreichardt Dec 23, 2024
bba7b76
Change `app_de.arb` as template file
nilsreichardt Dec 23, 2024
9861a84
Automatically add license header after generating files
nilsreichardt Dec 23, 2024
5276989
Use `format` property instead of running `dart format .`
nilsreichardt Dec 23, 2024
9a4161c
Fix language typo
nilsreichardt Dec 28, 2024
20e863a
Apply default language in SharezoneLocalizations
nilsreichardt Dec 28, 2024
ad73eaa
Renamed `context.sl` to `context.l10n`
nilsreichardt Dec 28, 2024
80cb441
Better error handle for context.l10n
nilsreichardt Dec 28, 2024
3585edc
Add mock of l10n feature flag to settings page
nilsreichardt Dec 28, 2024
16da52f
Fix golden test
nilsreichardt Dec 28, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ coverage/
.vscode/*
!.vscode/launch.json
!.vscode/settings.json
!.vscode/tasks.json

# FVM will create a relative symlink in your project from .fvm/versions/ to
# the cache of the selected version. We should add this to our .gitignore.
Expand Down
22 changes: 22 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
// This task generates the l10n files for the
// sharezone_localizations.
//
// FVM is required to run this task.
"label": "Generate l10n for sharezone_localizations",
"type": "shell",
// Additionally, we add the license header again (the "flutter
// gen-l10n" always removes the license header).
"command": "fvm flutter gen-l10n && addlicense -c \"Sharezone UG (haftungsbeschränkt)\" -f ../../header_template.txt .",
"options": {
"cwd": "${workspaceFolder}/lib/sharezone_localizations"
},
"problemMatcher": []
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:flutter/material.dart';
import 'package:key_value_store/key_value_store.dart';
import 'package:rxdart/rxdart.dart';
import 'package:helper_functions/helper_functions.dart';
import 'package:sharezone/l10n/feature_flag_l10n.dart';

import '../models/enter_activation_code_result.dart';
import 'enter_activation_code_activator.dart';
Expand All @@ -26,6 +27,7 @@ class EnterActivationCodeBloc extends BlocBase {
final _enterActivationCodeSubject =
BehaviorSubject<EnterActivationCodeResult>();
final KeyValueStore keyValueStore;
final FeatureFlagl10n featureFlagl10n;

String? _lastEnteredValue;

Expand All @@ -34,6 +36,7 @@ class EnterActivationCodeBloc extends BlocBase {
this.crashAnalytics,
this.appFunctions,
this.keyValueStore,
this.featureFlagl10n,
) {
_changeEnterActivationCodeResult(NoDataEnterActivationCodeResult());
}
Expand Down Expand Up @@ -89,6 +92,11 @@ class EnterActivationCodeBloc extends BlocBase {
return;
}

if (_lastEnteredValue?.trim().toLowerCase() == 'l10n') {
_togglel10nFeatureFlag();
return;
}

_changeEnterActivationCodeResult(LoadingEnterActivationCodeResult());

final enterActivationCodeResult = await _runAppFunction(enteredValue);
Expand All @@ -107,6 +115,18 @@ class EnterActivationCodeBloc extends BlocBase {
);
}

void _togglel10nFeatureFlag() {
final currentValue = featureFlagl10n.isl10nEnabled;
featureFlagl10n.toggle();

_changeEnterActivationCodeResult(
SuccessfulEnterActivationCodeResult(
'l10n',
'l10n wurde ${!currentValue ? 'aktiviert' : 'deaktiviert'}. Starte die App neu, um die Änderungen zu sehen.',
),
);
}

Future<void> _clearCache(BuildContext context) async {
await Future.wait([
keyValueStore.clear(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ import 'package:bloc_base/bloc_base.dart';
import 'package:crash_analytics/crash_analytics.dart';
import 'package:key_value_store/key_value_store.dart';
import 'package:sharezone/activation_code/src/bloc/enter_activation_code_bloc.dart';
import 'package:sharezone/l10n/feature_flag_l10n.dart';

class EnterActivationCodeBlocFactory extends BlocBase {
final CrashAnalytics crashAnalytics;
final Analytics analytics;
final SharezoneAppFunctions appFunctions;
final KeyValueStore keyValueStore;
final FeatureFlagl10n featureFlagl10n;

EnterActivationCodeBlocFactory({
required this.analytics,
required this.crashAnalytics,
required this.appFunctions,
required this.keyValueStore,
required this.featureFlagl10n,
});

EnterActivationCodeBloc createBloc() {
Expand All @@ -32,6 +35,7 @@ class EnterActivationCodeBlocFactory extends BlocBase {
crashAnalytics,
appFunctions,
keyValueStore,
featureFlagl10n,
);
}

Expand Down
42 changes: 42 additions & 0 deletions app/lib/l10n/feature_flag_l10n.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2024 Sharezone UG (haftungsbeschränkt)
// Licensed under the EUPL-1.2-or-later.
//
// You may obtain a copy of the Licence at:
// https://joinup.ec.europa.eu/software/page/eupl
//
// SPDX-License-Identifier: EUPL-1.2

import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:sharezone/util/cache/streaming_key_value_store.dart';

class FeatureFlagl10n extends ChangeNotifier {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a temporary class. Will be removed when we're done with translating.

FeatureFlagl10n(this.keyValueStore) {
_subscription = keyValueStore
.getBool('l10n_enabled', defaultValue: false)
.listen((event) {
final newValue = event == true;
if (isl10nEnabled != newValue) {
isl10nEnabled = newValue;
notifyListeners();
}
});
}
nilsreichardt marked this conversation as resolved.
Show resolved Hide resolved
nilsreichardt marked this conversation as resolved.
Show resolved Hide resolved

final StreamingKeyValueStore keyValueStore;
late StreamSubscription<bool> _subscription;
bool isl10nEnabled = false;
nilsreichardt marked this conversation as resolved.
Show resolved Hide resolved

void toggle() {
isl10nEnabled = !isl10nEnabled;
keyValueStore.setBool('l10n_enabled', isl10nEnabled);
notifyListeners();
}

@override
void dispose() {
_subscription.cancel();
super.dispose();
}
}
42 changes: 42 additions & 0 deletions app/lib/l10n/flutter_app_local_gateway.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2024 Sharezone UG (haftungsbeschränkt)
// Licensed under the EUPL-1.2-or-later.
//
// You may obtain a copy of the Licence at:
// https://joinup.ec.europa.eu/software/page/eupl
//
// SPDX-License-Identifier: EUPL-1.2

import 'dart:convert';

import 'package:sharezone/l10n/feature_flag_l10n.dart';
import 'package:sharezone/util/cache/streaming_key_value_store.dart';
import 'package:sharezone_localizations/sharezone_localizations.dart';

class FlutterAppLocaleProviderGateway extends AppLocaleProviderGateway {
const FlutterAppLocaleProviderGateway({
required this.keyValueStore,
required this.featureFlagl10n,
});

final FeatureFlagl10n featureFlagl10n;
final StreamingKeyValueStore keyValueStore;

@override
Stream<AppLocale> getLocale() {
final defaultValue = jsonEncode(featureFlagl10n.isl10nEnabled
? AppLocale.system.toMap()
: AppLocale.en.toMap());
return keyValueStore
.getString('locale', defaultValue: defaultValue)
.map((event) => AppLocale.fromMap(jsonDecode(event)));
}
nilsreichardt marked this conversation as resolved.
Show resolved Hide resolved

@override
Future<void> setLocale(AppLocale locale) async {
final value = jsonEncode(locale.toMap());
keyValueStore.setString(
'locale',
value,
);
}
nilsreichardt marked this conversation as resolved.
Show resolved Hide resolved
}
20 changes: 20 additions & 0 deletions app/lib/main/sharezone.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ import 'package:provider/provider.dart';
import 'package:sharezone/dynamic_links/beitrittsversuch.dart';
import 'package:sharezone/dynamic_links/dynamic_link_bloc.dart';
import 'package:sharezone/dynamic_links/dynamic_links.dart';
import 'package:sharezone/l10n/feature_flag_l10n.dart';
import 'package:sharezone/l10n/flutter_app_local_gateway.dart';
import 'package:sharezone/main/auth_app.dart';
import 'package:sharezone/main/bloc_dependencies.dart';
import 'package:sharezone/main/sharezone_app.dart';
import 'package:sharezone/main/sharezone_bloc_providers.dart';
import 'package:sharezone/navigation/logic/navigation_bloc.dart';
import 'package:sharezone/notifications/notifications_permission.dart';
import 'package:sharezone/onboarding/group_onboarding/logic/signed_up_bloc.dart';
import 'package:sharezone/util/cache/streaming_key_value_store.dart';
import 'package:sharezone/util/flavor.dart';
import 'package:sharezone/widgets/animation/color_fade_in.dart';
import 'package:sharezone/widgets/development_stage_banner.dart';
import 'package:sharezone_localizations/sharezone_localizations.dart';
import 'package:sharezone_utils/device_information_manager.dart';
import 'package:sharezone_widgets/sharezone_widgets.dart';

Expand Down Expand Up @@ -72,6 +76,8 @@ class Sharezone extends StatefulWidget {
class _SharezoneState extends State<Sharezone> with WidgetsBindingObserver {
late SignUpBloc signUpBloc;
late StreamSubscription<AuthUser?> authSubscription;
late StreamingKeyValueStore streamingKeyValueStore;
late FeatureFlagl10n featureFlagl10n;

@override
void initState() {
Expand All @@ -93,6 +99,11 @@ class _SharezoneState extends State<Sharezone> with WidgetsBindingObserver {
authSubscription = listenToAuthStateChanged().listen((user) {
authUserSubject.sink.add(user);
});

streamingKeyValueStore = FlutterStreamingKeyValueStore(
widget.blocDependencies.streamingSharedPreferences,
);
featureFlagl10n = FeatureFlagl10n(streamingKeyValueStore);
}

void logAppOpen() {
Expand Down Expand Up @@ -129,6 +140,15 @@ class _SharezoneState extends State<Sharezone> with WidgetsBindingObserver {
MobileDeviceInformationRetriever(),
),
),
ChangeNotifierProvider.value(value: featureFlagl10n),
ChangeNotifierProvider(
create: (context) => AppLocaleProvider(
gateway: FlutterAppLocaleProviderGateway(
keyValueStore: streamingKeyValueStore,
featureFlagl10n: featureFlagl10n,
),
),
),
],
child: MultiBlocProvider(
blocProviders: [
Expand Down
2 changes: 2 additions & 0 deletions app/lib/main/sharezone_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import 'package:sharezone/settings/settings_page.dart';
import 'package:sharezone/settings/src/subpages/about/about_page.dart';
import 'package:sharezone/settings/src/subpages/changelog_page.dart';
import 'package:sharezone/settings/src/subpages/imprint/page/imprint_page.dart';
import 'package:sharezone/settings/src/subpages/language/language_page.dart';
import 'package:sharezone/settings/src/subpages/my_profile/change_email.dart';
import 'package:sharezone/settings/src/subpages/my_profile/change_password.dart';
import 'package:sharezone/settings/src/subpages/my_profile/change_state.dart';
Expand Down Expand Up @@ -204,6 +205,7 @@ class _SharezoneAppState extends State<SharezoneApp>
ICalLinksDialog.tag: (context) => const ICalLinksDialog(),
CreateTermPage.tag: (context) => const CreateTermPage(),
GradesDialog.tag: (context) => const GradesDialog(),
LanguagePage.tag: (context) => const LanguagePage(),
},
navigatorKey: navigationService.navigatorKey,
),
Expand Down
4 changes: 3 additions & 1 deletion app/lib/main/sharezone_bloc_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import 'package:sharezone/ical_links/dialog/ical_links_dialog_controller_factory
import 'package:sharezone/ical_links/list/ical_links_page_controller.dart';
import 'package:sharezone/ical_links/shared/ical_link_analytics.dart';
import 'package:sharezone/ical_links/shared/ical_links_gateway.dart';
import 'package:sharezone/l10n/feature_flag_l10n.dart';
import 'package:sharezone/main/application_bloc.dart';
import 'package:sharezone/main/bloc_dependencies.dart';
import 'package:sharezone/main/onboarding/onboarding_navigator.dart';
Expand Down Expand Up @@ -497,7 +498,7 @@ class _SharezoneBlocProvidersState extends State<SharezoneBlocProviders> {
),
lazy: false,
),
Provider<KeyValueStore>.value(value: keyValueStore)
Provider<KeyValueStore>.value(value: keyValueStore),
];

mainBlocProviders = <BlocProvider>[
Expand Down Expand Up @@ -593,6 +594,7 @@ class _SharezoneBlocProvidersState extends State<SharezoneBlocProviders> {
analytics: analytics,
appFunctions: api.references.functions,
keyValueStore: widget.blocDependencies.keyValueStore,
featureFlagl10n: context.read<FeatureFlagl10n>(),
),
),
BlocProvider<NotificationsBlocFactory>(
Expand Down
16 changes: 5 additions & 11 deletions app/lib/main/sharezone_material_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import 'package:analytics/analytics.dart';
import 'package:analytics/observer.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:platform_check/platform_check.dart';
import 'package:provider/provider.dart';
import 'package:sharezone/main/bloc_dependencies.dart';
import 'package:sharezone_localizations/sharezone_localizations.dart';
import 'package:sharezone_widgets/sharezone_widgets.dart';

class SharezoneMaterialApp extends StatelessWidget {
Expand All @@ -35,7 +35,7 @@ class SharezoneMaterialApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final themeSettings = context.watch<ThemeSettings>();

final localProvider = context.watch<AppLocaleProvider>();
nilsreichardt marked this conversation as resolved.
Show resolved Hide resolved
return MaterialApp(
debugShowCheckedModeBanner: false,
title: PlatformCheck.isWeb ? "Sharezone Web-App" : "Sharezone",
Expand All @@ -45,15 +45,9 @@ class SharezoneMaterialApp extends StatelessWidget {
theme: getLightTheme().copyWith(
visualDensity: themeSettings.visualDensitySetting.visualDensity),
themeMode: _getThemeMode(themeSettings.themeBrightness),
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('en', 'US'),
Locale('de', 'DE'),
],
localizationsDelegates: SharezoneLocalizations.localizationsDelegates,
supportedLocales: SharezoneLocalizations.supportedLocales,
locale: localProvider.locale.toLocale(),
navigatorObservers: <NavigatorObserver>[
AnalyticsNavigationObserver(analytics: analytics)
],
Expand Down
Loading
Loading