Skip to content

Commit bb0c83a

Browse files
author
杨利兵
committed
改进msg生成功能
1 parent 973732a commit bb0c83a

File tree

4 files changed

+90
-33
lines changed

4 files changed

+90
-33
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright 2015 yanglb.com
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.yanglb.utilitys.codegen.core.translator;
17+
18+
import java.io.File;
19+
import java.util.List;
20+
21+
import com.yanglb.utilitys.codegen.core.model.TableModel;
22+
23+
public class BaseMsgTranslator extends BaseTranslator<List<TableModel>> {
24+
25+
/**
26+
* 文件名,优先使用--fn参数指定的文件名,如不指定使用excel名称
27+
* @return 文件名
28+
*/
29+
protected String getFileName() {
30+
String fileName = this.paramaModel.getOptions().get("fn");
31+
if (fileName != null) {
32+
fileName.replaceAll("\"", "");
33+
} else {
34+
fileName = this.model.get(0).getExcelFileName();
35+
File file = new File(fileName);
36+
fileName = file.getName();
37+
38+
int index = fileName.lastIndexOf(".");
39+
if(index != -1) {
40+
fileName = fileName.substring(0, index);
41+
}
42+
}
43+
44+
return fileName;
45+
}
46+
}

src/com/yanglb/utilitys/codegen/core/translator/impl/MsgJavaTranslatorImpl.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
*/
1616
package com.yanglb.utilitys.codegen.core.translator.impl;
1717

18-
import java.util.List;
1918
import java.util.Map;
2019

2120
import com.yanglb.utilitys.codegen.core.model.TableModel;
22-
import com.yanglb.utilitys.codegen.core.translator.BaseTranslator;
21+
import com.yanglb.utilitys.codegen.core.translator.BaseMsgTranslator;
2322
import com.yanglb.utilitys.codegen.exceptions.CodeGenException;
2423
import com.yanglb.utilitys.codegen.utility.StringUtility;
2524

