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

+71
View File
@@ -0,0 +1,71 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:openchamber/l10n.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.
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。',
};
void main() {
tearDown(() {
L10n.instance.apply(AppLanguage.zh);
});
test('English lookup falls back on the Chinese text it was given', () async {
L10n.instance.apply(AppLanguage.en);
expect(tr('保存'), isNot('保存'));
expect(tr('这不存在的文案'), '这不存在的文案');
expect(trArgs('{0} 个子代理', [3]), contains('3'));
expect(trArgs('{0} 条笔记', [1]), isNot(contains('条')));
});
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('保存'))));
expect(find.text('Save'), findsOneWidget);
});
test('every Chinese literal in lib has an English entry', () {
final quotes = [RegExp(r"'(?:\\.|[^'\\])*'"), RegExp(r'"(?:\\.|[^"\\])*"')];
final cjk = RegExp(r'[\u4e00-\u9fff]');
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);
}
}
}
}
expect(missing, isEmpty,
reason: 'Add these to lib/l10n/en_*.dart (or to _notTranslated if '
'they are not UI text):\n${missing.join('\n')}');
});
}