简体   繁体   English

Java中图像的无文件资源包?

[英]File-less resource bundle for images in Java?

I have a Java (JSF) web application that needs to be localized, including text, images and etc (numbers, dates). 我有一个需要本地化的Java(JSF)Web应用程序,包括文本,图像等(数字,日期)。 I'm trying to have programmers to only use 我试图让程序员只使用

<h:graphicImage value="#{images['i_print.png']}" ... />

... and have the correct localized image automatically picked in the background, according to the user's locale. ...,并根据用户的语言环境在后台自动选择正确的本地化图像。

One approach is to have resource bundles property files with different image paths for a certain image key. 一种方法是让资源束属性文件的某个图像键具有不同的图像路径。 Is there a way this can be done without property files at all? 有没有一种方法可以完全不用属性文件来完成?

I mean.. the images directory having the following structure: 我的意思是.. images目录具有以下结构:

-Images
--en
---en_US
---en_CA
--fr
---fr_FR

Is it possible to create resource bundles where the lookup would go through the most localized directory first and going up the tree until the image name is found? 是否有可能创建资源束,在这些资源束中查找将首先通过最本地化的目录并在树中向上进行直到找到映像名称?

So far I have the following: 到目前为止,我有以下内容:

(1) An images handler. (1)图像处理程序。

<resource-bundle>
    <base-name>com.example.ImageResourcesHandler</base-name>
    <var>images</var>
</resource-bundle>

(2) The handler implementation, which extends resource bundle and handles the call by overriding getObject (2)处理程序实现,该实现扩展资源束并通过重写getObject处理调用

public class ImageResourcesHandler extends ResourceBundle {

@Override
protected Object handleGetObject(String key) {...}


}

(3) An Images Control that extends ResourceBundle.Control and is supposed to the the BundleLoading and control (3)扩展ResourceBundle.Control并应绑定到BundleLoading和控件的Images控件

public class ImageControl extends ResourceBundle.Control { ... }

Right now I go through the images root directory and create a map with the names of images as the key and their path as values. 现在,我浏览图像根目录并创建一个以图像名称为键,其路径为值的地图。

The problem is I don't have a Bundle structure, meaning that if I have the key in two different locales, my map clearly doesn't work (two+ paths are mapped to the same key). 问题是我没有Bundle结构,这意味着如果我在两个不同的语言环境中拥有密钥,我的地图显然将不起作用(两个以上的路径映射到同一密钥)。

Do you have any ideas on how to accomplish this? 您对如何实现此目标有任何想法吗? I wouldn't like to maintain properties files. 我不想维护属性文件。

TL;DR: I would like to implement a mechanism that gets images according to the user's locale from the file system without using properties files. TL; DR:我想实现一种机制,该机制无需使用属性文件就可以根据用户的语言环境从文件系统获取图像。

Any ideas are highly appreciated. 任何想法都受到高度赞赏。 Thanks! 谢谢!

JSF has a built-in mechanism to do this: Resources. JSF有一个内置的机制可以做到这一点:资源。

A resource is localized by placing it in a directory named /resources/ locale / library in the .war. 通过将资源放在.war中名为/ resources / locale / library的目录中,可以对资源进行本地化。 So your .war might contain these files: 因此,您的.war可能包含以下文件:

  • /resources/images/i_print.png /resources/images/i_print.png
  • /resources/en_UK/images/i_print.png /resources/en_UK/images/i_print.png
  • /resources/de/images/i_print.png /resources/de/images/i_print.png
  • /resources/ja/images/i_print.png /resources/ja/images/i_print.png

…and so on. …等等。

For images, this is accessed as <h:graphicImage library="images" name="i_print.png"/> . 对于图像,可以通过<h:graphicImage library="images" name="i_print.png"/> JSF will automatically select the resources directory based on the locale of the view root. JSF将根据视图根的语言环境自动选择资源目录。

Stylesheets can be localized in the same way: <h:outputStylesheet library="css" name="apptheme.css"/> . 可以使用相同的方式来本地化样式表: <h:outputStylesheet library="css" name="apptheme.css"/>

For resources other than images and stylesheets, JSF provides a built-in EL resolver that treats the identifier resource like a map, whose keys are library + ":" + filename. 对于除图像和样式表以外的资源,JSF提供了一个内置的EL解析器,该标识符将标识符resource视为地图,其键为库+“:” +文件名。 So page authors could access the above resource as #{resource['images:i_print.png']} , though I think it's far more legible to use the library and name attributes. 因此页面作者可以使用#{resource['images:i_print.png']}来访问上述资源,尽管我认为使用libraryname属性更#{resource['images:i_print.png']}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM