jawr
  1. jawr
  2. JAWR-207

i18n: unit testing javascript when using "messages"

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Jawr Core
    • Labels:
      None

      Description


      Sorry if this is not the usual way to submit improvement ideas, I didn't know where else to go. Please move it to a more appropriate area if needed.


      When using JAWR i18n, it generates the following javascript object:

      function MyJsModule() {
      
          var text = messages.my.message(1, 2);
      
      }
      

      This means this javascript code is hard to unit test, unless the tests also define a global messages variable with the right nested objects.

      • Is there a way to control how JAWR generates the messages object to make it more test-friendly?
      • Could JAWR have an option to generate a messages function/module instead, that could easily be mocked when needed?

      My current workaround is:

      function getMessage() {
          var args = Array.prototype.slice.call(arguments);
          var messageId = args.shift();
          var messageFunc = window.messages;        
          messageId.split('.').forEach(function(part) {
              messageFunc = messageFunc[part];
          });
          return messageFunc(args);
      }
      

      This allows me to re-write the first code sample like this.

      function MyJsModule(getMessage) {
      
          var text = getMessage('my.message', 1, 2);
      
      }
      

      Here the module is easily testable by injecting a stub getMessage function. However it seems strange to have to re-process an OGNL-like notation to find the right message every time. Could JAWR generate a hashmap of messages, and provide a function to access them? Something like

      window.messages = {
          map: {
            'my.message': 'this is a message',
            'my.other.message': 'and another {0}',
          },
          get: function() {
            // get the right message and do the token processing
          }
      }
      

      Or are we missing an existing way to unit test JS code that uses window.messages?

        Activity

        Hide
        icefox added a comment -

        Hi Romain,

        I think that the forum is the better place to post such thing.

        Anyway, it's true that the Jawr messages are not easy to unit test in JS.
        But Jawr allows you to define you own message generator.
        You can define your own generator which generate the message as you like.
        You can take a look to the following link to check how to create your own generator :
        http://jawr.java.net/docs/generators.html

        You can take a look to the net.jawr.web.resource.bundle.locale.ResourceBundleMessagesGenerator as an example of generator and also to
        net.jawr.web.resource.bundle.locale.message.MessageBundleScriptCreator, which generates the javascript content.

        Cheers,

        Show
        icefox added a comment - Hi Romain, I think that the forum is the better place to post such thing. Anyway, it's true that the Jawr messages are not easy to unit test in JS. But Jawr allows you to define you own message generator. You can define your own generator which generate the message as you like. You can take a look to the following link to check how to create your own generator : http://jawr.java.net/docs/generators.html You can take a look to the net.jawr.web.resource.bundle.locale.ResourceBundleMessagesGenerator as an example of generator and also to net.jawr.web.resource.bundle.locale.message.MessageBundleScriptCreator, which generates the javascript content. Cheers,

          People

          • Assignee:
            icefox
            Reporter:
            romainp
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated: