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'; /// 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 = { 'English', '中文', // 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 _uiLiterals(String source) sync* { const quote = r'''(?:'(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*")'''; final patterns = [ 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.en); }); 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('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.en); await tester .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 UI string the code writes has a Chinese entry', () { final missing = {}; 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; 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/zh.dart (or to _notTranslated if they ' 'are not UI text):\n${missing.join('\n')}'); }); }