Class I18nXmlUtility


  • public class I18nXmlUtility
    extends Object
    Generally in Sakai, i18n happens at the moment when the user is accessing content - we invoke ResourceLoaders to get formatted messages using the locale that is appropriate to the user. Occasionally, we nest a few calls to ResourceLoader.getFormattedMessage(...), for instance the message: "An error has occurred with the service. Error code: 42; cause: A Sakaiger ate the paper" might be coded as follows: example.properties: service.error=An error has occurred with the service. Error code: {0}; cause: {1} service.sakaiger.ate.paper=A Sakaiger ate the paper ExampleContentReviewServiceImpl.java: message = rb.getFormattedMessage("service.error", 42, rb.getFormattedMessage("service.sakaiger.ate.paper")); We have an i18n use case in ContentReviewService that's slightly more complex: We persist error messages at the time that they are encountered (usually in a quartz job using the admin session), and these messages have to be displayed to end users who are potentially using varying locales. In order to accomplish this, we have to persist a model that represents any nesting of formatted messages, and all of their relevant arguments. Then when users of varying locales access the last error of their originality report, we can interpret our persisted model, and serve a helpful error message in the language appropriate to each individual. This class creates XML models representing the keys and arguments of what would normally be point-in-time calls to rb.getFormattedMessage()
    • Constructor Detail

      • I18nXmlUtility

        public I18nXmlUtility()
    • Method Detail

      • getDocumentBuilder

        public static DocumentBuilder getDocumentBuilder()
        Gets a DocumentBuilder instance
      • createXmlDocument

        public static Document createXmlDocument()
        Creates a new XML Document instance
      • addElementAndGetDocumentAsString

        public static String addElementAndGetDocumentAsString​(Document doc,
                                                              Element el)
        Adds the specified element to the XML document and returns the document's contents as a String
      • createFormattedMessageXML

        protected static Object createFormattedMessageXML​(Document document,
                                                          String key,
                                                          Object... args)
        Creates XML that represents a call to ResourceLoader.getFormattedMessage(). We define 'message' tags, which contain a 'key' tag followed by any number of 'arg' tags, or additional 'message' tags when nesting occurs. Consider the example: example.properties: service.error=An error has occurred with the service. Error code: {0}; cause: {1} service.sakaiger.ate.paper=A Sakaiger ate the paper ExampleContentReviewServiceImpl.java: message = rb.getFormattedMessage("service.error", 42, rb.getFormattedMessage("service.sakaiger.ate.paper")); This would be translated to: createFormattedMessageXML(doc, "service.error", 42, createFormattedMessageXML(doc, "service.sakaiger.ate.paper")) and the returned node would appear as follows: service.error 42 service.sakaiger.ate.paper Notice that nesting is achieved by passing an invocation of this method as an arg Throws a ContentReviewProviderException if document is null
        Parameters:
        document - the document that will be used to create XML
        key - the formatted message key
        args - the arguments to the formatted message. Elements are appended as is (assumed to be nested 'message' tags); everything else is embedded into 'arg' tags
        Returns:
        an XML element representing the i18n message key and arguments
      • getLocalizedMessage

        public static String getLocalizedMessage​(org.sakaiproject.util.ResourceLoader rb,
                                                 Node node)
        Parses an XML node expectedly created via createFormattedMessageXML, and localizes it into a user presentable message using the specified ResourceLoader