根据中文修改英文翻译,重新生成英文翻译文件(保证原有文件的顺序不变)

    /**
	 * 一键翻译
	 */
	@RequestMapping(value = "/filePostToTranslation",method = RequestMethod.POST)
	@ResponseBody
	public ModelMap filePostToTranslation(@RequestParam MultipartFile file1,
			@RequestParam MultipartFile file2,
			@RequestParam MultipartFile file3,
			@RequestParam MultipartFile file4,
			HttpServletRequest httpRequest) {
		Integer code = 0;
		ModelMap resultMap = new ModelMap();
		try {
			//转化要修改中文文件
			File file1_new = multipartfileTofile(file1);
			ArrayList<String> arrayList = toArrayByFileReader1(file1_new);
			//转化要修改英文文件
			File file2_new = multipartfileTofile(file2);
			ArrayList<String> arrayList2 = toArrayByFileReader1(file2_new);
			Map<String,Object> translationMap1 = new HashMap<>();
			for (int i = 0; i < arrayList.size(); i++) {
	            if(StringUtils.isNotBlank(arrayList.get(i))){
	            	translationMap1.put(arrayList.get(i), arrayList2.get(i));
	            }
	        }
			//转化中文文件
			File file3_new = multipartfileTofile(file3);
			ArrayList<String> arrayList3 = toArrayByFileReader1(file3_new);
			Map<String,Object> arrayList3_mapArr = new HashMap<>();
			for( String arrayList3_str: arrayList3){
				if(StringUtils.isNotBlank(arrayList3_str)){
					Map<String,Object> arrayList3_map = new HashMap<>();
					String[] arrayList3_stringArr = arrayList3_str.split("=");
					arrayList3_map.put(arrayList3_stringArr[0], arrayList3_stringArr[1]);
					arrayList3_mapArr.put(arrayList3_stringArr[1], arrayList3_map);
				}
			}
			Map<String,Object> translationMap2 = new HashMap<>();
			for(String key : arrayList3_mapArr.keySet()){
				boolean flag=translationMap1.containsKey(key);
				if(flag){
					Map<String,Object> map1 = new HashMap<>();
					String zn_String = String.valueOf(arrayList3_mapArr.get(key));
					String zn_Key = zn_String.substring(1, zn_String.length()-1).split("=")[0];
					map1.put(zn_Key, translationMap1.get(key));
					translationMap2.put(zn_Key, map1.get(zn_Key));
				}
			}
			//转化英文文件
			File file4_new = multipartfileTofile(file4);
			ArrayList<String> arrayList4 = toArrayByFileReader1(file4_new);
			resultMap.put("arrayListMap", translationMap2);
			resultMap.put("arrayList4", arrayList4);
			ArrayList<String> reslutStringArray = new ArrayList<>();
			for( String arrayList4_str: arrayList4){
				if(StringUtils.isNotBlank(arrayList4_str)){
					String key =  arrayList4_str.split("=")[0];
					boolean flag = translationMap2.containsKey(key);
					if(flag){
						String newString = key +"="+ String.valueOf(translationMap2.get(key));
						reslutStringArray.add(newString);
					}else{
						reslutStringArray.add(arrayList4_str);
					}
				}else{
					reslutStringArray.add(arrayList4_str);
				}
			}
			String newPath = "C:\\Users\\Administrator\\Desktop\\web\\message_en_US.properties";
			writeFileContext(reslutStringArray, newPath);
			
		} catch (Exception e) {
			e.printStackTrace();
			code = 1;
		}
		resultMap.put("code", code);
		return resultMap;
	}

MultipartFile转File

public File multipartfileTofile(MultipartFile file1){
	  //获得原来文件名(含后缀名)
		String originalFilename = file1.getOriginalFilename();
		int pos = originalFilename.lastIndexOf(".");
		//原文件后缀名
		String suffix = originalFilename.substring(pos);
		//保存文件
		String realPath = "C:\\Users\\Administrator\\Desktop\\web";
		//产生一个uuid随机文件名
		String uuid = UUID.randomUUID().toString();
		String fullPath = realPath + File.separator + uuid + suffix;
	    File file=  new File(fullPath);
	    try {
			file1.transferTo(file);
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
		}
	    return file;
	}

File按行读取数据,存于ArrayList

public static ArrayList<String> toArrayByFileReader1(File file) {
		// 使用ArrayList来存储每行读取到的字符串
		ArrayList<String> arrayList = new ArrayList<>();
		try {
			InputStreamReader inputReader = new InputStreamReader(new FileInputStream(file),"UTF-8");
			BufferedReader bf = new BufferedReader(inputReader);
			// 按行读取字符串
			String str;
			while ((str = bf.readLine()) != null) {
				arrayList.add(str);
			}
			bf.close();
			inputReader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 返回数组
		return arrayList;
	}

将ArrayList<String>按行写入到文件中

public static void writeFileContext(List<String>  strings, String path) throws Exception {
		File file = new File(path);
        //如果没有文件就创建
        if (!file.isFile()) {
            file.createNewFile();
        }
        BufferedWriter writer = new BufferedWriter(new FileWriter(path));
        for (String l:strings){
            writer.write(l + "\r\n");
        }
        writer.close();
    }

测试文件链接:https://pan.baidu.com/s/1f1D8SglQIF9wJ9SGN0tEmw 
提取码:blup

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值