feat: write the UI in English, select only conversation content
- source strings are English again; Chinese comes from lib/l10n/zh.dart through tr()/trArgs() and AppText, and a language without a table (Spanish, ...) reads English instead of Chinese - server and user data (titles, messages, file names, notes, task names) is drawn with plain Text, so it is never looked up in the table - SelectionArea moved from the whole message list to the messages, the answer text, an opened tool call's details and the turn footer; status lines and folded tool rows no longer start a selection - a message the transcript does not know (its start event was lost while the stream was down) fetches the tail once, so a turn continued from another client comes in with its prompt - bump to 0.5.3
This commit is contained in:
1 parent
7b5bccf28f
commit
511c3b006b
43 files changed
+1620
-1791
No files matched your search
+76
-39
@@ -3,69 +3,106 @@ import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:openchamber/l10n.dart';
|
||||
import 'package:openchamber/l10n/zh.dart';
|
||||
|
||||
/// Chinese literals that are not UI text: patterns matched against server
|
||||
/// data, protocol values, and the two language names that read the same in
|
||||
/// both languages.
|
||||
/// Strings that are deliberately the same in both languages, or not UI text
|
||||
/// at all (a screen reader label, a path pattern built at runtime).
|
||||
const _notTranslated = <String>{
|
||||
r'^终端 (\d+)$',
|
||||
'中文',
|
||||
'English',
|
||||
// The two halves of one key: both sentences are written as adjacent
|
||||
// literals in servers_screen and in en_servers, and only the joined text is
|
||||
// a key.
|
||||
'在 OpenChamber 网页端打开「连接设备」,粘贴链接或直接扫描二维码。',
|
||||
'链接里带一次性密钥,以及直连地址或私有 Relay。',
|
||||
'中文',
|
||||
// Product names, separators and glyphs read the same everywhere.
|
||||
'OpenChamber',
|
||||
'OpenCode',
|
||||
' / ',
|
||||
'·',
|
||||
'🖼',
|
||||
' *',
|
||||
};
|
||||
|
||||
/// The literal arguments of AppText, tr and trArgs in [source]: the strings
|
||||
/// our own code writes and therefore the ones a translation exists for.
|
||||
Iterable<String> _uiLiterals(String source) sync* {
|
||||
const quote = r'''(?:'(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*")''';
|
||||
final patterns = <RegExp>[
|
||||
RegExp('AppText\\(\\s*(?:$quote)(?:\\s*(?:$quote))*'),
|
||||
RegExp(r'\btr\(\s*(?:' + quote + ')'),
|
||||
RegExp(r'\btrArgs\(\s*(?:' + quote + r')(?:\s*(?:' + quote + '))*'),
|
||||
];
|
||||
final single = RegExp(quote);
|
||||
for (final pattern in patterns) {
|
||||
for (final match in pattern.allMatches(source)) {
|
||||
final call = match.group(0)!;
|
||||
final joined = single
|
||||
.allMatches(call)
|
||||
.map((m) => m.group(0)!.substring(1, m.group(0)!.length - 1))
|
||||
.map((s) => s.replaceAll(r'\n', '\n').replaceAll(r"\'", "'"))
|
||||
.join();
|
||||
// Interpolated text is built at runtime, not a translation key.
|
||||
if (joined.contains(r'$')) continue;
|
||||
yield joined;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
tearDown(() {
|
||||
L10n.instance.apply(AppLanguage.zh);
|
||||
L10n.instance.apply(AppLanguage.en);
|
||||
});
|
||||
|
||||
test('English lookup falls back on the Chinese text it was given', () async {
|
||||
test('Chinese lookup falls back on the English it was given', () {
|
||||
L10n.instance.apply(AppLanguage.zh);
|
||||
expect(tr('Save'), '保存');
|
||||
expect(tr('A text with no translation'), 'A text with no translation');
|
||||
expect(trArgs('{0} subagents', [3]), '3 个子代理');
|
||||
expect(trArgs('{0} notes', [1]), isNot(contains('notes')));
|
||||
});
|
||||
|
||||
test('English is the source, so it needs no table', () {
|
||||
L10n.instance.apply(AppLanguage.en);
|
||||
expect(tr('保存'), isNot('保存'));
|
||||
expect(tr('这不存在的文案'), '这不存在的文案');
|
||||
expect(trArgs('{0} 个子代理', [3]), contains('3'));
|
||||
expect(trArgs('{0} 条笔记', [1]), isNot(contains('条')));
|
||||
expect(tr('Save'), 'Save');
|
||||
expect(trArgs('{0} subagents', [3]), '3 subagents');
|
||||
});
|
||||
|
||||
test('a language without a table reads English', () {
|
||||
L10n.instance.apply(AppLanguage.zh);
|
||||
// Spanish follows the system language branch; the app has no Spanish
|
||||
// table, so everything is the English source.
|
||||
final dispatcher = TestWidgetsFlutterBinding.instance.platformDispatcher;
|
||||
dispatcher.localeTestValue = const Locale('es', 'ES');
|
||||
addTearDown(dispatcher.clearLocaleTestValue);
|
||||
L10n.instance.apply(AppLanguage.system);
|
||||
expect(L10n.instance.locale.languageCode, 'en');
|
||||
expect(tr('Save'), 'Save');
|
||||
expect(trArgs('{0} subagents', [3]), '3 subagents');
|
||||
});
|
||||
|
||||
testWidgets('AppText follows the chosen language', (tester) async {
|
||||
L10n.instance.apply(AppLanguage.zh);
|
||||
await tester
|
||||
.pumpWidget(const MaterialApp(home: Scaffold(body: AppText('保存'))));
|
||||
expect(find.text('保存'), findsOneWidget);
|
||||
|
||||
L10n.instance.apply(AppLanguage.en);
|
||||
await tester
|
||||
.pumpWidget(const MaterialApp(home: Scaffold(body: AppText('保存'))));
|
||||
.pumpWidget(const MaterialApp(home: Scaffold(body: AppText('Save'))));
|
||||
expect(find.text('Save'), findsOneWidget);
|
||||
|
||||
L10n.instance.apply(AppLanguage.zh);
|
||||
await tester
|
||||
.pumpWidget(const MaterialApp(home: Scaffold(body: AppText('Save'))));
|
||||
expect(find.text('保存'), findsOneWidget);
|
||||
});
|
||||
|
||||
test('every Chinese literal in lib has an English entry', () {
|
||||
final quotes = [RegExp(r"'(?:\\.|[^'\\])*'"), RegExp(r'"(?:\\.|[^"\\])*"')];
|
||||
final cjk = RegExp(r'[\u4e00-\u9fff]');
|
||||
test('every UI string the code writes has a Chinese entry', () {
|
||||
final missing = <String>{};
|
||||
for (final entity in Directory('lib').listSync(recursive: true)) {
|
||||
if (entity is! File || !entity.path.endsWith('.dart')) continue;
|
||||
if (entity.path.startsWith('lib/l10n')) continue;
|
||||
for (final line in entity.readAsLinesSync()) {
|
||||
if (line.trimLeft().startsWith('//')) continue;
|
||||
for (final quote in quotes) {
|
||||
for (final match in quote.allMatches(line)) {
|
||||
final raw = match.group(0)!.substring(1, match.group(0)!.length - 1);
|
||||
final text = raw.replaceAll(r'\n', '\n');
|
||||
if (!cjk.hasMatch(text)) continue;
|
||||
if (englishStrings.containsKey(text)) continue;
|
||||
if (_notTranslated.contains(text)) continue;
|
||||
missing.add(text);
|
||||
}
|
||||
}
|
||||
final source = entity.readAsStringSync();
|
||||
for (final text in _uiLiterals(source)) {
|
||||
if (text.trim().isEmpty) continue;
|
||||
if (chineseStrings.containsKey(text)) continue;
|
||||
if (_notTranslated.contains(text)) continue;
|
||||
missing.add(text);
|
||||
}
|
||||
}
|
||||
expect(missing, isEmpty,
|
||||
reason: 'Add these to lib/l10n/en_*.dart (or to _notTranslated if '
|
||||
'they are not UI text):\n${missing.join('\n')}');
|
||||
reason: 'Add these to lib/l10n/zh.dart (or to _notTranslated if they '
|
||||
'are not UI text):\n${missing.join('\n')}');
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user