feat: English and Chinese, and selectable chat text

- settings → general → language picks system, Chinese or English; the
  choice is stored on the device and read before the first frame
- our Chinese strings are translated through tr()/trArgs() and AppText,
  with the English in lib/l10n/en_*.dart; a missing entry falls back to
  Chinese and a test checks that every literal in lib has one
- AppText rebuilds on a language change (it depends on the resolved
  locale), so const text and whole pages switch at once; the built-in
  date/time pickers and selection menu follow the same language
- server and user data (titles, messages, prompts, paths) is never
  translated
- the chat list sits in a SelectionArea: long-press selects message text
  and shows the copy/select-all menu
- bump to 0.5.2
This commit is contained in:
blumia committed 2026-11-26 09:04:25 +08:00
1 parent da4bfb6826
commit 7b5bccf28f
42 files changed
+1943 -735

No files matched your search

+57 -52
View File
@@ -14,13 +14,14 @@ import '../state.dart';
import '../widgets/markdown_view.dart';
import 'knowledge_panel.dart';
import 'terminal_panel.dart';
import '../l10n.dart';
Future<void> showFilesPanel(
BuildContext context, AppState app, String directory) {
return showGeneralDialog<void>(
context: context,
barrierDismissible: true,
barrierLabel: '文件',
barrierLabel: tr('文件'),
barrierColor: Colors.black54,
transitionDuration: const Duration(milliseconds: 220),
pageBuilder: (context, _, __) {
@@ -243,7 +244,8 @@ class _FilesPanelState extends State<FilesPanel> {
if (!mounted || _previewPath != entry.path) return;
setState(() {
_previewLoading = false;
_previewError = '文件较大(${_fmtSize(stat.size)}),请下载后查看';
_previewError = trArgs(
'文件较大({0}),请下载后查看', [_fmtSize(stat.size)]);
});
return;
}
@@ -316,7 +318,7 @@ class _FilesPanelState extends State<FilesPanel> {
);
if (uri != null) _toast('已保存');
} catch (e) {
_toast('保存失败:$e');
_toast(trArgs('保存失败:{0}', [e]));
}
}
@@ -349,23 +351,23 @@ class _FilesPanelState extends State<FilesPanel> {
final name = await showDialog<String>(
context: context,
builder: (context) => AlertDialog(
title: Text(title),
title: AppText(title),
content: TextField(
controller: controller,
autofocus: true,
decoration: InputDecoration(
labelText: '名称',
hintText: title == '新建文件夹' ? '例如 docs' : '例如 notes.md',
labelText: tr('名称'),
hintText: title == '新建文件夹' ? tr('例如 docs') : tr('例如 notes.md'),
),
onSubmitted: (v) => Navigator.of(context).pop(v),
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('取消')),
child: const AppText('取消')),
FilledButton(
onPressed: () => Navigator.of(context).pop(controller.text),
child: const Text('创建')),
child: const AppText('创建')),
],
),
);
@@ -373,7 +375,7 @@ class _FilesPanelState extends State<FilesPanel> {
final trimmed = name?.trim() ?? '';
if (trimmed.isEmpty) return null;
if (trimmed.contains('/') || trimmed.contains('\\')) {
_toast('名称里不能有斜杠');
_toast(tr('名称里不能有斜杠'));
return null;
}
return trimmed;
@@ -381,7 +383,7 @@ class _FilesPanelState extends State<FilesPanel> {
void _toast(String message) {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(message)));
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: AppText(message)));
}
// -------------------------------------------------------------- build
@@ -425,7 +427,7 @@ class _FilesPanelState extends State<FilesPanel> {
),
segments: [
for (var i = 0; i < labels.length; i++)
ButtonSegment(value: i, label: Text(labels[i])),
ButtonSegment(value: i, label: AppText(labels[i])),
],
selected: {_tab},
onSelectionChanged: (v) => setState(() => _tab = v.first),
@@ -433,7 +435,7 @@ class _FilesPanelState extends State<FilesPanel> {
),
IconButton(
icon: const Icon(Icons.close),
tooltip: '关闭侧栏',
tooltip: tr('关闭侧栏'),
onPressed: () => Navigator.of(context).maybePop(),
),
],
@@ -443,7 +445,7 @@ class _FilesPanelState extends State<FilesPanel> {
Widget _body(ThemeData theme) {
if (_root.isEmpty) {
return const Center(child: Text('这个会话没有关联目录'));
return const Center(child: AppText('这个会话没有关联目录'));
}
switch (_tab) {
case 1:
@@ -485,12 +487,12 @@ class _FilesPanelState extends State<FilesPanel> {
decoration: InputDecoration(
isDense: true,
prefixIcon: const Icon(Icons.search, size: 20),
hintText: '搜索当前目录…',
hintText: tr('搜索当前目录…'),
suffixIcon: _query.trim().isEmpty
? null
: IconButton(
icon: const Icon(Icons.travel_explore, size: 20),
tooltip: '搜索子目录',
tooltip: tr('搜索子目录'),
onPressed: _searching ? null : _deepSearch,
),
border: OutlineInputBorder(
@@ -513,7 +515,7 @@ class _FilesPanelState extends State<FilesPanel> {
height: 14,
child: CircularProgressIndicator(strokeWidth: 2)),
const SizedBox(width: 8),
Text('正在搜索子目录…(${_deepResults.length})',
AppText(trArgs('正在搜索子目录…({0})', [_deepResults.length]),
style: TextStyle(fontSize: 12, color: theme.hintColor)),
],
),
@@ -535,7 +537,7 @@ class _FilesPanelState extends State<FilesPanel> {
IconButton(
icon: Icon(preview ? Icons.arrow_back : Icons.arrow_upward,
size: 20),
tooltip: preview ? '返回' : '上一级',
tooltip: preview ? tr('返回') : tr('上一级'),
onPressed: preview
? () => setState(() => _previewPath = null)
: (_path == _root ? null : _up),
@@ -546,12 +548,12 @@ class _FilesPanelState extends State<FilesPanel> {
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(_previewName,
AppText(_previewName,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: const TextStyle(
fontSize: 13.5, fontWeight: FontWeight.w600)),
Text(_previewPath!,
AppText(_previewPath!,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
@@ -565,7 +567,7 @@ class _FilesPanelState extends State<FilesPanel> {
_crumb(theme, _rootName, () => _load(_root),
last: crumbs.isEmpty),
for (var i = 0; i < crumbs.length; i++) ...[
Text(' / ', style: TextStyle(color: theme.hintColor)),
AppText(' / ', style: TextStyle(color: theme.hintColor)),
_crumb(theme, crumbs[i], () => _goToCrumb(i),
last: i == crumbs.length - 1),
],
@@ -579,7 +581,7 @@ class _FilesPanelState extends State<FilesPanel> {
icon: Icon(_editing
? Icons.visibility_outlined
: Icons.edit_outlined),
tooltip: _editing ? '预览' : '编辑',
tooltip: _editing ? tr('预览') : tr('编辑'),
onPressed:
_saving ? null : () => setState(() => _editing = !_editing),
),
@@ -591,27 +593,27 @@ class _FilesPanelState extends State<FilesPanel> {
height: 18,
child: CircularProgressIndicator(strokeWidth: 2))
: const Icon(Icons.check),
tooltip: '保存',
tooltip: tr('保存'),
onPressed: _saving ? null : _save,
),
IconButton(
icon: const Icon(Icons.download_outlined),
tooltip: '下载',
tooltip: tr('下载'),
onPressed: _download,
),
] else if (_root.isNotEmpty) ...[
PopupMenuButton<bool>(
icon: const Icon(Icons.add),
tooltip: '新建',
tooltip: tr('新建'),
onSelected: (directory) => _newEntry(directory: directory),
itemBuilder: (_) => const [
PopupMenuItem(value: false, child: Text('新建文件')),
PopupMenuItem(value: true, child: Text('新建文件夹')),
PopupMenuItem(value: false, child: AppText('新建文件')),
PopupMenuItem(value: true, child: AppText('新建文件夹')),
],
),
PopupMenuButton<String>(
icon: const Icon(Icons.more_horiz),
tooltip: '更多',
tooltip: tr('更多'),
onSelected: (value) {
if (value == 'ignored') {
setState(() => _showIgnored = !_showIgnored);
@@ -624,7 +626,7 @@ class _FilesPanelState extends State<FilesPanel> {
CheckedPopupMenuItem(
value: 'ignored',
checked: _showIgnored,
child: const Text('显示被 git 忽略的文件'),
child: const AppText('显示被 git 忽略的文件'),
),
const PopupMenuItem(
value: 'refresh',
@@ -632,7 +634,7 @@ class _FilesPanelState extends State<FilesPanel> {
children: [
Icon(Icons.refresh, size: 18),
SizedBox(width: 10),
Text('刷新'),
AppText('刷新'),
],
),
),
@@ -651,7 +653,7 @@ class _FilesPanelState extends State<FilesPanel> {
borderRadius: BorderRadius.circular(6),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 4),
child: Text(
child: AppText(
text,
style: TextStyle(
fontSize: 13,
@@ -672,9 +674,9 @@ class _FilesPanelState extends State<FilesPanel> {
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(_error!, textAlign: TextAlign.center),
AppText(_error!, textAlign: TextAlign.center),
const SizedBox(height: 8),
TextButton(onPressed: () => _load(), child: const Text('重试')),
TextButton(onPressed: () => _load(), child: const AppText('重试')),
],
),
),
@@ -688,7 +690,7 @@ class _FilesPanelState extends State<FilesPanel> {
.toList();
if (filtered.isEmpty && _deepResults.isEmpty) {
return Center(
child: Text(_query.trim().isEmpty ? '目录为空' : '没有匹配的文件',
child: AppText(_query.trim().isEmpty ? '目录为空' : '没有匹配的文件',
style: TextStyle(color: theme.hintColor)),
);
}
@@ -699,7 +701,7 @@ class _FilesPanelState extends State<FilesPanel> {
if (_query.trim().isNotEmpty && _deepResults.isNotEmpty) ...[
Padding(
padding: const EdgeInsets.fromLTRB(16, 12, 16, 4),
child: Text('子目录结果',
child: AppText('子目录结果',
style: TextStyle(fontSize: 12, color: theme.hintColor)),
),
for (final e in _deepResults) _tile(theme, e, showPath: true),
@@ -711,7 +713,7 @@ class _FilesPanelState extends State<FilesPanel> {
filtered.isNotEmpty)
Padding(
padding: const EdgeInsets.fromLTRB(16, 12, 16, 0),
child: Text('子目录里没有匹配的文件',
child: AppText('子目录里没有匹配的文件',
style: TextStyle(fontSize: 12, color: theme.hintColor)),
),
],
@@ -728,10 +730,10 @@ class _FilesPanelState extends State<FilesPanel> {
color: entry.isDirectory
? theme.colorScheme.primary
: theme.colorScheme.onSurfaceVariant),
title: Text(entry.name,
title: AppText(entry.name,
maxLines: 1, overflow: TextOverflow.ellipsis, style: const TextStyle(fontSize: 14)),
subtitle: showPath
? Text(_relative(entry.path),
? AppText(_relative(entry.path),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 11, color: theme.hintColor))
@@ -768,10 +770,10 @@ class _FilesPanelState extends State<FilesPanel> {
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(_previewError!, textAlign: TextAlign.center),
AppText(_previewError!, textAlign: TextAlign.center),
const SizedBox(height: 8),
if (_previewBytes != null || _previewError != null)
TextButton(onPressed: _download, child: const Text('下载')),
TextButton(onPressed: _download, child: const AppText('下载')),
],
),
),
@@ -793,16 +795,16 @@ class _FilesPanelState extends State<FilesPanel> {
Icon(Icons.insert_drive_file_outlined,
size: 40, color: theme.hintColor),
const SizedBox(height: 10),
Text('这个文件无法预览',
AppText('这个文件无法预览',
style: TextStyle(color: theme.hintColor)),
const SizedBox(height: 6),
Text(_fmtSize(_previewBytes?.length ?? 0),
AppText(_fmtSize(_previewBytes?.length ?? 0),
style: TextStyle(fontSize: 12, color: theme.hintColor)),
const SizedBox(height: 8),
OutlinedButton.icon(
onPressed: _download,
icon: const Icon(Icons.download_outlined, size: 18),
label: const Text('下载'),
label: const AppText('下载'),
),
],
),
@@ -1000,12 +1002,12 @@ class _DownloadDialogState extends State<_DownloadDialog> {
? (_received / total).clamp(0.0, 1.0)
: null;
return AlertDialog(
title: const Text('正在下载'),
title: const AppText('正在下载'),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(widget.name,
AppText(widget.name,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: const TextStyle(fontSize: 13)),
@@ -1013,15 +1015,18 @@ class _DownloadDialogState extends State<_DownloadDialog> {
if (_error == null) ...[
LinearProgressIndicator(value: fraction),
const SizedBox(height: 8),
Text(
AppText(
total != null
? '${_size(_received)} / ${_size(total)}'
'(${(fraction! * 100).toStringAsFixed(0)}%)'
: '已下载 ${_size(_received)}',
? trArgs('{0} / {1}({2}%)', [
_size(_received),
_size(total),
(fraction! * 100).toStringAsFixed(0),
])
: trArgs('已下载 {0}', [_size(_received)]),
style: TextStyle(fontSize: 12, color: theme.hintColor),
),
] else
Text(_error!,
AppText(_error!,
style:
TextStyle(fontSize: 12.5, color: theme.colorScheme.error)),
],
@@ -1035,14 +1040,14 @@ class _DownloadDialogState extends State<_DownloadDialog> {
setState(() => _cancelling = true);
_cancel.cancel();
},
child: Text(_cancelling ? '正在取消…' : '取消'),
child: AppText(_cancelling ? '正在取消…' : '取消'),
)
else
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('关闭'),
child: const AppText('关闭'),
),
],
);
}
}
}