|
| 1 | +/** |
| 2 | + * Copyright 2020 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.impl; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import com.yanglb.utilitys.codegen.core.model.TableModel; |
| 24 | +import com.yanglb.utilitys.codegen.core.translator.BaseMsgTranslator; |
| 25 | +import com.yanglb.utilitys.codegen.exceptions.CodeGenException; |
| 26 | +import com.yanglb.utilitys.codegen.utility.MsgUtility; |
| 27 | +import com.yanglb.utilitys.codegen.utility.StringUtility; |
| 28 | + |
| 29 | +public class MsgAndroidTranslatorImpl extends BaseMsgTranslator { |
| 30 | + @Override |
| 31 | + protected void onBeforeTranslate() throws CodeGenException { |
| 32 | + super.onBeforeTranslate(); |
| 33 | + |
| 34 | + this.writableModel.setExtension("xml"); |
| 35 | + String path = (this.isDefaultLanguage() ? "" : this.getSplitString() + this.msgLang); |
| 36 | + this.writableModel.setFilePath("msg/android/values" + path); |
| 37 | + |
| 38 | + // 文件名 |
| 39 | + this.writableModel.setFileName("strings"); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + protected void onTranslate() throws CodeGenException { |
| 44 | + super.onTranslate(); |
| 45 | + StringBuilder sb = this.writableModel.getData(); |
| 46 | + sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + |
| 47 | + this.settingMap.get("head") + |
| 48 | + "<resources>\r\n"); |
| 49 | + |
| 50 | + // 替换标记 |
| 51 | + String s = this.replaceFlags(sb.toString(), null); |
| 52 | + sb = new StringBuilder(s); |
| 53 | + |
| 54 | + // 用于检查相同的key |
| 55 | + Map<String, Boolean> keys = new HashMap<String, Boolean>(); |
| 56 | + Map<String, List<String>> arrays = new HashMap<String, List<String>>(); |
| 57 | + for(TableModel tblModel : this.model) { |
| 58 | + for(Map<String, String> itm : tblModel.toList()) { |
| 59 | + String id = itm.get("id"); |
| 60 | + if(StringUtility.isNullOrEmpty(id)) continue; |
| 61 | + if (!arrays.containsKey(id)) { |
| 62 | + arrays.put(id, new ArrayList<String>()); |
| 63 | + } |
| 64 | + |
| 65 | + // 对字符串进行转换 |
| 66 | + String value = this.convert2CSCode(itm.get(this.msgLang)); |
| 67 | + List<String> items = arrays.get(id); |
| 68 | + items.add(value); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + for(String key:arrays.keySet()) { |
| 73 | + List<String> items = arrays.get(key); |
| 74 | + if (items.size() > 1) { |
| 75 | + // list |
| 76 | + sb.append(String.format(" <string-array name=\"%s\">\r\n", key)); |
| 77 | + for(String value:items) { |
| 78 | + sb.append(String.format(" <item>%s</item>\r\n", value)); |
| 79 | + } |
| 80 | + sb.append(" </string-array>\r\n"); |
| 81 | + } else { |
| 82 | + sb.append(String.format(" <string name=\"%s\">%s</string>\r\n", key, items.get(0))); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + sb.append("</resources>\r\n"); |
| 87 | + |
| 88 | + this.writableModel.setData(sb); |
| 89 | + } |
| 90 | + |
| 91 | + private String convert2CSCode(String value) { |
| 92 | + if(value == null) return null; |
| 93 | + |
| 94 | + // 先替换\r\n,防止有文档只有\r或\n 后面再替换一次 |
| 95 | + value = value.replaceAll("\r\n", "\\\\r\\\\n"); |
| 96 | + value = value.replaceAll("\r", "\\\\r\\\\n"); |
| 97 | + value = value.replaceAll("\n", "\\\\r\\\\n"); |
| 98 | + return value; |
| 99 | + } |
| 100 | +} |
0 commit comments