国际化的代码中字符串的查找与替换
2016-11-17 13:36
246 查看
最近有国际客户要购买我们的应用,所以要做一个英文版的,然后我这边需要给到一个资源文件给翻译,自己的代码上的东西也要处理。
花了一点时间随手写了点代码,用来把原项目中代码中的字符串找了出来,替换掉了。
权给自己留个笔记吧,如果以后需要用就不用麻烦了
代码很小,不放上github了,以后也不打算维护
http://download.csdn.net/detail/yeshennet/9685557
花了一点时间随手写了点代码,用来把原项目中代码中的字符串找了出来,替换掉了。
权给自己留个笔记吧,如果以后需要用就不用麻烦了
代码很小,不放上github了,以后也不打算维护
http://download.csdn.net/detail/yeshennet/9685557
package org.yeshen.internationalize; public class Main { public static void main(String[] args){ InternationalizeOption option = new InternationalizeOption(); option.filePath = "D:\\WORLSPACE_ANDROID\\main\\android"; option.template = "<string name=\"keyy\">valuee</string>"; option.suffix=".java"; option.replaceTemplate = "getString(R.string.valuee)"; Internationalize object = new Internationalize(option); if(object.isVaild()){ //find and list all String in code object.find(); //print a map for Strings.xml object.write(); //write and replace "String in code" to "replaceTemplate(getString(R.string.valuee))" object.replace(); System.out.print("finish"); }else{ System.out.print("error params"); } } }
package org.yeshen.internationalize; public class InternationalizeOption { public static final char comment = '/'; public static final char commentAll = '*'; public static final char quto = '\"'; public static final char qutoIgnore = '\\'; public static final int end = -1; public static final String enableText = "\"\" qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM" + ",.;![]{}()^&|-=+%*1234567890_#/$:\\、:;'@<>?,"; public static final String keyName = "keyy"; public static final String valueName = "valuee"; public String filePath = "D:\\WORLSPACE_IOS\\main\\iosclient"; public String genFilePath = "D:\\WORLSPACE_IOS\\main\\gen"; public String template = "<string name=\"keyy\">valuee</string>"; public String replaceTemplate = "getString(R.string.valuee)"; public String suffix=".java"; public String valueKeyName = "LdStr"; public InternationalizeOption(){ } public boolean isVaild(){ return true; } }
package org.yeshen.internationalize; import java.io.File; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; public class Internationalize { private InternationalizeOption iOption; private File[] iFiles; private Map<String,String> iWords; private Map<String,String> iCopyWords; public Internationalize(InternationalizeOption option){ this.iOption = option; this.iWords = new LinkedHashMap<String,String>(900); if(iOption != null && iOption.isVaild()){ File srcDir = new File(iOption.filePath); if(!srcDir.isDirectory()){ return; } iFiles = srcDir.listFiles(); } } public boolean isVaild(){ return iFiles != null && iFiles.length > 0; } public void find(){ find(iFiles); } public void replace(){ replace(iFiles); } private void find(File[] fs) { for(File file:iFiles){ if(!file.isDirectory() ){ if(file.getName().endsWith(iOption.suffix)){ try{ new InternationalizeInFile(file,iWords,iOption).find(); }catch(Exception e){ e.printStackTrace(); } } }else{ find(file.listFiles()); } } } public void write(){ iCopyWords = new HashMap<String, String>(900); StringBuilder builder = new StringBuilder(); int i = 0; for(Entry<String, String> s : iWords.entrySet()){ String key = s.getKey(); String value = s.getValue() + i; String temp = iOption.template.replace(InternationalizeOption.keyName, value); temp = temp.replace("valuee", key); builder.append(temp); builder.append("\n"); iCopyWords.put(key, value); i++; } print(builder); } private void replace(File[] fs) { for(File file:fs){ if(!file.isDirectory() ){ if(file.getName().endsWith(iOption.suffix)){ try{ new InternationalizeInFile(file,iCopyWords,iOption).replace(); }catch(Exception e){ e.printStackTrace(); } } }else{ replace(file.listFiles()); } } } public void print(Object obj){ System.out.println(obj); } }
package org.yeshen.internationalize; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.HashSet; import java.util.Map; public class InternationalizeInFile { private static HashSet<Character> enble; static{ enble = new HashSet<Character>(); for(char s:InternationalizeOption.enableText.toCharArray()){ enble.add(s); } } private InternationalizeOption option; private StringBuffer cloneBuffer; private boolean isChinese; private StringBuffer sb; private Map<String,String> Words; private File target; private BufferedReader br; private char ch; private int token; public InternationalizeInFile(File file,Map<String,String> words,InternationalizeOption option){ this.sb = new StringBuffer(); this.cloneBuffer = new StringBuffer(); this.isChinese = false; this.target = file; this.Words = words; this.option = option; } public void handleChineseString(){ Words.put(sb.toString(), option.valueKeyName); print(sb); } public void find() throws Exception{ br = new BufferedReader(new InputStreamReader(new FileInputStream(target),"utf-8")); read(); while(token!=InternationalizeOption.end ){ if(ch == InternationalizeOption.comment){ skipComment(); } if(ch == InternationalizeOption.quto){ readString(); if(isChinese){ handleChineseString(); isChinese = false; } } read(); } br.close(); } private void readString()throws Exception{ sb = new StringBuffer(); read(); if(ch == InternationalizeOption.quto){ // "" return; } while(token!=InternationalizeOption.end){ if(ch == InternationalizeOption.qutoIgnore){ append(); read();//" append(); }else if(ch == InternationalizeOption.quto){ read(); return; }else{ append(); } read(); } throw new Exception("un ending"); } private void skipComment() throws Exception{ read(); if(ch == InternationalizeOption.comment){// // cloneBuffer.append(br.readLine()).append("\r\n"); read(); return; } if(ch == InternationalizeOption.commentAll){// /* */ read(); while(token!=InternationalizeOption.end){ if(ch == InternationalizeOption.commentAll){ read(); if(ch == InternationalizeOption.comment){ read(); return; } } read(); } } } private char read() throws IOException{ token = br.read(); ch = (char)token; cloneBuffer.append(ch); return ch; } private void append(){ sb.append(ch); if(!enble.contains(ch)){ this.isChinese = true; } } private void print(Object obj){ System.out.println(obj); } public void replace() throws Exception{ br = new BufferedReader(new InputStreamReader(new FileInputStream(target),"utf-8")); read(); while(token!=InternationalizeOption.end ){ if(ch == InternationalizeOption.comment){ skipComment(); } if(ch == InternationalizeOption.quto){ readString(); if(isChinese){ String key = sb.toString(); if(Words.containsKey(key)){ String value = Words.get(key); char last = cloneBuffer.charAt(cloneBuffer.length() - 1); cloneBuffer.delete(cloneBuffer.length() - key.length() - 3 , cloneBuffer.length()); cloneBuffer.append(option.replaceTemplate.replace(InternationalizeOption.valueName, value)); cloneBuffer.append(last); } isChinese = false; } } read(); } br.close(); String destFile = target.getAbsolutePath().replace(option.filePath, option.genFilePath); File file = new File(destFile); File folder = new File(file.getParent()); if(!folder.exists()){folder.mkdirs();} if(!file.exists()){file.createNewFile();} PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(destFile,false),"utf-8")); cloneBuffer.delete(cloneBuffer.length() - 3, cloneBuffer.length()); cloneBuffer.append("\r\n"); pw.write(cloneBuffer.toString()); pw.flush(); pw.close(); } }
相关文章推荐
- 查找本地文件中的特定字符串并替换代码
- 替换TXT文件里的字符串,一行一行查找替换,java代码
- Python 字符串操作实现代码(截取/替换/查找/分割)
- Tips:使用Xcode查找项目中的中文字符串,代码国际化
- Python 字符串操作实现代码(截取/替换/查找/分割)
- Dos批处理编程*字符串批量处理【查找|替换】 (转载)
- 查找与替换的基本代码用法之二(VBA)
- MemFindStrNoCase 字符串查找函数, 代码优化
- PHP在字符串中查找指定字符串并删除的代码
- 又一不错的应用-正则来查找替换字符串
- asp 用InStr查找特定字符串的代码
- 有关js 函数验证和字符串查找替换的方法
- 查找与替换的基本代码用法之三(批量替换) (VBA)
- asp 用InStr查找特定字符串的代码
- 使用VS正则表达式查找替换,批量修改Reflactor生成的代码问题
- 简单的查找替换字符串ui
- 查找与替换的基本代码用法之一(VBA)
- 文本编辑器 “查找与替换”操作的“全部替换”功能的实现代码 Visual Basic 2005 编写
- ruby 字符串查找替换插入
- delphi字符串列表(TStrings)中字符串替换函数代码