I didn’t seem to have broken myself how to add .properties files to the classpath. And I did not understand why. But I found a way to load the file from the file system (filesystem). Download dictionary:
UTF8ResourceLoader $utf8loader = new UTF8ResourceLoader(); $utf8loader.path = "D:\\_PROJECTS_\\Java\\Workspace\\Project\\bin\\Language\\"; ClassLoader $cloader = ClassLoader.getSystemClassLoader(); ResourceBundle dictionary = null; try{ return dictionary = $utf8loader.newBundle("dictionary", current_locale, "java.properties", $cloader, false); } catch (IllegalAccessException | InstantiationException | IOException e) { Quit.Error("Error occured while loading dictionary resource bundle." ); return null; }
To load localization files, you need to rewrite the method of the ResourceBundle class:
public class UTF8ResourceLoader extends Control { public String path; public void setPath(String path) { this.path = path; } @Override public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException, FileNotFoundException { if (!format.equals("java.properties")) return null; String bundleName = baseName + "_" + locale.getLanguage() + format.substring(format.lastIndexOf('.')); ResourceBundle bundle = null; InputStreamReader reader = null; FileInputStream fis = null; try { File file = new File(path, bundleName); if (file.isFile()) { fis = new FileInputStream(file); reader = new InputStreamReader(fis, Charset.forName("UTF-8")); bundle = new PropertyResourceBundle(reader); } } finally { IOUtils.closeQuietly(reader); IOUtils.closeQuietly(fis); } return bundle; } }
IOutils is from Apache Common IO Not all languages are encoded with ANSII, therefore non-encoded characters need to be replaced with a special code title=\u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u044b (c) http: // www .skipy.ru / technics / localization.html
Cap
final private static Locale current_locale = Locale.getDefault(); final private static ResourceBundle dictionary = getDictionary();
allows you to set values and make current_locale & dictionary constant, and then make all values final final constants:
final public static String art = dictionary.getString("art"); final public static String type = dictionary.getString("type"); ...
getApplicationPath.Do() + "/Language/dictionary"example;String bundlePath = getApplicationPath.Do() + "/Language/dictionary"; System.out.println(bundlePath);String bundlePath = getApplicationPath.Do() + "/Language/dictionary"; System.out.println(bundlePath);- Sergey