"ResourceBundle" for all files?

Obviously, this ResourceBundlerequires a properties file, such as syntax, in the found files.

We have a situation where we want to use whole files (in our case HTML files) as "values". This means that we do not have keys as such. Perhaps the file name will work as a key.

Here is the directory tree:

src/
    main/
        resources/
            html/
                content.html
                content_de.html
                content_fr.html
                content_es.html
                order.html
                order_de.html
                order_fr.html
                order_es.html

Now we need logic to find the correct file based on the current locale. If the current language is German and I am looking for a file html/content.html, it should find html/content_de.html. It does not have to be downloaded immediately. Is there any existing mechanism in Java? Do I need to do this manually?

- . , Java 6 SE -, ; , , .

№ 1: messages.properties, HTML . , (, , , ).

EDIT # 2: , .

+5
3

, , , " " -:

:

  public void attempt1(String baseName, String extension) {
    List<String> locales = buildLocaleStrings(Locale.getDefault());
    String resourceFound = null;

    for (String locale : locales) {
      String resourceName = baseName + locale + "." + extension;
      URL resource = getClass().getClassLoader().getResource(resourceName);
      if (resource != null) {
        resourceFound = resourceName;
        break;
      }
    }
    System.out.println("found #1: " + resourceFound);
  }

  private List<String> buildLocaleStrings(Locale localeBase) {
    String locale = "_" + localeBase;
    List<String> locales = new ArrayList<String>();

    while (locale.length() > 0) {
      locales.add(locale);
      locale = locale.replaceFirst("_[^_]*?$", "");
    }
    locales.add("");

    return locales;
  }

"" ResourceBundle toString():

  public void attempt2(String baseName, final String extension) {
    ResourceBundle.Control control = new ResourceBundle.Control() {

      private String resourceFound = null;

      @Override
      public List<String> getFormats(String baseName) {
        return Arrays.asList(extension);
      }

      @Override
      public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException {
        String bundleName = toBundleName(baseName, locale);
        String resourceName = toResourceName(bundleName, format);

        if (loader.getResource(resourceName) != null) {
          resourceFound = resourceName;
          return new ResourceBundle() {

            @Override
            public Enumeration<String> getKeys() {
              return null;
            }

            @Override
            protected Object handleGetObject(String key) {
              return null;
            }

          };
        }
        return null;
      }

      @Override
      public String toString() {
        return resourceFound;
      }
    };

    ResourceBundle.getBundle(baseName, control);

    System.out.println("found #2: " + control.toString());
  }

:

  public void proof() {
    attempt1("html/content", "html");
    attempt2("html/content", "html");
  }

.

, .

+1

, (.. , "en" , "fr" , es ' ), .

Properties , MessageFormat, , .

- , , .

content=content_{0}.html
order=order_{0}.html

{0} .

in .

Properties prop = new Properties();
try {
    MessageFormat messageFormat = new MessageFormat("");
    String property;
    // change to suit the locale you wish to serve
    String[] param = {"en"};

    prop.load(new FileReader(new File("/full/path/to/property/file.properties")));
    property = prop.getProperty("content");
    messageFormat.applyPattern(property);
    System.out.println(messageFormat.format(param));
} catch(IOException ioex) {
    System.out.println("no property file here");
}

:

content_en.html

, , , HTML, , , , String, , .

+1

, , , . API Java , API . , , - fr_FR, "content_fr.html" "order_fr.html", ?

, :

translation.properties:

localeCode = 

translation_fr.properties:

localeCode = fr

File translation_en.properties:

localeCode = en

Then you just need to read the value of "localeCode" and combine it with "content" and ".html" or "order" and ".html".

0
source

All Articles