[OpenLayers-Dev] OpenLayers Internationalization

Gertjan van Oosten gertjan at West.NL
Mon Jul 16 11:21:11 EDT 2007


As quoted from "Steven M. Ottens" <steven at minst.net>:
> We use a mbGetMessage function (/lib/util/Util.js) which gets called 
> with either just the message key or together with some extra parameters:
> 
> alert(mbGetMessage("noTargetModelTool", targetModelName, toolRef.id));
> 
> corresponds with:
> <noTargetContext>Error finding targetContext: {0} for: {1}</noTargetContext>
> 
> All the strings are located in a widgetText.xml document, which is 
> located in the skin/<lang>/ folder

This goes against the "no config files for OpenLayers" dogma ;-)
although the fact that an external XML file is used is not really
crucial to the solution.  In MapBuilder, the widget texts get read into
memory (JS objects) at configuration time.  Having them straight into JS
would not make much difference.

> Christopher Schmidt wrote:
> >  * How much support do we need for interpolation in text?

Note that the function mbFormatMessage() I wrote, copied below from
trunk/lib/util/Util.js for your convenience, can be used straight
out-of-the-box to get simple interpolation.  Since at the top of the
file it clearly states that it is LGPL, nothing is stopping you. 8-)

  /**
   * Format a message with the extra arguments. <br/>
   * E.g. if called as: <code>mbFormatMessage("{1} is {0} {2}, {1}", "a good", "this", "test")</code><br/>
   * the formatted message returned is: <code>"this is a good test, this"</code>
   * @param messageFormat the message format string
   * @param varArgs       optional extra parameters for formatting the message
   * @return              the formatted message
   */
  function mbFormatMessage(messageFormat)
  {
    var message = messageFormat;
    var varArgs = [].slice.call(arguments, mbFormatMessage.length);
    for (var i in varArgs) {
      var parm = new RegExp("\\{" + i + "\\}", "g");
      message = message.replace(parm, varArgs[i]);
    }
    return message;
  }

Kind regards,
-- 
-- Gertjan van Oosten, gertjan at West.NL, West Consulting B.V., +31 15 2191 600



More information about the Dev mailing list