代码拉取完成,页面将自动刷新
apply plugin: 'MultilingualTransform'
def excelDir = "${multilingual.TRANSLATION_EXCEL_DIR}"
buildscript {
repositories {
maven {
url 'https://devops-maven.zeekrlife.com/repository/maven-public/'
credentials {
username "sncdeploy"
password "5JA1sx11cy7v"
}
}
}
dependencies {
classpath 'com.alibaba:fastjson:1.2.83_noneautotype'
classpath 'com.alibaba:easyexcel:3.3.2'
}
}
import java.nio.charset.StandardCharsets
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.fastjson.JSON;
import org.gradle.internal.os.OperatingSystem
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets
import java.text.SimpleDateFormat;
import java.util.List;
class TransformConfig {
public String excelFile;
public int defaultRowStart;
public int stringResNameColumn;
public Map<String, Integer> mappingValues;
static class LanguageConfig {
public int index;
public boolean defaultLanguage;
}
@Override
String toString() {
return JSON.toJSONString(this);
}
}
project.tasks.findByName("MultilingualTransform").doLast {
if (MultilingualTransformConfig.excelFile != null) {
MultilingualTransformConfig.excelFile.delete()
}
}
static String file2String(File file) {
try (def fis = new FileInputStream(file)) {
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
return new String(bytes, StandardCharsets.UTF_8);
}
}
def defaultLanguage = multilingual.DEFAULT_LANGUAGE == null ? "en" : "${multilingual.DEFAULT_LANGUAGE}"
def defaultStringFileName = multilingual.DEFAULT_STRING_FILE_NAME == null ? "strings.xml" : "${multilingual.DEFAULT_STRING_FILE_NAME}"
def outputFile = new File(multilingual.OUTPUT_FILE == null ? "${project.projectDir}/src/main/res" : "${multilingual.OUTPUT_FILE}")
project.tasks.findByName("MultilingualTransform").doFirst { Task t ->
String cfgPath = "${multilingual.LANG_CONFIG_PATH}"
String cfgJson = null
if (cfgPath == null || cfgPath.trim().isEmpty() || cfgPath.trim().toLowerCase() == "null") {
cfgPath = "https://gitee.com/wing-hawk/multilingual-transform/raw/master/lang_cfg.json"
}
println("language cfg path $cfgPath")
File cfgFile = new File(cfgPath)
if (cfgFile.isFile()) {
cfgJson = file2String(cfgFile)
} else {
cfgJson = "curl ${cfgPath}".execute().text
}
if (cfgJson == null) {
throw new IllegalArgumentException("can not get config file by path ${cfgPath}")
}
File cfgOutputFile = new File("${rootProject.rootDir}/build/multilingual/lang_cfg.json")
if (cfgFile != cfgOutputFile) {
FileOutputStream cfgFos = new FileOutputStream(cfgOutputFile)
cfgFos.write(cfgJson.getBytes("utf-8"))
cfgFos.close()
}
// 1. 合并 csv 数据
List<TransformConfig> transformConfigs = JSON.parseArray(cfgJson, TransformConfig.class);
Map<Object, LinkedHashMap<Object, Object>> resultExcelData = new LinkedHashMap<>();
def combinedExcel = new File(excelDir, "combined_excel_${new SimpleDateFormat('yyyy_MM_dd_HH_mm_ss_SSS').format(new Date())}.xlsx")
def excelHeader = new ArrayList<List<String>>()
def resIdHeaderColumn = new ArrayList<String>()
resIdHeaderColumn.add("stringResId")
excelHeader.add(resIdHeaderColumn)
def defaultValueColumn = -1
for (TransformConfig config : transformConfigs) {
File excel = new File(excelDir, config.excelFile);
if (!excel.exists()) {
println "$excel is not exists!"
continue
}
if (config.excelFile.endsWith(".csv")) {
List<LinkedHashMap<Object, Object>> csvItems = EasyExcel.read(excel)
.charset(StandardCharsets.UTF_8)
.sheet()
.headRowNumber(config.defaultRowStart)
.doReadSync();
for (final def item in csvItems) {
Object stringResId = item.get(config.stringResNameColumn)
if (stringResId == null || stringResId.toString().trim() == "") {
println("stringResId column is null or empty, item = " + item + ", csv = " + config.excelFile)
continue
}
stringResId = stringResId.toString()
String lowercaseStringId = "${multilingual.FORCE_STRING_ID_LOWERCASE}"
if (lowercaseStringId.trim().toLowerCase() == "true") {
stringResId = stringResId.toLowerCase(Locale.US)
}
def map = resultExcelData.get(stringResId)
if (map == null) {
map = new LinkedHashMap<Object, Object>();
resultExcelData.put(stringResId, map)
map.put(0, stringResId)
}
def escapeBackslash = "${multilingual.ESCAPE_BACKSLASH}" == true
config.mappingValues.forEach { language, translateIndex ->
def translate = item.get(translateIndex) as String
if (escapeBackslash && translate != null) {
translate = translate.replace("\\", "\\\\")
}
List<String> headerColumn = null;
int headerColumnIndex = -1;
for (i in 0..<excelHeader.size()) {
def h = excelHeader.get(i)
if (h.contains(language)) {
headerColumn = h
headerColumnIndex = i
break
}
}
if (headerColumn == null) {
headerColumn = new ArrayList<>()
headerColumn.add(language)
excelHeader.add(headerColumn)
headerColumnIndex = excelHeader.size() - 1;
if (language == defaultLanguage) {
defaultValueColumn = headerColumnIndex
}
}
map.put(headerColumnIndex, translate)
}
}
}
}
// easy excel 导出有问题,如果某条目数据存在空缺会导致该条目其他 column 的数据导不进去
// 例如在 ar.csv 中有 stringId 为 linking 的条目而在 ru.csv 中无,这种情况可能导致某个其他语言的 linking 条目
// 没导进去。这里填充默认值来避免此问题。
for (int i = 1; i < excelHeader.size(); i++) {
for (final def mapItem in resultExcelData.values()) {
if (!mapItem.containsKey(i)) {
mapItem.put(i, null)
}
}
}
// 2. 输出到 xlsx 文件
ArrayList<LinkedHashMap<Object, Object>> list = new ArrayList<>(resultExcelData.values());
EasyExcel.write(combinedExcel)
.excelType(ExcelTypeEnum.XLSX)
.sheet(project.rootProject.name)
.head(excelHeader)
.doWrite(list);
copy {
from combinedExcel.path
into "${rootProject.rootDir}/build/multilingual"
rename {
"multilingual_${project.name}.xlsx"
}
}
// 2. 将合并后的 xlsx 文件丢给之前的 transform 执行
t.configure {
MultilingualTransformConfig.defaultStringFileName = defaultStringFileName
MultilingualTransformConfig.outputFile = outputFile
MultilingualTransformConfig.defaultRowStart = 1
MultilingualTransformConfig.stringResNameColumn = 0
MultilingualTransformConfig.sheetName = "${project.rootProject.name}"
MultilingualTransformConfig.excelFile = combinedExcel
for (i in 1..<excelHeader.size()) {
def headerColumn = excelHeader.get(i)
def language = headerColumn.get(0)
MultilingualTransformConfig.mappingValues(i, language)
println "mapping values $i, $language"
}
}
}
project.tasks.findByName("MultilingualTransform").doLast {
if (defaultLanguage != null) {
String overrideDefault = "${multilingual.OVERRIDE_DEFAULT_LANGUAGE_XML}"
if (overrideDefault.trim().toLowerCase() == "true") {
copy {
from "${outputFile.path}/values-$defaultLanguage/${defaultStringFileName}"
into "${outputFile.path}/values"
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。