{"id":421,"date":"2009-07-27T14:37:47","date_gmt":"2009-07-27T12:37:47","guid":{"rendered":"http:\/\/raftaman.net\/?p=421"},"modified":"2021-05-15T11:46:41","modified_gmt":"2021-05-15T09:46:41","slug":"using-a-resourcebundle-for-localization","status":"publish","type":"post","link":"https:\/\/possiblelossofprecision.net\/?p=421","title":{"rendered":"Using a ResourceBundle for localization"},"content":{"rendered":"<p>There is a nice java tutorial from Sun how to use ReseouceBundle for localization: <a href=\"http:\/\/java.sun.com\/docs\/books\/tutorial\/i18n\/resbundle\/propfile.html\">http:\/\/java.sun.com\/docs\/books\/tutorial\/i18n\/resbundle\/propfile.html<\/a>.<\/p>\n<p>It&#8217;s actually rather simple: Just  create a new <code>ResourceBundle<\/code> object by invoking the <code>getBundle<\/code> method, specifying the base name and <code>Locale<\/code>:<\/p>\n<pre class=\"brush: java; gutter: false; title: ; notranslate\" title=\"\">\r\nResourceBundle labels = ResourceBundle.getBundle(&quot;LabelsBundle&quot;, currentLocale);\r\n<\/pre>\n<p>The <code>getBundle<\/code> method first looks for a class file that matches the base name and the <code>Locale<\/code>. If it can&#8217;t find a class file, it then checks for properties files. When the <code>getBundle<\/code> method locates the correct properties file, it returns a <code>PropertyResourceBundle<\/code> object containing the key-value pairs from the properties file. <\/p>\n<p>To retrieve the translated value from the <code>ResourceBundle<\/code>, invoke the <code>getString<\/code> method as follows:<\/p>\n<pre class=\"brush: java; gutter: false; title: ; notranslate\" title=\"\">\r\nString value = labels.getString(key);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There is a nice java tutorial from Sun how to use ReseouceBundle for localization: http:\/\/java.sun.com\/docs\/books\/tutorial\/i18n\/resbundle\/propfile.html. It&#8217;s actually rather simple: Just create a new ResourceBundle object by invoking the getBundle method, specifying the base name and Locale: ResourceBundle labels = ResourceBundle.getBundle(&quot;LabelsBundle&quot;, currentLocale); The getBundle method first looks for a class file that matches the base name and the Locale. If it&#8230; <a href=\"https:\/\/possiblelossofprecision.net\/?p=421\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4],"class_list":["post-421","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-java"],"_links":{"self":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=421"}],"version-history":[{"count":5,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/421\/revisions"}],"predecessor-version":[{"id":2697,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/421\/revisions\/2697"}],"wp:attachment":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}