26-
public class MsgJavaTranslatorImpl extends BaseTranslator<List<TableModel>> {
25+
public class MsgJavaTranslatorImpl extends BaseMsgTranslator {
2726
protected String msgLang = "";
2827

2928
@Override
@@ -33,11 +32,19 @@ protected void onBeforeTranslate() throws CodeGenException {
3332
// 当前生成的国际化语言
3433
this.msgLang = this.settingMap.get("MsgLang");
3534

36-
this.writableModel.setExtension("properties");
37-
this.writableModel.setFileName("message");
38-
if(!this.msgLang.equals("default")) {
39-
this.writableModel.setFileName("message_"+this.msgLang);
35+
// 文件名
36+
String fileName = getFileName();
37+
if (fileName.equals("")) {
38+
// 空文件名
39+
fileName = this.msgLang;
40+
} else {
41+
if(!this.msgLang.equals("default")) {
42+
fileName = fileName + "_" + this.msgLang;
43+
}
4044
}
45+
46+
this.writableModel.setFileName(fileName);
47+
this.writableModel.setExtension("properties");
4148
this.writableModel.setFilePath("msg/properties");
4249
}
4350

src/com/yanglb/utilitys/codegen/core/translator/impl/MsgJsTranslatorImpl.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
*/
1616
package com.yanglb.utilitys.codegen.core.translator.impl;
1717

18-
import java.util.List;
1918
import java.util.Map;
2019

2120
import com.yanglb.utilitys.codegen.core.model.TableModel;
22-
import com.yanglb.utilitys.codegen.core.translator.BaseTranslator;
21+
import com.yanglb.utilitys.codegen.core.translator.BaseMsgTranslator;
2322
import com.yanglb.utilitys.codegen.exceptions.CodeGenException;
2423
import com.yanglb.utilitys.codegen.utility.StringUtility;
2524

26-
public class MsgJsTranslatorImpl extends BaseTranslator<List<TableModel>> {
25+
public class MsgJsTranslatorImpl extends BaseMsgTranslator {
2726
protected String msgLang = "";
2827

2928
@Override
@@ -33,16 +32,19 @@ protected void onBeforeTranslate() throws CodeGenException {
3332
// 当前生成的国际化语言
3433
this.msgLang = this.settingMap.get("MsgLang");
3534

36-
// 可指定文件名
37-
String fileName = this.paramaModel.getOptions().get("fn");
38-
if(fileName == null || fileName.equals("")){
39-
fileName = "message";
35+
// 文件名
36+
String fileName = getFileName();
37+
if (fileName.equals("")) {
38+
// 空文件名
39+
fileName = this.msgLang;
40+
} else {
41+
if(!this.msgLang.equals("default")) {
42+
fileName = fileName + "." + this.msgLang;
43+
}
4044
}
41-
this.writableModel.setExtension("js");
45+
4246
this.writableModel.setFileName(fileName);
43-
if(!this.msgLang.equals("default")) {
44-
this.writableModel.setFileName(fileName + "." + this.msgLang);
45-
}
47+
this.writableModel.setExtension("js");
4648
this.writableModel.setFilePath("msg/js");
4749
}
4850

@@ -70,16 +72,16 @@ protected void onTranslate() throws CodeGenException {
7072
if(StringUtility.isNullOrEmpty(id)) continue;
7173
// 对字符串进行转换
7274
String value = this.convert2JsCode(itm.get(this.msgLang));
73-
sb.append(String.format(" %s: '%s', \r\n", id, value));
75+
sb.append(String.format(" %s: \"%s\", \r\n", id, value));
7476
}
7577
} else {
76-
sb.append(String.format(" '%s': { \r\n", tblModel.getSheetName()));
78+
sb.append(String.format(" %s: { \r\n", tblModel.getSheetName()));
7779
for(Map<String, String> itm : tblModel.toList()) {
7880
String id = itm.get("id");
7981
if(StringUtility.isNullOrEmpty(id)) continue;
8082
// 对字符串进行转换
8183
String value = this.convert2JsCode(itm.get(this.msgLang));
82-
sb.append(String.format(" %s: '%s', \r\n", id, value));
84+
sb.append(String.format(" %s: \"%s\", \r\n", id, value));
8385
}
8486
int idx = sb.lastIndexOf(",");
8587
if(idx != -1) {
@@ -103,7 +105,7 @@ protected void onTranslate() throws CodeGenException {
103105
if(StringUtility.isNullOrEmpty(id)) continue;
104106
// 对字符串进行转换
105107
String value = this.convert2JsCode(itm.get(this.msgLang));
106-
sb.append(String.format(" %s: '%s', \r\n", id, value));
108+
sb.append(String.format(" %s: \"%s\", \r\n", id, value));
107109
}
108110
}
109111

src/com/yanglb/utilitys/codegen/core/translator/impl/MsgJsonTranslatorImpl.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
*/
1616
package com.yanglb.utilitys.codegen.core.translator.impl;
1717

18-
import java.util.List;
1918
import java.util.Map;
2019

2120
import com.yanglb.utilitys.codegen.core.model.TableModel;
22-
import com.yanglb.utilitys.codegen.core.translator.BaseTranslator;
21+
import com.yanglb.utilitys.codegen.core.translator.BaseMsgTranslator;
2322
import com.yanglb.utilitys.codegen.exceptions.CodeGenException;
2423
import com.yanglb.utilitys.codegen.utility.StringUtility;
2524

26-
public class MsgJsonTranslatorImpl extends BaseTranslator<List<TableModel>> {
25+
public class MsgJsonTranslatorImpl extends BaseMsgTranslator {
2726
protected String msgLang = "";
2827

2928
@Override
@@ -33,16 +32,19 @@ protected void onBeforeTranslate() throws CodeGenException {
3332
// 当前生成的国际化语言
3433
this.msgLang = this.settingMap.get("MsgLang");
3534

36-
// 可指定文件名
37-
String fileName = this.paramaModel.getOptions().get("fn");
38-
if(fileName == null || fileName.equals("")){
39-
fileName = "message";
35+
// 文件名
36+
String fileName = getFileName();
37+
if (fileName.equals("")) {
38+
// 空文件名
39+
fileName = this.msgLang;
40+
} else {
41+
if(!this.msgLang.equals("default")) {
42+
fileName = fileName + "." + this.msgLang;
43+
}
4044
}
41-
this.writableModel.setExtension("json");
45+
4246
this.writableModel.setFileName(fileName);
43-
if(!this.msgLang.equals("default")) {
44-
this.writableModel.setFileName(fileName + "." + this.msgLang);
45-
}
47+
this.writableModel.setExtension("json");
4648
this.writableModel.setFilePath("msg/json");
4749
}
4850

0 commit comments

Comments
 (0)