Skip to content

An open-source cross-platform alternative to AirDrop

feat: add locale setting

Tien Do Name24a831dff2b
diff --git a/assets/i18n/strings.i18n.json b/assets/i18n/strings.i18n.json
index a076d754..6d126aa8 100644
--- a/assets/i18n/strings.i18n.json
+++ b/assets/i18n/strings.i18n.json
@@ -29,6 +29,10 @@
"system": "System",
"dark": "Dark",
"light": "Light"
+ },
+ "language": "Language",
+ "languageOptions": {
+ "system": "System"
}
}
}
diff --git a/assets/i18n/strings_de.i18n.json b/assets/i18n/strings_de.i18n.json
new file mode 100644
index 00000000..d1b4c713
--- /dev/null
+++ b/assets/i18n/strings_de.i18n.json
@@ -0,0 +1,38 @@
+{
+ "appName": "LocalSend",
+ "general": {
+ "advanced": "Erweitert",
+ "hide": "Verstecken",
+ "unknown": "Unbekannt"
+ },
+ "receive": {
+ "title": "Empfangen",
+ "advanced": {
+ "ip": "IP:",
+ "server": "Server:",
+ "subnetMask": "Subnetzmaske:",
+ "alias": "Alias:"
+ }
+ },
+ "send": {
+ "title": "Senden",
+ "files": "Dateien",
+ "filesWithCount": "Dateien ({count})",
+ "selectFiles": "Dateien auswählen",
+ "nearbyDevices": "Geräte in der Nähe"
+ },
+ "settings": {
+ "title": "Einstellungen",
+ "alias": "Alias",
+ "theme": "Oberfläche",
+ "themeOptions": {
+ "system": "System",
+ "dark": "Dunkel",
+ "light": "Hell"
+ },
+ "language": "Sprache",
+ "languageOptions": {
+ "system": "System"
+ }
+ }
+}
diff --git a/ios/Runner/Info.plist b/ios/Runner/Info.plist
index 65691452..7c1bbd5f 100644
--- a/ios/Runner/Info.plist
+++ b/ios/Runner/Info.plist
@@ -49,6 +49,7 @@
<true/>
<key>CFBundleLocalizations</key>
<array>
+ <string>de</string>
<string>en</string>
</array>
</dict>
diff --git a/lib/main.dart b/lib/main.dart
index 79375ff1..1d3e93bd 100644
--- a/lib/main.dart
+++ b/lib/main.dart
@@ -10,7 +10,6 @@ import 'package:window_manager/window_manager.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
- LocaleSettings.useDeviceLocale();
if (!kIsWeb && defaultTargetPlatform == TargetPlatform.windows) {
await windowManager.ensureInitialized();
@@ -18,11 +17,21 @@ Future<void> main() async {
}
final persistenceService = await PersistenceService.initialize();
- runApp(ProviderScope(
- overrides: [
- settingsProvider.overrideWith((ref) => SettingsNotifier(persistenceService)),
- ],
- child: const LocalSendApp(),
+
+ final locale = persistenceService.getLocale();
+ if (locale == null) {
+ LocaleSettings.useDeviceLocale();
+ } else {
+ LocaleSettings.setLocale(locale);
+ }
+
+ runApp(TranslationProvider(
+ child: ProviderScope(
+ overrides: [
+ settingsProvider.overrideWith((ref) => SettingsNotifier(persistenceService)),
+ ],
+ child: const LocalSendApp(),
+ ),
));
}
diff --git a/lib/model/settings.dart b/lib/model/settings.dart
index 5da30058..5d222112 100644
--- a/lib/model/settings.dart
+++ b/lib/model/settings.dart
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
+import 'package:localsend_app/gen/strings.g.dart';
part 'settings.freezed.dart';
@@ -8,5 +9,6 @@ class Settings with _$Settings {
const factory Settings({
required String alias,
required ThemeMode theme,
+ required AppLocale? locale,
}) = _Settings;
}
diff --git a/lib/pages/home_page.dart b/lib/pages/home_page.dart
index 5edec76f..0580105b 100644
--- a/lib/pages/home_page.dart
+++ b/lib/pages/home_page.dart
@@ -37,6 +37,7 @@ class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
+ Translations.of(context); // rebuild on locale change
return Scaffold(
body: SafeArea(
child: IndexedStack(
diff --git a/lib/pages/tabs/receive_page.dart b/lib/pages/tabs/receive_page.dart
index c8a56fae..63ef2c1c 100644
--- a/lib/pages/tabs/receive_page.dart
+++ b/lib/pages/tabs/receive_page.dart
@@ -45,7 +45,7 @@ class _ReceivePageState extends ConsumerState<ReceivePage> {
mainAxisAlignment: MainAxisAlignment.center,
children: [
RotatingWidget(
- duration: const Duration(seconds: 10),
+ duration: const Duration(seconds: 15),
child: Assets.img.logo512.image(width: 200),
),
Text(networkInfo?.localIp?.visualId ?? t.general.unknown, style: const TextStyle(fontSize: 48)),
diff --git a/lib/pages/tabs/send_page.dart b/lib/pages/tabs/send_page.dart
index 1316e3e1..ac292f62 100644
--- a/lib/pages/tabs/send_page.dart
+++ b/lib/pages/tabs/send_page.dart
@@ -30,7 +30,7 @@ class _SendPageState extends ConsumerState<SendPage> {
children: [
Text(
selectedFiles.isEmpty ? t.send.files : t.send.filesWithCount(count: selectedFiles.length),
- style: Theme.of(context).textTheme.headline6,
+ style: Theme.of(context).textTheme.subtitle1,
),
const SizedBox(height: 10),
...selectedFiles.mapIndexed((index, file) {
@@ -81,7 +81,7 @@ class _SendPageState extends ConsumerState<SendPage> {
),
),
const SizedBox(height: 20),
- Text(t.send.nearbyDevices, style: Theme.of(context).textTheme.headline6),
+ Text(t.send.nearbyDevices, style: Theme.of(context).textTheme.subtitle1),
const SizedBox(height: 10),
...devices.when(
data: (data) {
diff --git a/lib/pages/tabs/settings_page.dart b/lib/pages/tabs/settings_page.dart
index 88e65d2c..c76b7bce 100644
--- a/lib/pages/tabs/settings_page.dart
+++ b/lib/pages/tabs/settings_page.dart
@@ -15,34 +15,67 @@ class _SettingsPageState extends ConsumerState<SettingsPage> {
Widget build(BuildContext context) {
final settings = ref.watch(settingsProvider);
return ListView(
- padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 30),
+ padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 40),
children: [
+ Padding(
+ padding: const EdgeInsets.only(left: 8),
+ child: Text(t.settings.title, style: Theme.of(context).textTheme.headline6, textAlign: TextAlign.center),
+ ),
+ const SizedBox(height: 30),
_SettingsEntry(
label: t.settings.alias,
- child: SizedBox(
- width: 150,
- child: TextFormField(
- initialValue: settings.alias,
- textAlign: TextAlign.center,
- onChanged: (s) async {
- await ref.read(settingsProvider.notifier).setAlias(s);
- },
- ),
+ child: TextFormField(
+ initialValue: settings.alias,
+ textAlign: TextAlign.center,
+ onChanged: (s) async {
+ await ref.read(settingsProvider.notifier).setAlias(s);
+ },
),
),
_SettingsEntry(
label: t.settings.theme,
child: DropdownButton<ThemeMode>(
value: settings.theme,
+ isExpanded: true,
items: ThemeMode.values.map((theme) {
return DropdownMenuItem(
value: theme,
+ alignment: Alignment.center,
child: Text(theme.humanName),
);
}).toList(),
- onChanged: (theme) {
+ onChanged: (theme) async {
if (theme != null) {
- ref.read(settingsProvider.notifier).setTheme(theme);
+ await ref.read(settingsProvider.notifier).setTheme(theme);
+ }
+ },
+ ),
+ ),
+ _SettingsEntry(
+ label: t.settings.language,
+ child: DropdownButton<AppLocale?>(
+ value: settings.locale,
+ isExpanded: true,
+ items: [
+ DropdownMenuItem(
+ value: null,
+ alignment: Alignment.center,
+ child: Text(t.settings.languageOptions.system),
+ ),
+ ...AppLocale.values.map((locale) {
+ return DropdownMenuItem(
+ value: locale,
+ alignment: Alignment.center,
+ child: Text(locale.humanName),
+ );
+ }),
+ ],
+ onChanged: (locale) async {
+ await ref.read(settingsProvider.notifier).setLocale(locale);
+ if (locale == null) {
+ LocaleSettings.useDeviceLocale();
+ } else {
+ LocaleSettings.setLocale(locale);
}
},
),
@@ -61,14 +94,22 @@ class _SettingsEntry extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Padding(
- padding: const EdgeInsets.only(bottom: 20),
- child: Row(
- children: [
- Expanded(
- child: Text(label),
+ padding: const EdgeInsets.only(bottom: 15),
+ child: Card(
+ child: Padding(
+ padding: const EdgeInsets.symmetric(horizontal: 10),
+ child: Row(
+ children: [
+ Expanded(
+ child: Text(label, style: const TextStyle(fontSize: 16)),
+ ),
+ SizedBox(
+ width: 150,
+ child: child,
+ ),
+ ],
),
- child,
- ],
+ ),
),
);
}
@@ -86,3 +127,14 @@ extension on ThemeMode {
}
}
}
+
+extension on AppLocale {
+ String get humanName {
+ switch (this) {
+ case AppLocale.de:
+ return 'Deutsch';
+ case AppLocale.en:
+ return 'English';
+ }
+ }
+}
diff --git a/lib/provider/settings_provider.dart b/lib/provider/settings_provider.dart
index 78e20ad3..86e9287f 100644
--- a/lib/provider/settings_provider.dart
+++ b/lib/provider/settings_provider.dart
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
+import 'package:localsend_app/gen/strings.g.dart';
import 'package:localsend_app/model/settings.dart';
import 'package:localsend_app/service/persistence_service.dart';
@@ -16,6 +17,7 @@ class SettingsNotifier extends StateNotifier<Settings> {
return Settings(
alias: service.getAlias(),
theme: service.getTheme(),
+ locale: service.getLocale(),
);
}
@@ -32,4 +34,11 @@ class SettingsNotifier extends StateNotifier<Settings> {
theme: theme,
);
}
+
+ Future<void> setLocale(AppLocale? locale) async {
+ await _service.setLocale(locale);
+ state = state.copyWith(
+ locale: locale,
+ );
+ }
}
diff --git a/lib/service/persistence_service.dart b/lib/service/persistence_service.dart
index 1eb6a099..ee513cd6 100644
--- a/lib/service/persistence_service.dart
+++ b/lib/service/persistence_service.dart
@@ -1,10 +1,12 @@
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
+import 'package:localsend_app/gen/strings.g.dart';
import 'package:localsend_app/util/alias_generator.dart';
import 'package:shared_preferences/shared_preferences.dart';
const _aliasKey = 'alias';
const _themeKey = 'theme';
+const _localeKey = 'locale';
/// This service abstracts the persistence layer.
class PersistenceService {
@@ -45,4 +47,20 @@ class PersistenceService {
Future<void> setTheme(ThemeMode theme) async {
await _prefs.setString(_themeKey, theme.name);
}
+
+ AppLocale? getLocale() {
+ final value = _prefs.getString(_localeKey);
+ if (value == null) {
+ return null;
+ }
+ return AppLocale.values.firstWhereOrNull((locale) => locale.languageTag == value);
+ }
+
+ Future<void> setLocale(AppLocale? locale) async {
+ if (locale == null) {
+ await _prefs.remove(_localeKey);
+ } else {
+ await _prefs.setString(_localeKey, locale.languageTag);
+ }
+ }
}