Index: lams_common/src/java/org/lamsfoundation/lams/tool/OutputFactory.java =================================================================== diff -u -r55787e184b4775a776ed958656ba17cda1a8c4c8 -ra818550d0d742ccd872808a455481913b88d8901 --- lams_common/src/java/org/lamsfoundation/lams/tool/OutputFactory.java (.../OutputFactory.java) (revision 55787e184b4775a776ed958656ba17cda1a8c4c8) +++ lams_common/src/java/org/lamsfoundation/lams/tool/OutputFactory.java (.../OutputFactory.java) (revision a818550d0d742ccd872808a455481913b88d8901) @@ -76,9 +76,9 @@ protected Logger log = Logger.getLogger(OutputFactory.class); private MessageService toolMessageService; - private ILoadedMessageSourceService loadedMessageSourceService; private String languageFilename; + private MessageSource msgSource = null; // derived from toolMessageService, loadedMessageSourceService, languageFilename protected final String KEY_PREFIX = "output.desc."; /** Create a map of the tool output definitions, suitable for returning from the method @@ -96,7 +96,7 @@ public abstract SortedMap getToolOutputDefinitions(Object toolContentObject) throws ToolException; /** Tool specific toolMessageService, such as the forumMessageService in the Forum tool */ - public MessageService getToolMessageService() { + private MessageService getToolMessageService() { return toolMessageService; } @@ -108,7 +108,7 @@ /** Set the common loadedMessageSourceService, based on the bean defined in the common Spring context. * If toolMessageService is not set, then the languageFilename * and loadedMessageSourceService should be set. */ - public ILoadedMessageSourceService getLoadedMessageSourceService() { + private ILoadedMessageSourceService getLoadedMessageSourceService() { return loadedMessageSourceService; } @@ -129,50 +129,64 @@ } /** - * Get the I18N description for this definitionName. If the tool has supplied a messageService, then this - * is used to look up the key and hence get the description. Otherwise if the tool has supplied a I18N + * Get the I18N description for this key. If the tool has supplied a messageService, then this + * is used to look up the key and hence get the text. Otherwise if the tool has supplied a I18N * languageFilename then it is accessed via the shared toolActMessageService. If neither are supplied or - * the key is not found, then any "." in the name are converted to space and this is used as the description. + * the key is not found, then any "." in the name are converted to space and this is used as the return value. * - * The key must be in the format output.desc.[definition name]. For example a + * This is normally used to get the description for a definition, in whic case the key should be in the format + * output.desc.[definition name], key = definition name and addPrefix = true. For example a * definition name of "learner.mark" becomes output.desc.learner.mark. + * + * If you want to use this to get an arbitrary string from the I18N files, then set addPrefix = false and the + * output.desc will not be added to the beginning. */ - protected String getDescription(String definitionName) { - MessageSource msgSource = null; - if ( getToolMessageService() != null ) { - msgSource = getToolMessageService().getMessageSource(); - } - if ( msgSource == null && getLoadedMessageSourceService() != null && getLanguageFilename() != null) { - msgSource = getLoadedMessageSourceService().getMessageService(getLanguageFilename()); - } - if ( msgSource == null ) { - log.warn("Unable to internationalise the description for the output definition "+definitionName+" as no MessageSource is available. "+ - "The tool's OutputDefinition factory needs to set either (a) messageSource or (b) loadedMessageSourceService and languageFilename."); - } - - String description = null; - if ( msgSource != null ) { - String key = KEY_PREFIX + definitionName; + protected String getI18NText(String key, boolean addPrefix) { + String translatedText = null; + + MessageSource tmpMsgSource = getMsgSource(); + if ( tmpMsgSource != null ) { + if ( addPrefix ) key = KEY_PREFIX + key; Locale locale = LocaleContextHolder.getLocale(); try { - description = msgSource.getMessage(key,null,locale); + translatedText = tmpMsgSource.getMessage(key,null,locale); } catch ( NoSuchMessageException e ) { + log.warn("Unable to internationalise the text for key "+key+" as no matching key found in the msgSource"); } - } - if ( description == null || description.length() == 0 ) { - description = definitionName.replace('.', ' '); + } else { + log.warn("Unable to internationalise the text for key "+key+" as no matching key found in the msgSource. The tool's OutputDefinition factory needs to set either (a) messageSource or (b) loadedMessageSourceService and languageFilename."); } - return description; + if ( translatedText == null || translatedText.length() == 0 ) { + translatedText = key.replace('.', ' '); + } + + return translatedText; } + /** + * Get the MsgSource, combining getToolMessageService() and getLoadedMessageSourceService(). Caches the result so it only needs to be calculated once (most tools will require + * more than one call to this code! + */ + private MessageSource getMsgSource() { + if ( msgSource == null ) { + if ( getToolMessageService() != null ) { + msgSource = getToolMessageService().getMessageSource(); + } + if ( msgSource == null && getLoadedMessageSourceService() != null && getLanguageFilename() != null) { + msgSource = getLoadedMessageSourceService().getMessageService(getLanguageFilename()); + } + } + return msgSource; + } + /** Generic method for building a tool output definition. It will get the definition's description * from the I18N file using the getDescription() method. * Only use if the other buildBlahDefinitions do not suit your needs. */ protected ToolOutputDefinition buildDefinition(String definitionName, OutputType type, Object startValue, Object endValue, Object complexValue, Boolean showConditionNameOnly) { ToolOutputDefinition definition = new ToolOutputDefinition(); definition.setName(definitionName); - definition.setDescription(getDescription(definitionName)); + definition.setDescription(getI18NText(definitionName, true)); definition.setType(type); definition.setStartValue( startValue ); definition.setEndValue( endValue ); @@ -221,11 +235,11 @@ List defaultConditions = new ArrayList(); defaultConditions.add(new BranchCondition(null, null, new Integer(1), definitionName, - getDescription(definitionName+".true"), + getI18NText(definitionName+".true", true), OutputType.OUTPUT_BOOLEAN.toString(), null, null, Boolean.TRUE.toString())); defaultConditions.add(new BranchCondition(null, null, new Integer(2), definitionName, - getDescription(definitionName+".false"), + getI18NText(definitionName+".false", true), OutputType.OUTPUT_BOOLEAN.toString(), null, null, Boolean.FALSE.toString())); definition.setDefaultConditions(defaultConditions); Index: lams_tool_forum/src/java/org/lamsfoundation/lams/tool/forum/service/ForumOutputFactory.java =================================================================== diff -u -r36c95f28c887810cf5c22f0434a8852d26fbb068 -ra818550d0d742ccd872808a455481913b88d8901 --- lams_tool_forum/src/java/org/lamsfoundation/lams/tool/forum/service/ForumOutputFactory.java (.../ForumOutputFactory.java) (revision 36c95f28c887810cf5c22f0434a8852d26fbb068) +++ lams_tool_forum/src/java/org/lamsfoundation/lams/tool/forum/service/ForumOutputFactory.java (.../ForumOutputFactory.java) (revision a818550d0d742ccd872808a455481913b88d8901) @@ -78,6 +78,6 @@ private ToolOutput getNumPosts(IForumService forumService, Long learnerId, Long toolSessionId) { int num = forumService.getTopicsNum(learnerId, toolSessionId); - return new ToolOutput(OUTPUT_NAME_LEARNER_NUM_POSTS, "", new Long(num)); + return new ToolOutput(OUTPUT_NAME_LEARNER_NUM_POSTS, getI18NText(OUTPUT_NAME_LEARNER_NUM_POSTS, true), new Long(num)); } } Index: lams_tool_lamc/src/java/org/lamsfoundation/lams/tool/mc/service/MCOutputFactory.java =================================================================== diff -u -r7ec5fb1ceecbc55268fd3a8cc5ce950eaef7e3bb -ra818550d0d742ccd872808a455481913b88d8901 --- lams_tool_lamc/src/java/org/lamsfoundation/lams/tool/mc/service/MCOutputFactory.java (.../MCOutputFactory.java) (revision 7ec5fb1ceecbc55268fd3a8cc5ce950eaef7e3bb) +++ lams_tool_lamc/src/java/org/lamsfoundation/lams/tool/mc/service/MCOutputFactory.java (.../MCOutputFactory.java) (revision a818550d0d742ccd872808a455481913b88d8901) @@ -106,7 +106,7 @@ private ToolOutput getLearnerMark(McQueUsr queUser) { Long mark = queUser != null ? queUser.getLastAttemptTotalMark() : new Long(0); return new ToolOutput(MCOutputFactory.OUTPUT_NAME_LEARNER_MARK, - getDescription(MCOutputFactory.OUTPUT_NAME_LEARNER_MARK), mark); + getI18NText(MCOutputFactory.OUTPUT_NAME_LEARNER_MARK, true), mark); } /** @@ -117,7 +117,7 @@ private ToolOutput getLearnerAllCorrect(IMcService mcService, McQueUsr queUser) { boolean allCorrect = allQuestionsCorrect(mcService, queUser); return new ToolOutput(MCOutputFactory.OUTPUT_NAME_LEARNER_ALL_CORRECT, - getDescription(MCOutputFactory.OUTPUT_NAME_LEARNER_ALL_CORRECT), allCorrect); + getI18NText(MCOutputFactory.OUTPUT_NAME_LEARNER_ALL_CORRECT, true), allCorrect); } // written to cope with more than one correct option for each question but only tested with