Files

96 lines
3.0 KiB
Dart

import 'package:flutter/material.dart';
import '../../../../l10n/app_localizations.dart';
import '../../../../shared/theme/design_tokens.dart';
import '../widgets/settings_section_widgets.dart';
class LanguageSettingsScreen extends StatelessWidget {
const LanguageSettingsScreen({super.key, required this.selectedLanguageTag});
final String selectedLanguageTag;
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
final colors = Theme.of(context).colorScheme;
final options = _buildLanguageOptions(l10n);
return Scaffold(
backgroundColor: colors.surfaceContainerLow,
appBar: AppBar(
title: Text(l10n.language),
centerTitle: true,
backgroundColor: colors.surfaceContainerLow,
surfaceTintColor: colors.surfaceContainerLow,
),
body: ListView(
padding: const EdgeInsets.all(AppSpacing.lg),
children: [
SectionLabel(text: l10n.settingsLanguageSection),
SettingsGroupCard(
children: [
for (int i = 0; i < options.length; i++)
SettingsMenuTile(
icon: Icons.language_rounded,
title: options[i].label,
subtitle: options[i].tag,
tint: colors.primary,
background: colors.surfaceContainerHighest,
showDivider: i != options.length - 1,
showChevron: false,
trailing: selectedLanguageTag == options[i].tag
? Icon(Icons.check_rounded, color: colors.primary)
: null,
onTap: () => Navigator.of(context).pop(options[i].tag),
),
],
),
],
),
);
}
List<({String tag, String label})> _buildLanguageOptions(
AppLocalizations l10n,
) {
final supportedLocales = AppLocalizations.supportedLocales;
return supportedLocales.map((locale) {
final tag = _localeToTag(locale);
final label = _getLocaleLabel(locale, l10n);
return (tag: tag, label: label);
}).toList();
}
String _localeToTag(Locale locale) {
final lang = locale.languageCode;
final script = locale.scriptCode;
final country = locale.countryCode;
if (script != null && country != null) {
return '$lang-$script-$country';
} else if (country != null) {
return '$lang-$country';
} else if (script != null) {
return '$lang-$script';
}
return _mapToBackendTag(lang);
}
String _mapToBackendTag(String flutterTag) {
const mapping = {'zh': 'zh-CN', 'en': 'en-US'};
return mapping[flutterTag] ?? flutterTag;
}
String _getLocaleLabel(Locale locale, AppLocalizations l10n) {
if (locale.languageCode == 'en') {
return l10n.english;
} else if (locale.languageCode == 'zh') {
if (locale.scriptCode == 'Hant') {
return '繁體中文';
}
return '简体中文';
}
return locale.languageCode;
}
}