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 = { 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 = {}; 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')}'); }); }