refactor: enhance localization support in chat and voice input features

- Integrated localization for various dialog messages and UI elements in the chat and voice input components.
- Updated the confirmation dialog to utilize localized strings for delete messages, improving user experience across different languages.
- Enhanced voice input sheet to reflect localized text for status updates, action buttons, and prompts, ensuring consistency in user interactions.
- Improved the file attachment widget to display the attachment label in a localized manner, enhancing accessibility for users in different regions.
- Streamlined localization management by centralizing string retrieval, promoting maintainability and clarity in the codebase.
This commit is contained in:
cogwheel0
2025-10-05 00:05:58 +05:30
parent 072453d588
commit 8629e1e039
14 changed files with 455 additions and 66 deletions

View File

@@ -11,6 +11,8 @@ import 'package:flutter_math_fork/flutter_math.dart';
import 'package:markdown/markdown.dart' as md;
import 'package:webview_flutter/webview_flutter.dart';
import 'package:conduit/l10n/app_localizations.dart';
import '../../theme/color_tokens.dart';
import '../../theme/theme_extensions.dart';
import 'code_block_header.dart';
@@ -76,18 +78,10 @@ class ConduitMarkdown {
return MarkdownStyleSheet(
p: baseBody,
h1: AppTypography.headlineLargeStyle.copyWith(
color: theme.textPrimary,
),
h2: AppTypography.headlineMediumStyle.copyWith(
color: theme.textPrimary,
),
h3: AppTypography.headlineSmallStyle.copyWith(
color: theme.textPrimary,
),
h4: AppTypography.bodyLargeStyle.copyWith(
color: theme.textPrimary,
),
h1: AppTypography.headlineLargeStyle.copyWith(color: theme.textPrimary),
h2: AppTypography.headlineMediumStyle.copyWith(color: theme.textPrimary),
h3: AppTypography.headlineSmallStyle.copyWith(color: theme.textPrimary),
h4: AppTypography.bodyLargeStyle.copyWith(color: theme.textPrimary),
h5: baseBody.copyWith(fontWeight: FontWeight.w600),
h6: secondaryBody,
a: baseBody.copyWith(
@@ -122,7 +116,10 @@ class ConduitMarkdown {
listIndent: Spacing.lg,
tableHead: secondaryBody.copyWith(fontWeight: FontWeight.w600),
tableBody: secondaryBody,
tableBorder: TableBorder.all(color: borderColor, width: BorderWidth.micro),
tableBorder: TableBorder.all(
color: borderColor,
width: BorderWidth.micro,
),
tableHeadAlign: TextAlign.start,
tableColumnWidth: const FlexColumnWidth(),
tableCellsPadding: const EdgeInsets.symmetric(
@@ -131,10 +128,7 @@ class ConduitMarkdown {
),
horizontalRuleDecoration: BoxDecoration(
border: Border(
top: BorderSide(
color: theme.dividerColor,
width: BorderWidth.small,
),
top: BorderSide(color: theme.dividerColor, width: BorderWidth.small),
),
),
);
@@ -153,9 +147,7 @@ class ConduitMarkdown {
}
static List<md.InlineSyntax> _buildInlineSyntaxes() {
return [
_LatexInlineSyntax(),
];
return [_LatexInlineSyntax()];
}
static Widget buildMermaidBlock(BuildContext context, String code) {
@@ -243,7 +235,9 @@ class ConduitMarkdown {
textAlign: TextAlign.left,
textDirection: TextDirection.ltr,
textWidthBasis: TextWidthBasis.parent,
style: AppTypography.codeStyle.copyWith(color: conduitTheme.codeText),
style: AppTypography.codeStyle.copyWith(
color: conduitTheme.codeText,
),
),
],
),
@@ -279,8 +273,12 @@ class _CodeBlockBuilder extends MarkdownElementBuilder {
final theme = context.conduitTheme;
final isDark = Theme.of(context).brightness == Brightness.dark;
final code = element.textContent;
final language = element.attributes['class']?.replaceFirst('language-', '') ?? 'plaintext';
final normalizedLanguage = language.trim().isEmpty ? 'plaintext' : language.trim();
final language =
element.attributes['class']?.replaceFirst('language-', '') ??
'plaintext';
final normalizedLanguage = language.trim().isEmpty
? 'plaintext'
: language.trim();
// Match GitHub/Atom theme colors for code block container
final codeBackground = isDark
@@ -306,9 +304,12 @@ class _CodeBlockBuilder extends MarkdownElementBuilder {
return;
}
ScaffoldMessenger.of(context).hideCurrentSnackBar();
final l10n = AppLocalizations.of(context);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Code copied to clipboard.'),
SnackBar(
content: Text(
l10n?.codeCopiedToClipboard ?? 'Code copied to clipboard.',
),
),
);
},
@@ -365,7 +366,10 @@ class _ImageBuilder extends MarkdownElementBuilder {
try {
final commaIndex = dataUrl.indexOf(',');
if (commaIndex == -1) {
throw const FormatException('Invalid data URL format');
throw FormatException(
AppLocalizations.of(context)?.invalidDataUrl ??
'Invalid data URL format',
);
}
final base64String = dataUrl.substring(commaIndex + 1);
@@ -421,10 +425,7 @@ class _ImageBuilder extends MarkdownElementBuilder {
);
}
Widget _buildImageError(
BuildContext context,
ConduitThemeExtension theme,
) {
Widget _buildImageError(BuildContext context, ConduitThemeExtension theme) {
return Container(
height: 120,
decoration: BoxDecoration(
@@ -467,9 +468,8 @@ class _LatexBuilder extends MarkdownElementBuilder {
final content = element.textContent.trim();
final isInline = element.attributes['isInline'] == 'true';
final baseStyle = (preferredStyle ?? AppTypography.bodyMediumStyle).copyWith(
color: isDark ? Colors.white : Colors.black,
);
final baseStyle = (preferredStyle ?? AppTypography.bodyMediumStyle)
.copyWith(color: isDark ? Colors.white : Colors.black);
if (content.isEmpty) {
return Text(element.textContent, style: baseStyle);
@@ -499,9 +499,9 @@ class _LatexBuilder extends MarkdownElementBuilder {
// LaTeX inline syntax
class _LatexInlineSyntax extends md.InlineSyntax {
_LatexInlineSyntax()
: super(
r'(\$\$[\s\S]+?\$\$)|(\$[^\n]+?\$)|(\\\([\s\S]+?\\\))|(\\\[[\s\S]+?\\\])',
);
: super(
r'(\$\$[\s\S]+?\$\$)|(\$[^\n]+?\$)|(\\\([\s\S]+?\\\))|(\\\[[\s\S]+?\\\])',
);
@override
bool onMatch(md.InlineParser parser, Match match) {

View File

@@ -1,4 +1,7 @@
import 'package:flutter/material.dart';
import 'package:conduit/l10n/app_localizations.dart';
import '../theme/theme_extensions.dart';
/// Centralized helper for building themed dialogs consistently
@@ -31,11 +34,14 @@ class ThemedDialogs {
BuildContext context, {
required String title,
required String message,
String confirmText = 'Confirm',
String cancelText = 'Cancel',
String? confirmText,
String? cancelText,
bool isDestructive = false,
bool barrierDismissible = true,
}) async {
final l10n = AppLocalizations.of(context);
final effectiveConfirmText = confirmText ?? l10n?.confirm ?? 'Confirm';
final effectiveCancelText = cancelText ?? l10n?.cancel ?? 'Cancel';
final result = await showDialog<bool>(
context: context,
barrierDismissible: barrierDismissible,
@@ -50,7 +56,7 @@ class ThemedDialogs {
TextButton(
onPressed: () => Navigator.of(ctx).pop(false),
child: Text(
cancelText,
effectiveCancelText,
style: TextStyle(color: ctx.conduitTheme.textSecondary),
),
),
@@ -62,7 +68,7 @@ class ThemedDialogs {
: ctx.conduitTheme.buttonPrimary,
),
child: Text(
confirmText,
effectiveConfirmText,
style: TextStyle(
color: isDestructive
? ctx.conduitTheme.error
@@ -103,8 +109,8 @@ class ThemedDialogs {
required String title,
required String hintText,
String? initialValue,
String confirmText = 'Save',
String cancelText = 'Cancel',
String? confirmText,
String? cancelText,
bool barrierDismissible = true,
TextInputType? keyboardType,
TextCapitalization textCapitalization = TextCapitalization.sentences,
@@ -112,6 +118,9 @@ class ThemedDialogs {
}) async {
final theme = context.conduitTheme;
final controller = TextEditingController(text: initialValue ?? '');
final l10n = AppLocalizations.of(context);
final effectiveConfirmText = confirmText ?? l10n?.save ?? 'Save';
final effectiveCancelText = cancelText ?? l10n?.cancel ?? 'Cancel';
String? result = await showDialog<String>(
context: context,
@@ -169,7 +178,7 @@ class ThemedDialogs {
TextButton(
onPressed: () => Navigator.of(ctx).pop(),
child: Text(
cancelText,
effectiveCancelText,
style: TextStyle(color: theme.textSecondary),
),
),
@@ -185,7 +194,7 @@ class ThemedDialogs {
? () => Navigator.of(ctx).pop(trimmed)
: null,
child: Text(
confirmText,
effectiveConfirmText,
style: TextStyle(
color: enabled
? theme.buttonPrimary