@@ -3,17 +3,34 @@ import type {
33 Guard ,
44 GuardDetail ,
55 GuardMode ,
6+ Matcher ,
67 RuleAction ,
78 RuleScope ,
89 SecurityKindView ,
910 SecurityRuleView ,
1011} from '@/lib/security-types' ;
12+ import CATALOG from './guard.zh.json' ;
1113
12- // Names on the security page. A built-in rule's name, reason and
13- // description, and each group's name, come with the rule view in both
14- // languages (thinkwatch-core writes them once for both products); the
15- // console keeps no table of its own. A custom rule's id is the name its
16- // author gave it.
14+ // Built-in display text is translated by guard and ID. The view supplies
15+ // English fallbacks and custom rules keep their original names.
16+ type RuleCopy = { name : string ; why ?: string ; about ?: string } ;
17+ const catalog : Record < Guard , { rules : Record < string , RuleCopy > ; kinds : Record < string , string > } > = CATALOG ;
18+
19+ function guardAboutZh ( m : Matcher ) : string | undefined {
20+ if ( m . kind !== "prefix" && m . kind !== "prefixes" ) return undefined ;
21+ const prefixes = m . kind === "prefix" ? [ { ...m , tail : "any" } ] : m . prefixes ;
22+ const groups = new Map < string , { prefixes : string [ ] ; length : number ; unit : string } > ( ) ;
23+ for ( const p of prefixes ) {
24+ const key = `${ p . min_tail } :${ p . tail } ` ;
25+ const unit = p . tail === "hex" ? "十六进制字符" : p . tail === "alnum" ? "字母或数字" : "字符" ;
26+ const group = groups . get ( key ) ?? { prefixes : [ ] , length : p . min_tail , unit } ;
27+ group . prefixes . push ( `\`${ p . prefix } \`` ) ;
28+ groups . set ( key , group ) ;
29+ }
30+ return [ ...groups . values ( ) ] . map ( ( { prefixes, length, unit } ) =>
31+ `${ prefixes . join ( "、" ) } 开头,其后至少 ${ length } 个${ unit } ` ,
32+ ) . join ( ";" ) || undefined ;
33+ }
1734
1835export function guardName ( t : TFunction , guard : Guard ) : string {
1936 return t ( `contentSecurity.guard.${ guard } .name` ) ;
@@ -29,20 +46,19 @@ export function scopeName(t: TFunction, scope: RuleScope): string {
2946 return t ( `contentSecurity.scope.${ scope } ` ) ;
3047}
3148
32- /** Whether the console is in Chinese: rule text comes from the server in both languages. */
3349function zh ( lang : string ) : boolean {
3450 return lang . startsWith ( 'zh' ) ;
3551}
3652
37- /** The Chinese text in Chinese when there is one, else the English. */
38- function pick ( lang : string , en : string | undefined , cn : string | undefined ) : string {
39- return ( zh ( lang ) && cn ? cn : en ) ?? '' ;
53+ function ruleCopy ( guard : Guard , id : string ) : RuleCopy | undefined {
54+ const rules = catalog [ guard ] . rules ;
55+ return Object . hasOwn ( rules , id ) ? rules [ id ] : undefined ;
4056}
4157
4258/** A rule's name as the view gives it. A custom rule's id is the name its author gave it. */
43- export function viewName ( lang : string , r : SecurityRuleView ) : string {
59+ export function viewName ( lang : string , guard : Guard , r : SecurityRuleView ) : string {
4460 if ( r . custom ) return r . id ;
45- return pick ( lang , r . name , r . name_zh ) || r . id ;
61+ return ( zh ( lang ) ? ruleCopy ( guard , r . id ) ?. name : undefined ) ?? ( r . name || r . id ) ;
4662}
4763
4864/**
@@ -52,32 +68,35 @@ export function viewName(lang: string, r: SecurityRuleView): string {
5268 */
5369export function ruleName (
5470 lang : string ,
71+ guard : Guard ,
5572 detail : GuardDetail | undefined ,
5673 id : string ,
5774 custom : boolean ,
5875 fallback ?: string ,
5976) : string {
6077 if ( custom ) return id ;
6178 const r = detail ?. rules . find ( ( r ) => ! r . custom && r . id === id ) ;
62- return r ? viewName ( lang , r ) : ( fallback ?? id ) ;
79+ return r ? viewName ( lang , guard , r ) : ( ( zh ( lang ) ? ruleCopy ( guard , id ) ?. name : undefined ) ?? fallback ?? id ) ;
6380}
6481
6582/** Why a built-in rule is worth a look. Empty for rules that carry no reason. */
66- export function ruleWhy ( lang : string , r : SecurityRuleView ) : string {
83+ export function ruleWhy ( lang : string , guard : Guard , r : SecurityRuleView ) : string {
6784 if ( r . custom ) return '' ;
68- return pick ( lang , r . why , r . why_zh ) ;
85+ return ( zh ( lang ) ? ruleCopy ( guard , r . id ) ?. why : undefined ) ?? r . why ?? '' ;
6986}
7087
7188/** What a built-in redaction rule finds, as a sentence; backticks mark code. Empty elsewhere. */
72- export function ruleAbout ( lang : string , r : SecurityRuleView ) : string {
89+ export function ruleAbout ( lang : string , guard : Guard , r : SecurityRuleView ) : string {
7390 if ( r . custom ) return '' ;
74- return pick ( lang , r . about , r . about_zh ) ;
91+ return ( zh ( lang ) ? guardAboutZh ( r . matcher ) ?? ruleCopy ( guard , r . id ) ?. about : undefined ) ?? r . about ?? '' ;
7592}
7693
7794/** A rule group's heading, from the guard's view. */
78- export function kindName ( lang : string , kinds : SecurityKindView [ ] | undefined , slug : string ) : string {
95+ export function kindName ( lang : string , guard : Guard , kinds : SecurityKindView [ ] | undefined , slug : string ) : string {
96+ const copy = catalog [ guard ] . kinds ;
97+ if ( zh ( lang ) && Object . hasOwn ( copy , slug ) ) return copy [ slug ] ?? slug ;
7998 const k = kinds ?. find ( ( k ) => k . slug === slug ) ;
80- return k ? pick ( lang , k . name , k . name_zh ) : slug ;
99+ return k ? .name ?? slug ;
81100}
82101
83102export function actionName ( t : TFunction , action : RuleAction ) : string {
0 commit comments