본문 바로가기

[IT/Programming]

Web site - 다국어 지원 (multi-language support)

728x90
반응형
# Web site - 다국어 지원 (multi-language support) 다국어를 지원하는 여러 방법들이 있는거 같긴한데... 내가 다루기 편한 형태로서 다국어 지원하려고 다음과 같은 JAVA class 들을 만듬. ## PH
  • 2016-01-29 : First posting.
## TOC ## class FileMap ```[.linenums.scrollable] package recoeve.db; import java.lang.StringBuilder; import java.util.Set; import java.util.HashSet; import java.util.ArrayList; import java.util.Map; import java.util.HashMap; import java.util.regex.Pattern; import java.util.regex.Matcher; import java.io.File; import java.io.FileReader; import java.io.IOException; public class FileMap { private static final String[] referersAllowed={ "localhost" , "recoeve.net" // , "kipid.tistory.com" }; private static final String imgPath="C:/Recoeve/img"; private static final String[] imgNames={ "/favicon.ico" , "/icon-Twitter.png", "/icon-Facebook.png" }; private static final String filePath="C:/Recoeve/sources/recoeve/db/html/"; private static final String[] fileNames={ "jquery.min.js" , "log-in.html", "to-log-in.html" //, "log-in.css", "log-in.js" , "log-out.html" , "user-page.html", "to-user-page.html" , "redirect.html", "remember-me.html" , "AJAX post test (Cross orgin policy) and Reco test.html", "Reco-musics-test.html" }; private static final int fileMapSize=50; private static final int fileLangMapSize=10; public static Set<String> refererSet; public static Map<String, String> imgMap; // imgMap.get("imgName") public static Map<String, Map<String, String>> fileMap; // fileMap.get("fileName").get("lang") public static final Pattern ptnReplacer=Pattern.compile("\\[--[\\s\\S]+?--\\]"); static { refererSet=new HashSet<String>(); for (String referer: referersAllowed) { refererSet.add(referer); } imgMap=new HashMap<String, String>(); for (String imgName: imgNames) { imgMap.put(imgName, imgPath+imgName); } fileMap=new HashMap<String, Map<String, String>>(fileMapSize); File file=null; String fileStr=null; file=new File(filePath+"lang.txt"); if (file.exists()) { try { StringBuilder sb=new StringBuilder(); int ch; FileReader reader=new FileReader(file); while((ch=reader.read())!=-1) { sb.append((char)ch); } reader.close(); fileStr=sb.toString(); } catch (IOException e) { System.out.println(e); } finally { file=null; } } StrArray langMap=new StrArray(fileStr, true, true); // System.out.println(langMap); fileStr=null; for (String fileName: fileNames) { file=new File(filePath+fileName); if (file.exists()) { try { StringBuilder sb=new StringBuilder(); int ch; FileReader reader=new FileReader(file); while((ch=reader.read())!=-1) { sb.append((char)ch); } reader.close(); fileStr=sb.toString(); } catch (IOException e) { System.out.println(e); } finally { file=null; } } if (fileStr!=null) { // System.out.println("\nfileName : "+fileName); fileMap.put(fileName, new HashMap<String, String>(fileLangMapSize)); Map<String, String> fileLangMap=fileMap.get(fileName); fileLangMap.put("df", fileStr); // default. int start=0; Matcher matchReplacer=ptnReplacer.matcher(fileStr); ArrayList<String> strList=new ArrayList<String>(); while (start<fileStr.length()) { if (matchReplacer.find(start)) { strList.add(fileStr.substring(start, matchReplacer.start())); strList.add(matchReplacer.group()); start=matchReplacer.end(); } else { strList.add(fileStr.substring(start)); start=fileStr.length(); } } if (strList.size()>1) { int colSize=langMap.getColSizeAtRow(0); for (int k=2;k<colSize;k++) { String lang=langMap.get(0,k); if (!lang.equals("desc")) { String strReplaced=""; String replaced=null; for (int i=0;i<strList.size();i++) { if (i%2==0) { strReplaced+=strList.get(i); } else { replaced=langMap.get(strList.get(i), lang); if (replaced==null||replaced.isEmpty()||replaced.equals("-")) { replaced=langMap.get(strList.get(i), "en"); // "en" is default lang. } if (replaced==null) { replaced=strList.get(i); } strReplaced+=replaced; } } fileLangMap.put(lang, strReplaced); // after replacing langMap. } } } fileStr=null; } } } public FileMap() {} public static boolean refererAllowed(String host) { return refererSet.contains(host); } public static String getImg(String imgName) { return imgMap.get(imgName); } public static String get(String fileName, String lang) { Map<String, String> fileLangMap=fileMap.get(fileName); if (fileLangMap==null) {return null;} String res=fileLangMap.get(lang); if (res==null) { res=fileLangMap.get("df"); } return res; } public static void main(String... args) { // System.out.println(FileMap.refererAllowed("localhost")); // System.out.println(FileMap.get("AJAX post test (Cross orgin policy) and Reco test.html", "en")); // System.out.println(Pattern.quote("[a-d]")); } } ```/ ## class FileMapWithVar ```[.linenums.scrollable] package recoeve.db; import java.lang.StringBuilder; import java.util.ArrayList; import java.util.Map; import java.util.HashMap; import java.util.regex.Pattern; import java.util.regex.Matcher; import java.io.File; import java.io.FileReader; import java.io.IOException; public class FileMapWithVar { private static String filePath="C:/Recoeve/sources/recoeve/db/html/"; private static final String[] fileNames={ "user-page.html" , "signed-up.html" // , "redirect.html", "remember-me.html" }; private static final int fileMapSize=50; private static final int fileLangMapSize=10; public static Map<String, Map<String, ArrayList<String>>> fileMap; // fileMap.get("fileName").get("lang") public static final Pattern ptnReplacer=Pattern.compile("\\[--[\\s\\S]+?--\\]"); public static final Pattern ptnVariable=Pattern.compile("\\{--[\\s\\S]+?--\\}"); static { fileMap=new HashMap<String, Map<String, ArrayList<String>>>(fileMapSize); File file=null; String fileStr=null; file=new File(filePath+"lang.txt"); if (file.exists()) { try { StringBuilder sb=new StringBuilder(); int ch; FileReader reader=new FileReader(file); while((ch=reader.read())!=-1) { sb.append((char)ch); } reader.close(); fileStr=sb.toString(); } catch (IOException e) { System.out.println(e); } finally { file=null; } } StrArray langMap=new StrArray(fileStr, true, true); // System.out.println(langMap); fileStr=null; for (String fileName: fileNames) { file=new File(filePath+fileName); if (file.exists()) { try { StringBuilder sb=new StringBuilder(); int ch; FileReader reader=new FileReader(file); while((ch=reader.read())!=-1) { sb.append((char)ch); } reader.close(); fileStr=sb.toString(); } catch (IOException e) { System.out.println(e); } finally { file=null; } } if (fileStr!=null) { // System.out.println("\nfileName : "+fileName); fileMap.put(fileName, new HashMap<String, ArrayList<String>>(fileLangMapSize)); Map<String, ArrayList<String>> fileLangMap=fileMap.get(fileName); ArrayList<String> strListVars=new ArrayList<String>(); Matcher matchVariable=ptnVariable.matcher(fileStr); // default int start=0; while (start<fileStr.length()) { if (matchVariable.find(start)) { strListVars.add(fileStr.substring(start, matchVariable.start())); strListVars.add(matchVariable.group()); start=matchVariable.end(); } else { strListVars.add(fileStr.substring(start)); start=fileStr.length(); } } fileLangMap.put("df", strListVars); // default. ArrayList<String> strList=new ArrayList<String>(); Matcher matchReplacer=ptnReplacer.matcher(fileStr); start=0; while (start<fileStr.length()) { if (matchReplacer.find(start)) { strList.add(fileStr.substring(start, matchReplacer.start())); strList.add(matchReplacer.group()); start=matchReplacer.end(); } else { strList.add(fileStr.substring(start)); start=fileStr.length(); } } if (strList.size()>1) { int colSize=langMap.getColSizeAtRow(0); for (int k=2;k<colSize;k++) { String lang=langMap.get(0,k); if (!lang.equals("desc")) { String strReplaced=""; String replaced=null; for (int i=0;i<strList.size();i++) { if (i%2==0) { strReplaced+=strList.get(i); } else { replaced=langMap.get(strList.get(i), lang); if (replaced==null||replaced.isEmpty()||replaced.equals("-")) { replaced=langMap.get(strList.get(i), "en"); // "en" is default lang. } if (replaced==null) { replaced=strList.get(i); } strReplaced+=replaced; } } strListVars=new ArrayList<String>(); matchVariable=ptnVariable.matcher(strReplaced); // [--lang--] replaced start=0; while (start<strReplaced.length()) { if (matchVariable.find(start)) { strListVars.add(strReplaced.substring(start, matchVariable.start())); strListVars.add(matchVariable.group()); start=matchVariable.end(); } else { strListVars.add(strReplaced.substring(start)); start=strReplaced.length(); } } fileLangMap.put(lang, strListVars); // after replacing langMap. } } } fileStr=null; } } } public FileMapWithVar() {} public static String get(String fileName, String lang, Map<String,String> varMap) { Map<String, ArrayList<String>> fileLangMap=fileMap.get(fileName); if (fileLangMap==null) {return null;} ArrayList<String> strList=fileLangMap.get(lang); if (strList==null) { strList=fileLangMap.get("df"); } String res=""; String replaced=null; for (int i=0;i<strList.size();i++) { if (i%2==0) { res+=strList.get(i); } else { replaced=varMap.get(strList.get(i)); if (replaced==null) { replaced=strList.get(i); } res+=replaced; } } return res; } public static void main(String... args) { Map<String,String> varMap=new HashMap<String,String>(); varMap.put("{--userIndex--}", "10000"); varMap.put("{--userId--}", "id"); varMap.put("{--user email--}", "id@email.com"); // System.out.println(FileMapWithVar.get("signed-up.html", "en", varMap)); } } ```/ ## RRA
  1. https://recoeve.net/
728x90
반응형