feat(i18n): define user copy manifest contract
parent
bc86c6a961
commit
5258310c2b
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\UserCopy;
|
||||
|
||||
/** Reports source patterns that must disappear as pages join the copy catalog. */
|
||||
final class UserCopyCoverageScanner
|
||||
{
|
||||
private const RULES = [
|
||||
'direct_toast_text' => '/\btoast\s*\(\s*[\'\"]/',
|
||||
'direct_confirm_text' => '/\b(?:window\.)?confirm\s*\(\s*[\'\"]/',
|
||||
'hardcoded_zh_locale' =>
|
||||
'/\.toLocale(?:String|DateString|TimeString)\s*\(\s*[\'\"]zh-CN[\'\"]/',
|
||||
'legacy_language_branch' => '/\$language\s*={2,3}\s*[\'\"]true[\'\"]/',
|
||||
'visible_chinese_literal' => '/\p{Han}/u',
|
||||
];
|
||||
|
||||
/**
|
||||
* Lines can be temporarily excluded with a documented marker while a
|
||||
* migration is in progress. The final strict gate will reject markers
|
||||
* without a non-empty reason.
|
||||
*/
|
||||
public static function scanContents(string $path, string $contents): array
|
||||
{
|
||||
$violations = [];
|
||||
$lines = preg_split('/\R/', $contents);
|
||||
if (!is_array($lines)) {
|
||||
return $violations;
|
||||
}
|
||||
|
||||
foreach ($lines as $offset => $line) {
|
||||
if (preg_match('/user-copy:\s*allow\s+\S+/i', $line) === 1) {
|
||||
continue;
|
||||
}
|
||||
foreach (self::RULES as $rule => $pattern) {
|
||||
if (preg_match($pattern, $line) !== 1) {
|
||||
continue;
|
||||
}
|
||||
$violations[] = [
|
||||
'path' => $path,
|
||||
'line' => $offset + 1,
|
||||
'rule' => $rule,
|
||||
'snippet' => trim($line),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $violations;
|
||||
}
|
||||
|
||||
public static function scanFiles(array $paths): array
|
||||
{
|
||||
$violations = [];
|
||||
foreach ($paths as $path) {
|
||||
if (!is_string($path) || !is_file($path)) {
|
||||
continue;
|
||||
}
|
||||
$contents = file_get_contents($path);
|
||||
if (!is_string($contents)) {
|
||||
continue;
|
||||
}
|
||||
array_push($violations, ...self::scanContents($path, $contents));
|
||||
}
|
||||
return $violations;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Source-language manifest for user-visible interface copy.
|
||||
*
|
||||
* Only stable keys, Chinese safety baselines and rendering metadata belong
|
||||
* here. English and every other target-locale translation are maintained by
|
||||
* administrators and must never be committed to this source manifest.
|
||||
*/
|
||||
return [
|
||||
'version' => 1,
|
||||
'pages' => [
|
||||
'auth.login' => ['label' => '登录页', 'scopes' => ['common', 'auth']],
|
||||
'auth.password' => ['label' => '重置密码页', 'scopes' => ['common', 'auth']],
|
||||
'auth.register' => ['label' => '注册页', 'scopes' => ['common', 'auth']],
|
||||
'cloud.backups' => ['label' => '云备份页', 'scopes' => ['common', 'backups']],
|
||||
'cloud.deploy' => ['label' => '实例部署页', 'scopes' => ['common', 'deploy']],
|
||||
'cloud.detail' => ['label' => '实例详情页', 'scopes' => ['common', 'instance_detail']],
|
||||
'cloud.invoice_detail' => ['label' => '账单详情页', 'scopes' => ['common', 'billing']],
|
||||
'cloud.invoices' => ['label' => '账单列表页', 'scopes' => ['common', 'billing']],
|
||||
'cloud.instances' => ['label' => '实例列表页', 'scopes' => ['common', 'instances']],
|
||||
'cloud.isos' => ['label' => 'ISO镜像页', 'scopes' => ['common', 'isos']],
|
||||
'cloud.keys' => ['label' => 'SSH公钥页', 'scopes' => ['common', 'ssh_keys']],
|
||||
'cloud.reinstall' => ['label' => '重装系统页', 'scopes' => ['common', 'reinstall']],
|
||||
'cloud.snapshots' => ['label' => '云快照页', 'scopes' => ['common', 'snapshots']],
|
||||
'cloud.traffic' => ['label' => '流量池页', 'scopes' => ['common', 'traffic']],
|
||||
'cloud.vpcs' => ['label' => 'VPC私网页', 'scopes' => ['common', 'vpc']],
|
||||
'legal.privacy' => ['label' => '隐私政策页', 'scopes' => ['common', 'legal']],
|
||||
'legal.terms' => ['label' => '服务条款页', 'scopes' => ['common', 'legal']],
|
||||
'public.home' => ['label' => '落地页', 'scopes' => ['common', 'home']],
|
||||
'user.account' => ['label' => '账户设置页', 'scopes' => ['common', 'account']],
|
||||
'user.announcements' => ['label' => '公告抽屉', 'scopes' => ['common', 'announcements']],
|
||||
'user.invite' => ['label' => '推广页', 'scopes' => ['common', 'invite']],
|
||||
'user.recharge' => ['label' => '充值页', 'scopes' => ['common', 'recharge']],
|
||||
'user.tasks' => ['label' => '任务中心', 'scopes' => ['common', 'tasks']],
|
||||
'user.tickets' => ['label' => '工单页', 'scopes' => ['common', 'tickets']],
|
||||
],
|
||||
'entries' => [
|
||||
[
|
||||
'key' => 'common.toast.close.aria',
|
||||
'scope' => 'common',
|
||||
'page' => 'cloud.instances',
|
||||
'kind' => 'aria_label',
|
||||
'source' => '关闭提示',
|
||||
'format' => 'plain_text',
|
||||
'parameters' => [],
|
||||
'criticality' => 'required',
|
||||
'allow_hide' => false,
|
||||
'toast_tone' => null,
|
||||
],
|
||||
[
|
||||
'key' => 'common.toast.error.title',
|
||||
'scope' => 'common',
|
||||
'page' => 'cloud.instances',
|
||||
'kind' => 'toast_title',
|
||||
'source' => '操作失败',
|
||||
'format' => 'plain_text',
|
||||
'parameters' => [],
|
||||
'criticality' => 'required',
|
||||
'allow_hide' => false,
|
||||
'toast_tone' => 'error',
|
||||
],
|
||||
[
|
||||
'key' => 'common.toast.success.body',
|
||||
'scope' => 'common',
|
||||
'page' => 'cloud.instances',
|
||||
'kind' => 'toast_body',
|
||||
'source' => '操作已完成。',
|
||||
'format' => 'plain_text',
|
||||
'parameters' => [],
|
||||
'criticality' => 'required',
|
||||
'allow_hide' => false,
|
||||
'toast_tone' => 'success',
|
||||
],
|
||||
[
|
||||
'key' => 'common.toast.success.title',
|
||||
'scope' => 'common',
|
||||
'page' => 'cloud.instances',
|
||||
'kind' => 'toast_title',
|
||||
'source' => '操作完成',
|
||||
'format' => 'plain_text',
|
||||
'parameters' => [],
|
||||
'criticality' => 'required',
|
||||
'allow_hide' => false,
|
||||
'toast_tone' => 'success',
|
||||
],
|
||||
[
|
||||
'key' => 'common.toast.warning.title',
|
||||
'scope' => 'common',
|
||||
'page' => 'cloud.instances',
|
||||
'kind' => 'toast_title',
|
||||
'source' => '请注意',
|
||||
'format' => 'plain_text',
|
||||
'parameters' => [],
|
||||
'criticality' => 'required',
|
||||
'allow_hide' => false,
|
||||
'toast_tone' => 'warning',
|
||||
],
|
||||
],
|
||||
];
|
||||
@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Services\UserCopy\UserCopyCoverageScanner;
|
||||
use App\Services\UserCopy\UserCopyManifest;
|
||||
|
||||
$assert = static function (bool $condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
$rejects = static function (array $manifest, string $needle) use ($assert): void {
|
||||
try {
|
||||
UserCopyManifest::validate($manifest);
|
||||
} catch (InvalidArgumentException $exception) {
|
||||
$assert(str_contains($exception->getMessage(), $needle),
|
||||
'Manifest rejection did not identify the expected contract: '
|
||||
. $exception->getMessage());
|
||||
return;
|
||||
}
|
||||
throw new RuntimeException('Invalid Manifest was accepted: ' . $needle);
|
||||
};
|
||||
|
||||
$manifest = UserCopyManifest::load();
|
||||
$loadedEntries = array_column($manifest['entries'], null, 'key');
|
||||
$assert(($manifest['version'] ?? null) === 1
|
||||
&& isset($manifest['pages']['cloud.instances'])
|
||||
&& isset($loadedEntries['common.toast.success.title']),
|
||||
'Source-language user-copy Manifest did not load its page and Toast contract.');
|
||||
$assert(strlen(UserCopyManifest::digest($manifest)) === 64,
|
||||
'User-copy Manifest digest is not a SHA-256 identity.');
|
||||
$base = [
|
||||
'version' => 1,
|
||||
'pages' => [
|
||||
'test.page' => ['label' => '测试页面', 'scopes' => ['test']],
|
||||
],
|
||||
'entries' => [
|
||||
[
|
||||
'key' => 'test.greeting',
|
||||
'scope' => 'test', 'page' => 'test.page', 'kind' => 'label',
|
||||
'source' => '您好,{name}', 'format' => 'icu',
|
||||
'parameters' => ['name' => 'string'],
|
||||
'criticality' => 'normal', 'allow_hide' => true,
|
||||
'toast_tone' => null,
|
||||
],
|
||||
],
|
||||
];
|
||||
$validatedBase = UserCopyManifest::validate($base);
|
||||
$validatedBaseEntries = array_column(
|
||||
$validatedBase['entries'], null, 'key'
|
||||
);
|
||||
$assert($validatedBaseEntries['test.greeting']['source'] === '您好,{name}',
|
||||
'Valid ICU source entry was not retained.');
|
||||
|
||||
$duplicateKey = $base;
|
||||
$duplicateKey['entries'][] = $duplicateKey['entries'][0];
|
||||
$rejects($duplicateKey, '词条键重复');
|
||||
|
||||
$invalidKey = $base;
|
||||
$invalidKey['entries'][0]['key'] = 'test.{dynamic}';
|
||||
$rejects($invalidKey, '静态语义键');
|
||||
|
||||
$invalidParameter = $base;
|
||||
$invalidParameter['entries'][0]['parameters'] = [
|
||||
'other' => 'string',
|
||||
];
|
||||
$rejects($invalidParameter, '参数与中文基线不一致');
|
||||
|
||||
$invalidParameterName = $base;
|
||||
$invalidParameterName['entries'][0]['parameters'] = [
|
||||
'user-name' => 'string',
|
||||
];
|
||||
$rejects($invalidParameterName, '参数合同无效');
|
||||
|
||||
$translationInSource = $base;
|
||||
$translationInSource['entries'][0]['en'] = 'Hello';
|
||||
$rejects($translationInSource, '未声明字段');
|
||||
|
||||
$unsafeDirection = $base;
|
||||
$unsafeDirection['entries'][0]['source'] = "您好\u{202E}";
|
||||
$rejects($unsafeDirection, '非法内容');
|
||||
|
||||
$hiddenCritical = $base;
|
||||
$hiddenCritical['entries'][0]['criticality'] = 'critical';
|
||||
$rejects($hiddenCritical, '高风险词条不能隐藏');
|
||||
|
||||
$toastWithoutTone = $base;
|
||||
$toastWithoutTone['entries'][0]['kind'] = 'toast_body';
|
||||
$rejects($toastWithoutTone, 'Toast 词条必须声明有效 tone');
|
||||
|
||||
$source = <<<'SOURCE'
|
||||
toast('任务已接收。', 'success');
|
||||
window.confirm('确定删除?');
|
||||
date.toLocaleString('zh-CN');
|
||||
// user-copy: allow fixture 中文测试数据
|
||||
SOURCE;
|
||||
$violations = UserCopyCoverageScanner::scanContents('fixture.js', $source);
|
||||
$rules = array_count_values(array_column($violations, 'rule'));
|
||||
$assert(($rules['direct_toast_text'] ?? 0) === 1
|
||||
&& ($rules['direct_confirm_text'] ?? 0) === 1
|
||||
&& ($rules['hardcoded_zh_locale'] ?? 0) === 1
|
||||
&& ($rules['visible_chinese_literal'] ?? 0) === 2,
|
||||
'Coverage scanner did not report direct user-visible copy consistently.');
|
||||
|
||||
echo "User-copy Manifest and coverage-scanner contract checks passed.\n";
|
||||
Loading…
Reference in New Issue