// Copyright (c) 2005-2007, Mihai Preda import java.io.*; public class L { static String[] strings = null; static Class c = null; static final String[] defaultLang = {"" #include "defaultLang.inc" }; private static InputStream openLang(String langId) { if (c == null) { try { c = Class.forName("L"); } catch (ClassNotFoundException e) { return null; } } InputStream is = c.getResourceAsStream("/lang/" + langId); if (is == null) { is = c.getResourceAsStream("lang/" + langId); } return is; } private static String readLang(String langId, int maxLen) { InputStream is = openLang(langId); if (is == null) { return null; } char[] chars = new char[maxLen]; InputStreamReader reader; int nRead = 0; try { reader = new InputStreamReader(is, "UTF-8"); int nowRead; while (true) { nowRead = reader.read(chars, nRead, maxLen - nRead); /* if (nRead < 10) { System.out.println("read " + nowRead + " " + (int)chars[0]); } */ if (nowRead == -1) { break; } nRead += nowRead; if (nRead >= maxLen) { break; } } } catch (UnsupportedEncodingException e) { return null; } catch (IOException e) { return null; } int firstChar = 0; if (nRead > 0 && (int)chars[0] > 65000) { //skip BOM firstChar = 1; } /* if (nRead > 4) { System.out.println("" + nRead + ": " + (int)(chars[firstChar]) + ' ' + (int)(chars[firstChar + 1]) + ' ' + (int)(chars[firstChar + 2]) + ' ' + (int)(chars[firstChar + 3])); } */ String bigstr = new String(chars, firstChar, nRead - firstChar); return bigstr; } private static void loadStrings(String bigstr, String strings[]) { int pos = 0; String value; int maxKey = strings.length - 1; int nRead = bigstr.length(); //System.out.println("loadStrings " + nRead); //System.out.println(bigstr); while (pos < nRead) { //System.out.println("pos " + pos + "; nRead " + nRead); int endline = bigstr.indexOf('\n', pos); int start = bigstr.indexOf('{', pos); if (start == -1) { break; } if (endline != -1 && endline < start) { pos = endline + 1; continue; } int end = bigstr.indexOf('}', start + 1); if (end == -1) { break; } int key = Integer.parseInt(bigstr.substring(pos, start).trim()); if (key > maxKey) { break; } value = bigstr.substring(start + 1, end); pos = end + 2; int doubleLine = 0; while ((doubleLine = value.indexOf("\r\n", doubleLine)) >= 0) { value = value.substring(0, doubleLine) + value.substring(doubleLine + 1); } strings[key] = value; //System.out.println("" + key + ": " + value); } } private static final int MAX_STRINGS = 100; private static final int MAX_FILE_LEN = 8000; static void loadLanguage(String langId) { String bigstr = readLang(langId, MAX_FILE_LEN); if (bigstr == null) { strings = null; } else { strings = new String[MAX_STRINGS]; loadStrings(bigstr, strings); } } static String _(int key) { return (strings == null || key >= strings.length || strings[key] == null) ? (key < defaultLang.length) ? defaultLang[key] : "_" + key + '_' : strings[key]; } };