2026-11-26 09:04:25 +08:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
|
import 'package:openchamber/l10n.dart';
|
2026-11-26 11:48:16 +08:00
|
|
|
import 'package:openchamber/l10n/zh.dart';
|
2026-11-26 09:04:25 +08:00
|
|
|
|
2026-11-26 11:48:16 +08:00
|
|
|
/// 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).
|
2026-11-26 09:04:25 +08:00
|
|
|
const _notTranslated = <String>{
|
|
|
|
|
'English',
|
2026-11-26 11:48:16 +08:00
|
|
|
'中文',
|
2026-11-26 17:18:09 +08:00
|
|
|
'Chamber Go',
|
2026-11-26 11:48:16 +08:00
|
|
|
// Product names, separators and glyphs read the same everywhere.
|
|
|
|
|
'OpenChamber',
|
|
|
|
|
'OpenCode',
|
|
|
|
|
' / ',
|
|
|
|
|
'·',
|
|
|
|
|
'🖼',
|
|
|
|
|
' *',
|
2026-11-26 09:04:25 +08:00
|
|
|
};
|
|
|
|
|
|
2026-11-26 11:48:16 +08:00
|
|
|
/// 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-26 09:04:25 +08:00
|
|
|
void main() {
|
|
|
|
|
tearDown(() {
|
2026-11-26 11:48:16 +08:00
|
|
|
L10n.instance.apply(AppLanguage.en);
|
2026-11-26 09:04:25 +08:00
|
|
|
});
|
|
|
|
|
|
2026-11-26 11:48:16 +08:00
|
|
|
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', () {
|
2026-11-26 09:04:25 +08:00
|
|
|
L10n.instance.apply(AppLanguage.en);
|
2026-11-26 11:48:16 +08:00
|
|
|
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');
|
2026-11-26 09:04:25 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
testWidgets('AppText follows the chosen language', (tester) async {
|
|
|
|
|
L10n.instance.apply(AppLanguage.en);
|
|
|
|
|
await tester
|
2026-11-26 11:48:16 +08:00
|
|
|
.pumpWidget(const MaterialApp(home: Scaffold(body: AppText('Save'))));
|
2026-11-26 09:04:25 +08:00
|
|
|
expect(find.text('Save'), findsOneWidget);
|
2026-11-26 11:48:16 +08:00
|
|
|
|
|
|
|
|
L10n.instance.apply(AppLanguage.zh);
|
|
|
|
|
await tester
|
|
|
|
|
.pumpWidget(const MaterialApp(home: Scaffold(body: AppText('Save'))));
|
|
|
|
|
expect(find.text('保存'), findsOneWidget);
|
2026-11-26 09:04:25 +08:00
|
|
|
});
|
|
|
|
|
|
2026-11-26 11:48:16 +08:00
|
|
|
test('every UI string the code writes has a Chinese entry', () {
|
2026-11-26 09:04:25 +08:00
|
|
|
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;
|
2026-11-26 11:48:16 +08:00
|
|
|
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);
|
2026-11-26 09:04:25 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
expect(missing, isEmpty,
|
2026-11-26 11:48:16 +08:00
|
|
|
reason: 'Add these to lib/l10n/zh.dart (or to _notTranslated if they '
|
|
|
|
|
'are not UI text):\n${missing.join('\n')}');
|
2026-11-26 09:04:25 +08:00
|
|
|
});
|
|
|
|
|
}
|