-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add scaffold for localizations (l10n) in Sharezone (#1798)
This pull request introduces a new package, sharezone_localizations, which provides a streamlined way of managing and accessing localized strings in the Sharezone-App using Flutter’s internationalization features. Below is an overview of the core additions: 1. New Package: sharezone_localizations • README Documentation: Explains how to add, generate, and use translatable strings. • Internationalization Setup: Based on Flutter’s gen-l10n tooling, enabling multiple locales and seamless code generation. 2. Locale Management • `AppLocaleProvider`: Facilitates dynamic switching of app locales. 3. Usage Guidelines • String Access: Via the context.sl extension, which simplifies referencing keys from the .arb files. • Adding/Updating Strings: Detailed steps on modifying .arb files, including placeholder usage and recommended auto-translation tools. 4. Generating Localizations • Flutter gen-l10n Command: Simple CLI approach to regenerate localizations after .arb updates. • VS Code Task: Option to run a dedicated VS Code Task (“Generate l10n for sharezone_localizations”) for developers who prefer an IDE-based workflow. Overall, this package consolidates translation logic, enhances maintainability by centralizing locale management, and simplifies how developers interact with localized strings. <img width="1552" alt="image" src="https://github.com/user-attachments/assets/39c625b4-1f5d-437f-b1fb-3b6d2f81511a" /> Things, we need to do in future pull requests: * #1799 * Write tests for language page * Store locale in user document (the backend have to know the language to send push notifications in the user's language) * Replace hard-coded strings with l10n strings * Remove old l10n files in `/app/l10n/` * Document how to add new languages (copy `.arb` file, use [`arb_translate`](https://pub.dev/packages/arb_translate), makes native changes - `Info.plist` needs changed) * Make sure that when system language is French, English is selected as default <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Summary by CodeRabbit - **New Features** - Introduced a language selection feature in the settings. - Added localization support for German and English languages. - Implemented a `LanguagePage` for users to choose their preferred language. - Enhanced localization management with a new `FeatureFlagl10n` class. - Integrated dynamic locale adjustment based on user preferences. - Added a new `AppLocaleProvider` for managing application locale settings. - Established a structured localization framework with `SharezoneLocalizations`. - **Bug Fixes** - Enhanced state management for localization features. - **Documentation** - Added a comprehensive README for the `sharezone_localizations` package. - **Chores** - Updated `.gitignore` to manage ignored files more effectively. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Part of #346 --------- Co-authored-by: nilsreichardt <me@nils.re>
- Loading branch information
1 parent
01368dc
commit 1b8aa06
Showing
32 changed files
with
1,158 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
FeatureFlagl10n(this.keyValueStore) { | ||
_subscription = keyValueStore | ||
.getBool('l10n_enabled', defaultValue: false) | ||
.listen((event) { | ||
final newValue = event == true; | ||
if (isl10nEnabled != newValue) { | ||
isl10nEnabled = newValue; | ||
notifyListeners(); | ||
} | ||
}); | ||
} | ||
|
||
final StreamingKeyValueStore keyValueStore; | ||
late StreamSubscription<bool> _subscription; | ||
bool isl10nEnabled = false; | ||
|
||
void toggle() { | ||
isl10nEnabled = !isl10nEnabled; | ||
keyValueStore.setBool('l10n_enabled', isl10nEnabled); | ||
notifyListeners(); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_subscription.cancel(); | ||
super.dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
|
||
@override | ||
Future<void> setLocale(AppLocale locale) async { | ||
final value = jsonEncode(locale.toMap()); | ||
keyValueStore.setString( | ||
'locale', | ||
value, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.