final private static Locale current_locale = Locale.getDefault(); final private static ResourceBundle dictionary = getDictionary(); private static ResourceBundle getDictionary() { try { return ResourceBundle.getBundle(getApplicationPath.Do() + "/Language/dictionary", current_locale); } catch (NullPointerException | MissingResourceException e) { Quit.Error("Local language " + current_locale.getDisplayLanguage() + " wasn't found in database."); return null; } } 

Localization is stored in the folder where the program is launched. I have it, respectively, in /Workspace/project/bin/ and further Language/dictionary_en.properties . As you can see, the address in .getBundle () is full, the folder /bin/Language/ is added to the classpath - in the Eclipse Project Explorer I see it along with other libraries, and inside are my localization files.

But I can’t detect the reason why the compiler doesn’t find my file.

  • Could you check what is 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
  • Indicates exactly where the dictionary is. All other files not related to. Properties, the program regularly picks up from / bin. Path: / D: / _ PROJECTS_ / Java / Workspace / Project / bin / Language / dictionary - Katoteshi Fuku
  • I don’t know if this is necessary, but some strange way. Its beginning. - Sergey
  • Forgot to add that the .properties file encoding is a UTF-8 file (if it matters) - Katoteshi Fuku

1 answer 1

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"); ...