mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-07 00:50:27 +00:00
feat: new ui (#1455)
This commit is contained in:
742 files changed
+59951
-5934
No files matched your search
@@ -0,0 +1,104 @@
|
||||
// Validate Flutter ARB translation catalogs before localization code generation.
|
||||
//
|
||||
// Usage (from ui/flutter):
|
||||
// dart run ../../.github/workflows/scripts/check_l10n.dart
|
||||
//
|
||||
// What it checks:
|
||||
// - Every ARB file uses the repository's canonical two-space JSON formatting.
|
||||
// - Every app_<locale>.arb file declares the matching @@locale value.
|
||||
// - Message values are not empty.
|
||||
// - Every locale has exactly the same message keys as app_en.arb.
|
||||
// - Every translation preserves the placeholder variables used by app_en.arb.
|
||||
//
|
||||
// Notes:
|
||||
// - This script does not judge translation quality.
|
||||
// - It does not generate localization code or fully parse ICU MessageFormat.
|
||||
// `flutter gen-l10n` runs immediately afterwards in CI to validate ICU syntax
|
||||
// and generate app_localizations*.dart.
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
/// Runs repository-specific checks that `flutter gen-l10n` does not enforce.
|
||||
void main() {
|
||||
final directory = Directory('lib/l10n');
|
||||
final templateFile = File('${directory.path}/app_en.arb');
|
||||
final template = _readArb(templateFile);
|
||||
final templateKeys = _messageKeys(template);
|
||||
var failed = false;
|
||||
|
||||
for (final file in directory.listSync().whereType<File>().where(
|
||||
(file) => file.path.endsWith('.arb'),
|
||||
)) {
|
||||
final messages = _readArb(file);
|
||||
if (!_hasCanonicalFormat(file, messages)) {
|
||||
stderr.writeln(
|
||||
'${file.path}: ARB formatting is not canonical (use two-space JSON indentation and a trailing newline)',
|
||||
);
|
||||
failed = true;
|
||||
}
|
||||
final expectedLocale = file.uri.pathSegments.last
|
||||
.replaceFirst('app_', '')
|
||||
.replaceFirst('.arb', '');
|
||||
if (messages['@@locale'] != expectedLocale) {
|
||||
stderr.writeln('${file.path}: @@locale must be $expectedLocale');
|
||||
failed = true;
|
||||
}
|
||||
final keys = _messageKeys(messages);
|
||||
final empty =
|
||||
keys.where((key) => (messages[key] as String).trim().isEmpty).toList()
|
||||
..sort();
|
||||
if (empty.isNotEmpty) {
|
||||
stderr.writeln('${file.path}: empty messages $empty');
|
||||
failed = true;
|
||||
}
|
||||
if (file.path == templateFile.path) continue;
|
||||
final missing = templateKeys.difference(keys);
|
||||
final extra = keys.difference(templateKeys);
|
||||
if (missing.isNotEmpty || extra.isNotEmpty) {
|
||||
stderr.writeln(
|
||||
'${file.path}: missing ${missing.toList()..sort()}, extra ${extra.toList()..sort()}',
|
||||
);
|
||||
failed = true;
|
||||
}
|
||||
|
||||
for (final key in templateKeys.intersection(keys)) {
|
||||
final expected = _placeholders(template[key] as String);
|
||||
final actual = _placeholders(messages[key] as String);
|
||||
if (!_sameSet(expected, actual)) {
|
||||
stderr.writeln(
|
||||
'${file.path}: $key placeholders $actual do not match $expected',
|
||||
);
|
||||
failed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (failed) exitCode = 1;
|
||||
}
|
||||
|
||||
/// Reads one ARB file as its top-level JSON object.
|
||||
Map<String, Object?> _readArb(File file) =>
|
||||
jsonDecode(file.readAsStringSync()) as Map<String, Object?>;
|
||||
|
||||
/// Checks the exact JSON layout so whitespace-only formatting drift fails CI.
|
||||
bool _hasCanonicalFormat(File file, Map<String, Object?> arb) {
|
||||
const encoder = JsonEncoder.withIndent(' ');
|
||||
return file.readAsStringSync() == '${encoder.convert(arb)}\n';
|
||||
}
|
||||
|
||||
/// Returns user-facing message keys and excludes ARB metadata keys (`@`/`@@`).
|
||||
Set<String> _messageKeys(Map<String, Object?> arb) =>
|
||||
arb.keys.where((key) => !key.startsWith('@')).toSet();
|
||||
|
||||
/// Extracts both simple placeholders (`{name}`) and ICU selector variables.
|
||||
///
|
||||
/// For example, this returns `count` from
|
||||
/// `{count, plural, =1{1 file} other{{count} files}}`.
|
||||
Set<String> _placeholders(String message) => RegExp(
|
||||
r'\{([A-Za-z_][A-Za-z0-9_]*)(?=\s*(?:\}|,\s*(?:plural|select|selectordinal)\s*,))',
|
||||
).allMatches(message).map((match) => match.group(1)!).toSet();
|
||||
|
||||
/// Compares sets without depending on iteration order.
|
||||
bool _sameSet(Set<String> left, Set<String> right) =>
|
||||
left.length == right.length && left.containsAll(right);
|
||||
@@ -10,7 +10,7 @@
|
||||
// (including the common pattern where Flutter concatenates the base URL with a relative path)
|
||||
// - Download them into: build/web/assets/gstatic/<path>
|
||||
// - Rewrite "https://fonts.gstatic.com/s/" -> "assets/gstatic/" so runtime loads locally
|
||||
// - Only download font subsets needed by supported locales (parsed from message.dart)
|
||||
// - Only download font subsets needed by supported locales (parsed from lib/l10n/*.arb)
|
||||
//
|
||||
// Notes:
|
||||
// - This script DOES NOT call `flutter build web`. It only patches the output.
|
||||
@@ -23,15 +23,18 @@ import 'dart:io';
|
||||
/// Uses language code prefix to automatically detect the script system.
|
||||
/// Returns empty set if base fonts (Latin/Cyrillic/Greek) are sufficient.
|
||||
Set<String> _getFontFamiliesForLocale(String locale) {
|
||||
final lang = locale.toLowerCase().split('_').first;
|
||||
final normalizedLocale = locale.toLowerCase().replaceAll('-', '_');
|
||||
final lang = normalizedLocale.split('_').first;
|
||||
|
||||
// CJK languages - need specific large font files
|
||||
if (lang == 'zh') {
|
||||
// Simplified vs Traditional Chinese
|
||||
if (locale.contains('cn') || locale.contains('sg')) {
|
||||
return {'notosanssc'}; // Simplified Chinese
|
||||
if (normalizedLocale.contains('_tw') ||
|
||||
normalizedLocale.contains('_hk') ||
|
||||
normalizedLocale.contains('_mo')) {
|
||||
return {'notosanstc', 'notosanshk'}; // Traditional Chinese (TW, HK, MO)
|
||||
}
|
||||
return {'notosanstc', 'notosanshk'}; // Traditional Chinese (TW, HK, MO)
|
||||
return {'notosanssc'}; // Simplified Chinese, including generic zh
|
||||
}
|
||||
if (lang == 'ja') return {'notosansjp'}; // Japanese
|
||||
if (lang == 'ko') return {'notosanskr'}; // Korean
|
||||
@@ -95,29 +98,25 @@ const _baseFontFamilies = <String>{
|
||||
// to reduce bundle size. Add them back if emoji support is needed.
|
||||
};
|
||||
|
||||
/// Parse supported locales from message.dart file.
|
||||
/// Reads the import statements and extracts locale codes like 'zh_cn', 'en_us', etc.
|
||||
Set<String> _parseSupportedLocales(File messageFile) {
|
||||
/// Parse supported locales from Flutter gen-l10n ARB filenames.
|
||||
/// Filenames follow app_<locale>.arb, for example app_zh_CN.arb or app_en.arb.
|
||||
Set<String> _parseSupportedLocales(Directory l10nDirectory) {
|
||||
final locales = <String>{};
|
||||
|
||||
if (!messageFile.existsSync()) {
|
||||
_fail('Warning: message.dart not found, only base fonts will be used');
|
||||
if (!l10nDirectory.existsSync()) {
|
||||
_fail('Localization directory not found: ${l10nDirectory.path}');
|
||||
}
|
||||
|
||||
final content = messageFile.readAsStringSync();
|
||||
|
||||
// Match import statements like: import 'langs/zh_cn.dart';
|
||||
final importRegex = RegExp(r"import\s+'langs/(\w+)\.dart'");
|
||||
for (final match in importRegex.allMatches(content)) {
|
||||
final locale = match.group(1);
|
||||
if (locale != null) {
|
||||
locales.add(locale);
|
||||
}
|
||||
final arbName = RegExp(r'^app_(.+)\.arb$');
|
||||
for (final entity in l10nDirectory.listSync()) {
|
||||
if (entity is! File) continue;
|
||||
final filename = entity.uri.pathSegments.last;
|
||||
final locale = arbName.firstMatch(filename)?.group(1);
|
||||
if (locale != null && locale.isNotEmpty) locales.add(locale);
|
||||
}
|
||||
|
||||
if (locales.isEmpty) {
|
||||
_fail(
|
||||
'Warning: No locales found in message.dart, only base fonts will be used');
|
||||
_fail('No app_<locale>.arb files found in ${l10nDirectory.path}');
|
||||
}
|
||||
|
||||
return locales;
|
||||
@@ -139,7 +138,9 @@ Set<String> _getRequiredFontFamilies(Set<String> locales) {
|
||||
/// Font paths have the format: "fontfamily/version/filename.ext"
|
||||
/// Example: "notosanssc/v36/xxx.ttf" -> font family is "notosanssc"
|
||||
bool _fontMatchesRequiredFamilies(
|
||||
String fontPath, Set<String> requiredFamilies) {
|
||||
String fontPath,
|
||||
Set<String> requiredFamilies,
|
||||
) {
|
||||
// Extract font family from path (first segment before '/')
|
||||
final slashIndex = fontPath.indexOf('/');
|
||||
if (slashIndex == -1) {
|
||||
@@ -158,10 +159,9 @@ Future<void> main(List<String> args) async {
|
||||
// but will also try to locate pubspec.yaml by walking up.
|
||||
final flutterDir = _findFlutterProjectRoot(Directory.current);
|
||||
|
||||
// Parse supported locales from message.dart
|
||||
final messageFile =
|
||||
File(_join(flutterDir.path, 'lib', 'i18n', 'message.dart'));
|
||||
final supportedLocales = _parseSupportedLocales(messageFile);
|
||||
// Parse supported locales from Flutter gen-l10n ARB files.
|
||||
final l10nDirectory = Directory(_join(flutterDir.path, 'lib', 'l10n'));
|
||||
final supportedLocales = _parseSupportedLocales(l10nDirectory);
|
||||
stdout.writeln('Supported locales: ${supportedLocales.join(', ')}');
|
||||
|
||||
// Get required font families based on supported locales
|
||||
@@ -203,7 +203,8 @@ Future<void> main(List<String> args) async {
|
||||
if (p.contains('://') || p.startsWith('data:')) continue;
|
||||
if (p.startsWith('/') ||
|
||||
p.startsWith('assets/') ||
|
||||
p.startsWith('packages/')) continue;
|
||||
p.startsWith('packages/'))
|
||||
continue;
|
||||
if (p.contains('..')) continue;
|
||||
relAssetsUnderS.add(p);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user