{"id":4437,"date":"2012-03-01T14:05:37","date_gmt":"2012-03-01T19:05:37","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=4437"},"modified":"2012-06-18T12:54:47","modified_gmt":"2012-06-18T17:54:47","slug":"jsf-message-bundle","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=4437","title":{"rendered":"JSF 2 Resource\/Message Bundle"},"content":{"rendered":"<div class='toc wptoc'>\n<h2>Contents<\/h2>\n<ol class='toc-odd level-1'>\n\t<li>\n\t\t<a href=\"#Prepare_Eclipse_Project\">Prepare Eclipse Project<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Create_a_Resource_Bundle_File\">Create a Resource Bundle File<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Use_in_JSF_Pages\">Use in JSF Pages<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Use_ResourceBundle_Directly_in_Back_Beans\">Use <em>ResourceBundle <\/em>Directly in Back Beans<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Use_Injectable_ResourceBundle_via_CDI_Producer\">Use Injectable ResourceBundle via CDI Producer<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#References\">References<\/a>\n\t<\/li>\n<\/ol>\n<\/ol>\n<\/div>\n<div class='wptoc-end'>&nbsp;<\/div>\n<span id=\"Prepare_Eclipse_Project\"><h2>Prepare Eclipse Project<\/h2><\/span>\n<p>* Follow <a href=\"?p=4373\">this post<\/a> to setup <em>test.cdi<\/em> test app if needed<br \/>\n* Or you can download the source code for this example from <a href='https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/03\/test.cdi_.msg_.bundle.zip'>test.cdi.msg.bundle<\/a><\/p>\n<span id=\"Create_a_Resource_Bundle_File\"><h2>Create a Resource Bundle File<\/h2><\/span>\n<p>* Create a new Java package named <strong>resources<\/strong><br \/>\n* Create a message bundle file named <strong>mymessage.properties<\/strong> with content:<\/p>\n<pre lang=\"txt\">\r\nmessage1=Hello\r\nmessage2= world\r\nmsg.dotted.one=This is a dotted message.\r\nmsg.with.params.one=This is message with parameters: parameter one: \"{0}\" and parameter two: \"{1}\"\r\n<\/pre>\n<span id=\"\"><h6><a href=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/03\/weld_hello_msgbundle_pkg.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/03\/weld_hello_msgbundle_pkg.jpg\" alt=\"\" title=\"weld_hello_msgbundle_pkg\" width=\"222\" height=\"92\" class=\"aligncenter size-full wp-image-4438\" \/><\/a><\/h6><\/span>\n<span id=\"Use_in_JSF_Pages\"><h2>Use in JSF Pages<\/h2><\/span>\n<p>* Configure either a <em>message-bundle<\/em> and\/or(?) <em>resource-bundle<\/em> in <em>WEB-INF\/faces-config.xml<\/em><\/p>\n<pre lang=\"xml\">\r\n  <application>\r\n    ...\r\n    <message-bundle>resources.mymessage<\/message-bundle>\r\n    <resource-bundle>\r\n\t\t<base-name>resources.mymessage<\/base-name>\r\n\t\t<var>mymsg<\/var>\r\n   <\/resource-bundle>\r\n  <\/application>\r\n<\/pre>\n<p>* Use <em>message-bundle<\/em> in pages.<br \/>\n* Note that you use <em>f:loadbundle<\/em> to load bundle in individual pages.<\/p>\n<pre lang=\"html\">\r\n<body>\r\n\t<p>\r\n\t<f:loadBundle basename=\"resources.mymessage\" var=\"msg\"><\/f:loadBundle>\r\n\tFrom message bundle: #{msg.message1} #{msg.message2}  \r\n\t<\/p>\r\n<\/pre>\n<p>* Or use resource-bundle in pages.<br \/>\n* Note that you do <em>not <\/em>need to use <em>f:loadbundle<\/em> to load bundle in individual pages.<\/p>\n<pre lang=\"html\">\r\n<body>\r\n<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/html4\/loose.dtd\">\r\n<html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"\r\n\txmlns:h=\"http:\/\/java.sun.com\/jsf\/html\"\r\n\txmlns:tr=\"http:\/\/myfaces.apache.org\/trinidad\"\r\n\txmlns:f=\"http:\/\/java.sun.com\/jsf\/core\">\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=ISO-8859-1\" \/>\r\n<title>Insert title here<\/title>\r\n<\/head>\r\n<body>\r\n\t<b>Greetings from resource bundle:<\/b> #{mymsg.message1} #{mymsg.message2}  \r\n\r\n\t<br \/>\r\n\t<b>Greetings from resource-bundle with \"dotted\" message:<\/b> #{mymsg['msg.dotted.one']}\r\n\r\n\t<br \/>\r\n\t<b>Greetings from resource-bundle with parameters:<\/b>\r\n\t<h:outputFormat value=\"#{mymsg['msg.with.params.one']}\">\r\n\t\t<f:param value=\"Hello\"\/>\r\n\t\t<f:param value=\"World\"\/>\r\n\t<\/h:outputFormat>\r\n<\/body>\r\n<\/html>\r\n<\/pre>\n<p>* Browser output:<\/p>\n<pre lang=\"txt\">\r\nGreetings from resource bundle: Hello world\r\nGreetings from resource-bundle with \"dotted\" message: This is a dotted message.\r\nGreetings from resource-bundle with parameters:This is message with parameters: parameter one: \"Hello\" and parameter two: \"World\"\r\n<\/pre>\n<span id=\"_1\"><h6><a href=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/03\/weld_hello_msgbundle_test.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/03\/weld_hello_msgbundle_test.jpg\" alt=\"\" title=\"weld_hello_msgbundle_test\" width=\"286\" height=\"114\" class=\"aligncenter size-full wp-image-4443\" \/><\/a><\/h6><\/span>\n<span id=\"Use_ResourceBundle_Directly_in_Back_Beans\"><h2>Use <em>ResourceBundle <\/em>Directly in Back Beans<\/h2><\/span>\n<p>* Back bean class:<\/p>\n<pre lang=\"java\">\r\npackage hello;\r\n\r\nimport java.text.MessageFormat;\r\nimport java.util.Locale;\r\nimport java.util.ResourceBundle;\r\n\r\nimport javax.annotation.PostConstruct;\r\nimport javax.faces.context.FacesContext;\r\nimport javax.inject.Named;\r\n\r\n@Named\r\npublic class GreetingSvc1 {\r\n\tResourceBundle rs;\r\n\tString msg1 = \"message2\";\r\n\t\r\n\t@PostConstruct\r\n\tprivate void init(){\r\n\t\tFacesContext context = FacesContext.getCurrentInstance();\r\n        Locale locale=context.getViewRoot().getLocale();\r\n        rs=ResourceBundle.getBundle(\"resources.mymessage\",locale);\r\n\t}\r\n\r\n\tpublic String getMessageBundleGreeting(){\r\n\t\treturn rs.getString(msg1);\r\n\t}\r\n\r\n\tpublic String getParameterizedGreeting() {\r\n\t\treturn MessageFormat.format(rs.getString(\"msg.with.params.one\"),\r\n\t\t\t\t\"Hello\", \"World\");\r\n\t}\r\n}\r\n<\/pre>\n<p>* JSF page:<\/p>\n<pre lang=\"xml\">\r\n\t<br \/>\r\n\t<b>Greetings from greetingSvc1:<\/b> #{greetingSvc1.messageBundleGreeting}\r\n\r\n\t<br \/>\r\n\t<b>Greetings from greetingSvc1:<\/b> #{greetingSvc1.parameterizedGreeting}\r\n<\/pre>\n<p>* Browser output:<\/p>\n<pre lang=\"txt\">\r\nGreetings from greetingSvc1: world\r\nGreetings from greetingSvc1: This is message with parameters: parameter one: \"Hello\" and parameter two: \"World\"\r\n<\/pre>\n<span id=\"Use_Injectable_ResourceBundle_via_CDI_Producer\"><h2>Use Injectable ResourceBundle via CDI Producer<\/h2><\/span>\n<p>* Create a new <strong>MyMessageBundle<\/strong> qualifier class:<\/p>\n<pre lang=\"java\">\r\npackage hello;\r\n\r\nimport static java.lang.annotation.ElementType.FIELD;\r\nimport static java.lang.annotation.ElementType.METHOD;\r\nimport static java.lang.annotation.ElementType.PARAMETER;\r\nimport static java.lang.annotation.ElementType.TYPE;\r\nimport static java.lang.annotation.RetentionPolicy.RUNTIME;\r\n\r\nimport java.lang.annotation.Retention;\r\nimport java.lang.annotation.Target;\r\n\r\nimport javax.inject.Qualifier;\r\n@Qualifier\r\n@Retention(RUNTIME)\r\n@Target({ TYPE, METHOD, FIELD, PARAMETER })\r\npublic @interface MyMessageBundle {\r\n\r\n}\r\n<\/pre>\n<p>* Create a new <strong>MyMessageBundleProvider<\/strong> producer class:<\/p>\n<pre lang=\"java\">\r\npackage hello;\r\n\r\nimport java.util.Locale;\r\nimport java.util.ResourceBundle;\r\n\r\nimport javax.enterprise.inject.Produces;\r\nimport javax.faces.context.FacesContext;\r\n\r\npublic class MyMessageBundleProvider {\r\n\r\n\tprivate ResourceBundle bundle;\r\n\r\n\t@Produces\r\n\t@MyMessageBundle\r\n\tpublic ResourceBundle getBundle() {\r\n\t\tif (this.bundle == null) {\r\n\t\t\tFacesContext context = FacesContext.getCurrentInstance();\r\n\t\t\tLocale locale = context.getViewRoot().getLocale();\r\n\t\t\tbundle = ResourceBundle.getBundle(\"resources.mymessage\", locale);\r\n\t\t}\r\n\t\treturn this.bundle;\r\n\t}\r\n}\r\n<\/pre>\n<p>* Create a new <strong>GreetingSvc2<\/strong> back bean class and inject ResourceBundle :<\/p>\n<pre lang=\"java\">\r\npackage hello;\r\n\r\nimport java.text.MessageFormat;\r\nimport java.util.ResourceBundle;\r\n\r\nimport javax.inject.Inject;\r\nimport javax.inject.Named;\r\n\r\n@Named\r\npublic class GreetingSvc2 {\r\n\tString msg1 = \"message1\";\r\n\r\n\t@Inject\r\n\t@MyMessageBundle\r\n\tprivate ResourceBundle rs;\r\n\r\n\tpublic String getMessageBundleGreeting() {\r\n\t\treturn rs.getString(msg1);\r\n\t}\r\n\r\n\tpublic String getParameterizedGreeting() {\r\n\t\treturn MessageFormat.format(rs.getString(\"msg.with.params.one\"),\r\n\t\t\t\t\"Hello\", \"World\");\r\n\t}\r\n}\r\n<\/pre>\n<p>* JSF page:<\/p>\n<pre lang=\"xml\">\r\n\t<br \/>\r\n\t<b>Greetings from greetingSvc2:<\/b> #{greetingSvc2.messageBundleGreeting}\r\n\r\n\t<br \/>\r\n\t<b>Greetings from greetingSvc2 parameterized:<\/b> #{greetingSvc2.parameterizedGreeting}\r\n<\/pre>\n<p>* Browser output:<\/p>\n<pre lang=\"txt\">\r\nGreetings from greetingSvc2: Hello\r\nGreetings from greetingSvc2 parameterized: This is message with parameters: parameter one: \"Hello\" and parameter two: \"World\" \r\n<\/pre>\n<span id=\"References\"><h2>References<\/h2><\/span>\n<p>* <a href=\"http:\/\/www.mkyong.com\/jsf2\/jsf-2-0-and-resource-bundles-example\/\">JSF 2.0 and Resource Bundles example<\/a><br \/>\n* <a href=\"http:\/\/www.java-javafx.com\/2010\/09\/jsf2-and-internationalization-i18n.html\">JSF2 and Internationalization (I18N)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Prepare Eclipse Project * Follow this post to setup test.cdi test app if needed * Or you can download the source code for this example from test.cdi.msg.bundle Create a Resource Bundle File * Create a new Java package named resources &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=4437\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[120],"tags":[],"class_list":["post-4437","post","type-post","status-publish","format-standard","hentry","category-jsf"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-19z","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/4437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4437"}],"version-history":[{"count":17,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/4437\/revisions"}],"predecessor-version":[{"id":4441,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/4437\/revisions\/4441"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}