feat: 多语言提取器

This commit is contained in:
2024-03-14 21:49:21 +08:00
parent 8426570ced
commit a84852d4f0
16 changed files with 432 additions and 17 deletions

7
packages/sct_dev_tools/.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/
# Avoid committing pubspec.lock for library packages; see
# https://dart.dev/guides/libraries/private-files#pubspeclock.
pubspec.lock

View File

@ -0,0 +1,3 @@
## 1.0.0
- Initial version.

View File

@ -0,0 +1,39 @@
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.
For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/guides/libraries/writing-package-pages).
For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-library-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/developing-packages).
-->
TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.
## Features
TODO: List what your package can do. Maybe include images, gifs, or videos.
## Getting started
TODO: List prerequisites and provide or point to information on how to
start using the package.
## Usage
TODO: Include short and useful examples for package users. Add longer examples
to `/example` folder.
```dart
const like = 'sample';
```
## Additional information
TODO: Tell users more about the package: where to find more information, how to
contribute to the package, how to file issues, what response they can expect
from the package authors, and more.

View File

@ -0,0 +1,30 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.
include: package:lints/recommended.yaml
# Uncomment the following section to specify additional rules.
# linter:
# rules:
# - camel_case_types
# analyzer:
# exclude:
# - path/to/excluded/files/**
# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints
# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options

View File

@ -0,0 +1,117 @@
import 'dart:convert';
import 'dart:io';
import 'package:analyzer/dart/analysis/utilities.dart';
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:uuid/v4.dart';
final stringResult = <String>[];
class AutoL10nTools {
void genL10nFiles() {
final dir = Directory('lib/ui');
for (var entity in dir.listSync(recursive: true)) {
if (entity is File && entity.path.endsWith('.dart')) {
print('Processing ${entity.path}...');
_processDartFile(entity);
}
}
if (stringResult.isNotEmpty) {
final outputMap = <String, String>{};
for (var value in stringResult) {
if (outputMap.containsValue(value)) {
continue;
}
final key = UuidV4().generate();
outputMap[key] = value;
}
// output to json
final j = json.encode(outputMap);
File("./lib/generated/l10_temp.json").writeAsStringSync(j);
print(
"output to json file (length: ${outputMap.length}): ./lib/generated/l10n_temp.json");
}
// output to json
}
void _processDartFile(File file) {
final parseResult = parseFile(
path: file.path, featureSet: FeatureSet.latestLanguageVersion());
final unit = parseResult.unit;
unit.accept(MyAstVisitor());
}
}
class MyAstVisitor extends GeneralizingAstVisitor {
@override
visitStringLiteral(StringLiteral node) {
final value = node.stringValue ?? "";
if (containsChinese(value)) {
print('Found->visitStringLiteral: $value');
addStringResult(value);
}
return super.visitStringLiteral(node);
}
@override
visitAdjacentStrings(AdjacentStrings node) {
int interpolationIndex = 0;
var result = '';
for (var string in node.strings) {
if (string is SimpleStringLiteral) {
result += string.value;
} else if (string is StringInterpolation) {
for (var element in string.elements) {
if (element is InterpolationString) {
result += element.value;
} else if (element is InterpolationExpression) {
result += '{{${interpolationIndex++}}}';
}
}
}
}
if (containsChinese(result)) {
print('Found->visitAdjacentStrings: $result');
addStringResult(result);
}
return super.visitAdjacentStrings(node);
}
@override
visitStringInterpolation(StringInterpolation node) {
int interpolationIndex = 0;
var result = '';
for (var element in node.elements) {
if (element is InterpolationString) {
result += element.value;
} else if (element is InterpolationExpression) {
result += '{{${interpolationIndex++}}}';
}
}
if (containsChinese(result)) {
print('Found->visitStringInterpolation: $result');
addStringResult(result);
}
return super.visitStringInterpolation(node);
}
@override
visitInterpolationExpression(InterpolationExpression node) {
int interpolationIndex = 0;
final value = '{{${interpolationIndex++}}}';
if (containsChinese(value)) {
print('Found->visitInterpolationExpression: $value');
addStringResult(value);
}
return super.visitInterpolationExpression(node);
}
bool containsChinese(String input) {
return RegExp(r'[\u4e00-\u9fa5]').hasMatch(input);
}
addStringResult(String value) {
stringResult.add(value);
}
}

View File

@ -0,0 +1,10 @@
import 'auto_l10n.dart';
void main(List<String> args) {
switch (args.elementAtOrNull(0)) {
case "gen":
return AutoL10nTools().genL10nFiles();
default:
throw Exception("cmd not found");
}
}

View File

@ -0,0 +1,17 @@
name: sct_dev_tools
description: A starting point for Dart libraries or applications.
version: 1.0.0
# repository: https://github.com/my_org/my_repo
environment:
sdk: ^3.3.1
# Add regular dependencies here.
dependencies:
# path: ^1.8.0
analyzer: ^6.4.1
uuid: ^4.3.3
dev_dependencies:
lints: ^3.0.0
test: ^1.24.0