JSF-1.1 I18n-如何在faces-config.xml中注册Custom ResourceBundle



当我在旧的JSF-1.x项目中对消息/字符串进行国际化时,我发现UTF-8呈现没有应用于消息。

我在这里遇到了一个类似的问题:在JSF 2.0应用中使用UTF-8编码的属性文件的-i18n

但这将适用于JSF-2的应用,我不能像上面解释的那样注册我的自定义资源包。(显示命名空间不匹配的部署错误。)

需要帮助在faces-config.xml中为JSF-1.x注册我的自定义资源包。

(我们在那里使用了消息捆绑包。但我不能像在资源捆绑包标签中那样在消息捆绑包中注册我的捆绑包类)

JSF-1.x中,不支持注册我自己的自定义ResourceBundle类。

因此,需要继续创建一个自定义标记<x:loadBundle>,该标记以正确的编码对Properties文件进行编码。

请参阅Facelets自定义标记"不渲染",以便在web.xml中注册Tag Library XML。然后在您的TagLibrary XML中,使用以下配置:-

<tag>
            <tag-name>loadBundle</tag-name>
            <handler-class>org.somepackage.facelets.tag.jsf.handler.LoadBundleHandler</handler-class>
</tag>

并扩展您的自定义LoadBundleHandler以扩展TagHandler facelets类-(代码与LoadBundleHandler@com.sun.facelets.tag.jsf.core.LoadBundleHandler 相同

现在你可以覆盖Control类,就像我在问题中提到的帖子一样。所以总的来说,你的课会是这样的:-

public final class LoadBundleHandler extends TagHandler {

    private static final String BUNDLE_EXTENSION = "properties";    // The default extension for langauge Strings (.properties)
    private static final String CHARSET = "UTF-8";
    private static final Control UTF8_CONTROL = new UTF8Control();  
    private final static class ResourceBundleMap implements Map {
        private final static class ResourceEntry implements Map.Entry {
            protected final String key;
            protected final String value;
            public ResourceEntry(String key, String value) {
                this.key = key;
                this.value = value;
            }
            public Object getKey() {
                return this.key;
            }
            public Object getValue() {
                return this.value;
            }
            public Object setValue(Object value) {
                throw new UnsupportedOperationException();
            }
            public int hashCode() {
                return this.key.hashCode();
            }
            public boolean equals(Object obj) {
                return (obj instanceof ResourceEntry && this.hashCode() == obj
                        .hashCode());
            }
        }
        protected final ResourceBundle bundle;
        public ResourceBundleMap(ResourceBundle bundle) {
            this.bundle = bundle;
        }
        public void clear() {
            throw new UnsupportedOperationException();
        }
        public boolean containsKey(Object key) {
            try {
                bundle.getString(key.toString());
                return true;
            } catch (MissingResourceException e) {
                return false;
            }
        }
        public boolean containsValue(Object value) {
            throw new UnsupportedOperationException();
        }
        public Set entrySet() {
            Enumeration e = this.bundle.getKeys();
            Set s = new HashSet();
            String k;
            while (e.hasMoreElements()) {
                k = (String) e.nextElement();
                s.add(new ResourceEntry(k, this.bundle.getString(k)));
            }
            return s;
        }
        public Object get(Object key) {
            try {
                return this.bundle.getObject((String) key);
            } catch( java.util.MissingResourceException mre ) {
                return "???"+key+"???";
            }
        }
        public boolean isEmpty() {
            return false;
        }
        public Set keySet() {
            Enumeration e = this.bundle.getKeys();
            Set s = new HashSet();
            while (e.hasMoreElements()) {
                s.add(e.nextElement());
            }
            return s;
        }
        public Object put(Object key, Object value) {
            throw new UnsupportedOperationException();
        }
        public void putAll(Map t) {
            throw new UnsupportedOperationException();
        }
        public Object remove(Object key) {
            throw new UnsupportedOperationException();
        }
        public int size() {
            return this.keySet().size();
        }
        public Collection values() {
            Enumeration e = this.bundle.getKeys();
            Set s = new HashSet();
            while (e.hasMoreElements()) {
                s.add(this.bundle.getObject((String) e.nextElement()));
            }
            return s;
        }
    }
    private final TagAttribute basename;
    private final TagAttribute var;

    public LoadBundleHandler(TagConfig config) {
        super(config);
        this.basename = this.getRequiredAttribute("basename");
        this.var = this.getRequiredAttribute("var");
    }

    public void apply(FaceletContext ctx, UIComponent parent)
            throws IOException, FacesException, FaceletException, ELException {
        UIViewRoot root = ComponentSupport.getViewRoot(ctx, parent);
        ResourceBundle bundle = null;
        FacesContext faces = ctx.getFacesContext();
        String name = this.basename.getValue(ctx);
        try {
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            if (root != null && root.getLocale() != null) {
                bundle = ResourceBundle.getBundle(name, root.getLocale(), cl, UTF8_CONTROL);
            } else {
                bundle = ResourceBundle
                        .getBundle(name, Locale.getDefault(), cl);
            }
        } catch (Exception e) {
            throw new TagAttributeException(this.tag, null , e);
        }
        ResourceBundleMap map = new ResourceBundleMap(bundle);
        faces.getExternalContext().getRequestMap().put(this.var.getValue(ctx),
                map);
    }

    protected static class UTF8Control extends Control {
        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, BUNDLE_EXTENSION);
            ResourceBundle bundle = null;
            InputStream stream = null;
            if (reload) {
                URL url = loader.getResource(resourceName);
                if (url != null) {
                    URLConnection connection = url.openConnection();
                    if (connection != null) {
                        connection.setUseCaches(false);
                        stream = connection.getInputStream();
                    }
                }
            } else {
                stream = loader.getResourceAsStream(resourceName);
            }
            if (stream != null) {
                try {
                    bundle = new PropertyResourceBundle(new InputStreamReader(stream, UTF8_CONTROL));
                } finally {
                    stream.close();
                }
            }
            return bundle;
        }
    }

}

您甚至可以通过删除此类中的basename属性来跳过<x:loadbundle var="msg basename="resources">中的basename配置。

最新更新