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

+30 -29
View File
@@ -13,6 +13,7 @@ import 'api/client.dart';
import 'api/models.dart';
import 'api/pairing.dart';
import 'api/relay_transport.dart';
import 'l10n.dart';
import 'storage.dart';
import 'terminal_state.dart';
@@ -174,7 +175,7 @@ class AppState extends ChangeNotifier {
/// The name of the connected server, for the sidebar footer.
String get connectionName {
final c = conn;
if (c == null) return '未连接';
if (c == null) return tr('未连接');
final label = c.label.trim();
if (label.isNotEmpty) return label;
final host = Uri.tryParse(c.url)?.host ?? '';
@@ -253,7 +254,7 @@ class AppState extends ChangeNotifier {
} catch (e) {
if (client == cl) {
followUpBehavior = before;
_lastError = '保存失败:$e';
_lastError = trArgs('保存失败:{0}', [e]);
notifyListeners();
}
return false;
@@ -300,7 +301,7 @@ class AppState extends ChangeNotifier {
final payload = parsePairingLink(link);
if (payload == null) {
debugPrint('pair: invalid link (${link.length} chars)');
_lastError = '配对链接无效或已过期';
_lastError = tr('配对链接无效或已过期');
notifyListeners();
return false;
}
@@ -337,7 +338,7 @@ class AppState extends ChangeNotifier {
? const Duration(seconds: 30)
: const Duration(seconds: 15));
if (redeemed.token.isEmpty) {
throw ApiError(401, '服务器没有返回设备令牌');
throw ApiError(401, tr('服务器没有返回设备令牌'));
}
final connection = _pairingConnection(candidate, payload, redeemed.response);
await storage.saveToken(connection.id, redeemed.token);
@@ -358,7 +359,7 @@ class AppState extends ChangeNotifier {
}
}
busy = false;
final message = friendlyConnectError(failure ?? '配对失败');
final message = friendlyConnectError(failure ?? tr('配对失败'));
debugPrint('pair: failed: $message');
error = message;
_lastError = message;
@@ -389,7 +390,7 @@ class AppState extends ChangeNotifier {
if (candidate.isRelay) {
return Connection(
id: existing?.id ?? Storage.newId(),
label: label.isNotEmpty ? label : (existing?.label ?? 'Relay 服务器'),
label: label.isNotEmpty ? label : (existing?.label ?? tr('Relay 服务器')),
url: 'relay://${candidate.serverId}',
relayUrl: candidate.relayUrl,
serverId: candidate.serverId,
@@ -418,7 +419,7 @@ class AppState extends ChangeNotifier {
if (c.isRelay) {
// A relay connection is created by pairing; a missing token means it
// was never stored or was revoked.
throw ApiError(401, '配对已失效,请重新配对');
throw ApiError(401, tr('配对已失效,请重新配对'));
}
// No saved token: a passwordless server needs none.
try {
@@ -428,13 +429,13 @@ class AppState extends ChangeNotifier {
}
} on ApiError {
if (password.isEmpty) {
throw ApiError(401, '需要密码或已保存的凭据');
throw ApiError(401, tr('需要密码或已保存的凭据'));
}
}
if (token == null && password.isNotEmpty) {
final res = await cl.login(password, await storage.installId());
token = res['clientToken'] as String? ?? '';
if (token.isEmpty) throw ApiError(401, '登录成功但没有返回令牌');
if (token.isEmpty) throw ApiError(401, tr('登录成功但没有返回令牌'));
await storage.saveToken(c.id, token);
}
}
@@ -457,25 +458,25 @@ class AppState extends ChangeNotifier {
if (e is ApiError) {
if (e.status == 401 &&
e.message.toLowerCase().contains('invalid credentials')) {
return '密码错误,请重试';
return tr('密码错误,请重试');
}
return e.message;
}
if (e is ArgumentError) return e.message?.toString() ?? e.toString();
if (e is TimeoutException) return '连接超时:检查服务器与代理地址';
if (e is TimeoutException) return tr('连接超时:检查服务器与代理地址');
final s = e.toString();
if (e is SocketException) {
if (s.contains('Connection refused')) {
return '连接被拒绝:服务器或代理未启动,或端口不对';
return tr('连接被拒绝:服务器或代理未启动,或端口不对');
}
if (s.contains('Failed host lookup') || s.contains('nodename nor servname')) {
return '无法解析地址:检查服务器或代理主机名';
return tr('无法解析地址:检查服务器或代理主机名');
}
if (s.contains('Network is unreachable')) return '网络不可达';
if (s.contains('Network is unreachable')) return tr('网络不可达');
if (s.contains('Connection closed') || s.contains('reset by peer')) {
return '连接被断开:检查代理是否可用';
return tr('连接被断开:检查代理是否可用');
}
return '网络错误:$s';
return trArgs('网络错误:{0}', [s]);
}
return s;
}
@@ -702,10 +703,10 @@ class AppState extends ChangeNotifier {
cl.commands(directory: directory),
cl.skills(directory: directory),
]);
const local = [
final local = [
SlashItem(
name: 'compact',
description: '使用 AI 压缩会话历史以减少上下文大小',
description: tr('使用 AI 压缩会话历史以减少上下文大小'),
action: 'compact',
),
];
@@ -740,7 +741,7 @@ class AppState extends ChangeNotifier {
}
String projectLabelFor(String directory) {
if (directory == chatTarget || isChatDirectory(directory)) return '聊天';
if (directory == chatTarget || isChatDirectory(directory)) return tr('聊天');
if (directory.isEmpty) return '';
for (final project in projects) {
if (samePath(project.canonical, directory)) return project.label;
@@ -783,7 +784,7 @@ class AppState extends ChangeNotifier {
await openChat(session);
return true;
} catch (e) {
_lastError = '无法打开子代理会话:$e';
_lastError = trArgs('无法打开子代理会话:{0}', [e]);
notifyListeners();
return false;
}
@@ -826,11 +827,11 @@ class AppState extends ChangeNotifier {
/// project scope.
Future<String> createChatDirectory() async {
final cl = client;
if (cl == null) throw StateError('未连接服务器');
if (cl == null) throw StateError(tr('未连接服务器'));
if (chatsRoot == null || chatsRoot!.isEmpty) await refreshFsHome();
final root = chatsRoot;
if (root == null || root.isEmpty) {
throw StateError('服务器没有提供聊天目录');
throw StateError(tr('服务器没有提供聊天目录'));
}
final now = DateTime.now();
final date = '${now.year.toString().padLeft(4, '0')}-'
@@ -1007,8 +1008,8 @@ class AppState extends ChangeNotifier {
current.directory = target;
} catch (e) {
sendingDraft = false;
current.error =
'无法创建聊天目录:${e is ApiError ? e.message : e}';
current.error = trArgs(
'无法创建聊天目录:{0}', [e is ApiError ? e.message : e]);
notifyListeners();
return;
}
@@ -1118,7 +1119,7 @@ class AppState extends ChangeNotifier {
// Anything arriving means the stream is back (the server greets
// every connection with `server.connected`), so the "reconnecting"
// banner has done its job.
if (error == '连接中断,正在重连…') error = null;
if (error == tr('连接中断,正在重连…')) error = null;
if (catchUp) {
catchUp = false;
_resyncActivity();
@@ -1136,7 +1137,7 @@ class AppState extends ChangeNotifier {
await done.future;
if (client != cl || gen != _streamGen) return;
afterDrop = true;
error = '连接中断,正在重连…';
error = tr('连接中断,正在重连…');
notifyListeners();
await Future.delayed(const Duration(seconds: 2));
}
@@ -1788,7 +1789,7 @@ class ChatState extends ChangeNotifier {
type: 'user',
text: t,
created: DateTime.now().millisecondsSinceEpoch,
files: files?.map((f) => FileAttachment(f['name'] ?? '附件', '')).toList() ?? const [],
files: files?.map((f) => FileAttachment(f['name'] ?? tr('附件'), '')).toList() ?? const [],
));
_sortMsgs();
notifyListeners();
@@ -1822,7 +1823,7 @@ class ChatState extends ChangeNotifier {
type: 'user',
text: t,
created: DateTime.now().millisecondsSinceEpoch,
files: files?.map((f) => FileAttachment(f['name'] ?? '附件', '')).toList() ?? const [],
files: files?.map((f) => FileAttachment(f['name'] ?? tr('附件'), '')).toList() ?? const [],
));
_sortMsgs();
notifyListeners();
@@ -1857,7 +1858,7 @@ class ChatState extends ChangeNotifier {
delivery: delivery,
created: DateTime.now().millisecondsSinceEpoch,
files: files
?.map((f) => FileAttachment(f['name'] ?? '附件', ''))
?.map((f) => FileAttachment(f['name'] ?? tr('附件'), ''))
.toList() ??
const [],
);