0

How to load zk language file dynamically?

asked 2017-06-13 14:11:14 +0800

ankushk gravatar image ankushk
1

I want to load all zk language properties files from different application jars dynamically. How can I do that in zk..?

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-06-13 15:52:21 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

You can do that in your dependent jars.

/**
 * ModuleInitializer. Implemented the javax.servlet.ServletContainerInitializer
 * interface so it can be called if the web application is started and this
 * class is inside a jar that is placed in the WEB-INF/lib folder of the main
 * web application.<br>
 * The ServletContainerInitializer is a feature from servlet 3.0 specifications.
 * ----------------------------------------------------------------------------<br>
 * 
 * @see javax.servlet.ServletContainerInitializer
 * @Blog http://piotrnowicki.com/2011/03/using-servlets-3-0-
 *       servletcontainerinitializer/<br>
 * <br>
 * 
 * @author Stephan Gerth
 */
public class DummyModuleInitializer implements javax.servlet.ServletContainerInitializer {

    private static final Logger log = LoggerFactory.getLogger(DummyModuleInitializer.class);

    /**
     * Default constructor.
     */
    public DummyModuleInitializer() {
        super();
    }

    @Override
    public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
        // log.debug("ZKBOOST: [STARTING UI MODULE]: empty dummy module");
        System.out.println("ZKBOOST: [STARTING UI MODULE]: empty dummy module");
        registerLabels(ctx);
    }

    /**
     * Registers the labels. The I18N file name are set in the
     * OXModuleLabelLocator().
     * 
     * @see com.myfirm.dummy.init.DummyModuleLabelLocator
     * @param ctx
     *            The ServletContext.
     */
    public void registerLabels(ServletContext ctx) {
        Labels.register(new DummyModuleLabelLocator(ctx));
    }

}


/**
 * Module LabelLocator for URL.<br>
 * --------------------------------------------------------<br>
 * The files should be zk default named and in zk default folder.<br>
 * FileNames: <b>zk-label</b>.properties or <b>zk-label</b>_de_DE.properties,
 * ...<br>
 * Folder: src/main/resources/metainfo/ <br>
 * 
 * @author Stephan Gerth
 */
public class DummyModuleLabelLocator implements org.zkoss.util.resource.LabelLocator, Serializable {
    /**
     * Serial Version UID.
     */
    private static final long serialVersionUID = 1L;

    /**
     * The servlet context.
     */
    private ServletContext ctx;

    /**
     * Constructor.
     * 
     * @param _ctx
     *            Servlet context.
     */
    public DummyModuleLabelLocator(ServletContext _ctx) {
        this.ctx = _ctx;
    }

    @Override
    public URL locate(Locale _locale) throws Exception {

        /**
         * The '/' before the resource name is needed for running in Tomcat 8.
         */

        if (_locale != null) {
            return ctx.getResource("/zk-label" + "_" + _locale + ".properties");
        } else {
            return ctx.getResource("/zk-label" + ".properties");
        }
    }

}

best Stephan

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2017-06-13 14:11:14 +0800

Seen: 17 times

Last updated: Jun 13 '17

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More