Results 1 to 3 of 3

Thread: Database Driven Resource Bundles - Examples and best practice?

  1. #1

    Default Database Driven Resource Bundles - Examples and best practice?

    Hi,

    I am trying to implement a database driven resource bundle and am searching for the best implementation. I have the requirement to change the web page text based on the language and also what user has logged in. I'd like to access my resource bundle from my action classes as well as my JSP pages. My application uses struts 2 and spring 2.5.6.

    Spring is set up for annotation based configuration.

    My current implementation is as follows.
    Code:
    @Component
    public class CoreDatabaseResourceBundle extends ListResourceBundle {
    
        @Autowired
        private TranslationService translationService;
        private Locale locale;
        private String moduleName;
        private Object[][] contents;
    
        @PostConstruct
        public void initialise() {
            contents = this.translationService.getTranslations(this.moduleName, this.locale);
        }
    
        @Override
        public Object[][] getContents(){
            return contents; 
        }
    
    // other getters and setters omitted
    }
    Then on my JSP
    Code:
    <fmt:setLocale scope="page" value="en" />
    <fmt:setBundle basename="com.example.CoreDatabaseResourceBundle"/>
    
    <fmt:message key="test.message"/>
    When i debug the code the resource bundle initialises okay so the annotations are working. However when the JSP creates an instance of the resource bundle it does not do it via spring hence my translation service is null.

    Any pointers how to fix my current implementaion or pointers to a better more 'spring' way of implemention this is appreciated!

    Thanks
    Last edited by russellgordon; Sep 21st, 2009 at 02:41 PM. Reason: added code tags

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Use [ code][/code ] tags when posting code, that way it remains readable.

    Use a MessageSource instead of a ResourceBundle, use Spring for view resolving and it should work. The reference guide explains internationalization in more detail.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Thanks for your help that helped a lot. So to summarise

    I now extend an AbstractMessageSource

    Code:
    @Component("messageSource")
    public class TranslationMessageSource extends AbstractMessageSource {
    
        private static final Log LOGGER = LogFactory.getLog(TranslationMessageSource.class);
    
        @Autowired
        private TranslationService translationService;
        // getters/setters and other methods ommited for clarity
    }
    and in my jsp I use the spring tag for message

    Code:
    <%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
    <spring:message code="sample.test"/>
    and that now works fine.

    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •