File tree Expand file tree Collapse file tree 2 files changed +39
-3
lines changed
src/main/java/com/yanglb/codegen Expand file tree Collapse file tree 2 files changed +39
-3
lines changed Original file line number Diff line number Diff line change @@ -154,8 +154,8 @@ private void translateDesigner(WritableModel writableModel) throws CodeGenExcept
154154
155155 // 对字符串进行转换
156156 id = escape (id );
157- String value = this . escape ( itm . get ( this . msgLang ) );
158- code .append (String .format (" %s static string %s {\n " , designerAccessibility , id ));
157+ String propertyName = StringUtil . toValidPropertyName ( id );
158+ code .append (String .format (" %s static string %s {\n " , designerAccessibility , propertyName ));
159159 code .append (String .format (" get {\n " ));
160160 code .append (String .format (" return ResourceManager.GetString(\" %s\" , resourceCulture);\n " , id ));
161161 code .append (String .format (" }\n " ));
@@ -166,7 +166,7 @@ private void translateDesigner(WritableModel writableModel) throws CodeGenExcept
166166 String template = readResource ("msg/resx/designer.cs.txt" );
167167 Map <String , String > values = new HashMap <>();
168168 values .put ("namespace" , designerNamespace );
169- values .put ("className" , writableModel .getFileName ());
169+ values .put ("className" , StringUtil . toValidPropertyName ( writableModel .getFileName () ));
170170 values .put ("accessibility" , designerAccessibility );
171171 values .put ("code" , code .toString ());
172172
Original file line number Diff line number Diff line change @@ -50,4 +50,40 @@ public static String escapeIOSString(String value) {
5050 value = value .replaceAll ("\n " , "\\ \\ n" );
5151 return value ;
5252 }
53+
54+ public static String toValidPropertyName (String input ) {
55+ if (input == null || input .isEmpty ()) {
56+ return "_" ;
57+ }
58+
59+ StringBuilder sb = new StringBuilder ();
60+
61+ // .NET 属性名必须以字母或下划线开头
62+ char first = input .charAt (0 );
63+ if (Character .isLetter (first ) || first == '_' ) {
64+ sb .append (first );
65+ } else {
66+ sb .append ('_' );
67+ }
68+
69+ // 处理剩余字符:字母 / 数字 / 下划线 保留,其它替换成 "_"
70+ for (int i = 1 ; i < input .length (); i ++) {
71+ char c = input .charAt (i );
72+ if (Character .isLetterOrDigit (c ) || c == '_' ) {
73+ sb .append (c );
74+ } else {
75+ sb .append ('_' );
76+ }
77+ }
78+
79+ return sb .toString ();
80+ }
81+
82+ // 简单测试
83+ public static void main (String [] args ) {
84+ System .out .println (toValidPropertyName ("123Name" )); // _23Name
85+ System .out .println (toValidPropertyName ("user-name" )); // user_name
86+ System .out .println (toValidPropertyName ("name@domain" )); // name_domain
87+ System .out .println (toValidPropertyName ("正常属性" )); // 正常属性(中文是合法的 Unicode 字母)
88+ }
5389}
You can’t perform that action at this time.
0 commit comments