How to customized properties folder in wicket

2019-06-13 06:49发布

问题:

in my application I have about about 20 pages, Each page have about 4 properties file for each language. Now all properties have to be in same directory as is *. java file. Is there option to change in ? I want to create new directory properties and here add all properties file. Or is there option to add all localized message in one file ? Problem is that in page a.hmtl properties "name" has value "a" and in another page b.html has the same key another value: "name" value "b"

I try to add

        this.getResourceSettings().addResourceFolder("/properties");

but with no success

回答1:

Have a look at the example on the wicket-library. There are several different ways to archieve this. I'm sure you'll find one that suits your needs.

Short version: For maximum control you'll have to write your own IResourceStreamLocator along with your own IResourceStream and set wicket to use them in Application.init().



回答2:

Now all properties have to be in same directory as is *. java file

Not at all, the properties file does not need to be next to the java file necessarily. The property files are scanned following the default lookup algorithm, as described in the Wicket Free Guide:

  1. ComponentStringResourceLoader: implements most of the default algorithm. It searches for a given resource across bundles from the container hierarchy, from class hierarchy and from the given component.
  2. PackageStringResourceLoader: searches into package bundles.
  3. ClassStringResourceLoader: searches into bundles of a given class. By default the target class is the application class.
  4. ValidatorStringResourceLoader: searches for resources into validator's bundles. A list of validators is provided by the form component that failed validation.
  5. InitializerStringResourceLoader: this resource allows internationalization to interact with the initialization mechanism of the framework that will be illustrated in paragraph 15.4.

Read more in chapter 12.4.

If you want to keep it simple I would recommend you to have just one property file for each language. These property files could be located next to your application class e.g.

WicketApplication.properties.xml
WicketApplication_en.properties.xml
WicketApplication_zh.properties.xml
WicketApplication_de.properties.xml

This method saves you from looking through several localization files and avoids bugs originating from complex message overriding rules.



回答3:

I didnt find easier solution. I write my own class where I changing path to my properties:

package org.toursys.web.finder;

import org.apache.wicket.util.resource.IResourceStream;
import org.apache.wicket.util.resource.locator.ResourceStreamLocator;

public class CustomResourceStreamLocator extends ResourceStreamLocator {

    @Override
    protected IResourceStream locateByClassLoader(Class<?> clazz, final String path) {
        String newPath = path;
        if (newPath.endsWith(".properties")) {
            newPath = path.substring(0, path.lastIndexOf("/")) + "/properties" + path.substring(path.lastIndexOf("/"));
        }
        IResourceStream stream = super.locateByClassLoader(clazz, newPath);
        if (stream == null) {
            stream = super.locateByClassLoader(clazz, path);
        }
        return stream;
    }

}

and then I add this stream locator to my app