advice on localizing specific attributes from XML file

  • I"m working on a new version of my Blocks plugin framework. This
    version will allow for a lot more configuration info to be declared
    in the plugin.xml configuration files. And some of that information
    will need to be localized. For instance here's some markup that
    defines a main menu:

    <menu id="com.blocks.BUserInterface.menus.main">
      <menuitem title="" id="application"
    submenu="com.blocks.BUserInterface.menus.main.application" />
      <menuitem title="File" id="file"
    submenu="com.blocks.BUserInterface.menus.main.file" />
      <menuitem title="Edit" id="edit"
    submenu="com.blocks.BUserInterface.menus.main.edit" />
      <menuitem title="Window" id="window"
    submenu="com.blocks.BUserInterface.menus.main.window" />
      <menuitem title="Help" id="help"
    submenu="com.blocks.BUserInterface.menus.main.help" />
    </menu>

    And in this case the File, Edit, Window, and Help titles all need to
    be localized. If I were working in a .m file I would just use
    NSLocalizedString, but I'm not sure what the best way to handle
    string localization in an XML file is.

    Basically I want something that will work like NSLocalizedString(),
    but has friendlier XML syntax. I'm thinking that I might just append
    attribute strings that should be localized with a '%'. So the above
    would turn into:

    <menu id="com.blocks.BUserInterface.menus.main">
      <menuitem title="" id="application"
    submenu="com.blocks.BUserInterface.menus.main.application" />
      <menuitem title="%File" id="file"
    submenu="com.blocks.BUserInterface.menus.main.file" />
      <menuitem title="%Edit" id="edit"
    submenu="com.blocks.BUserInterface.menus.main.edit" />
      <menuitem title="%Window" id="window"
    submenu="com.blocks.BUserInterface.menus.main.window" />
      <menuitem title="%Help" id="help"
    submenu="com.blocks.BUserInterface.menus.main.help" />
    </menu>

    That would make the strings easy to detect when I read them into my
    program, and then I would call [NSBundle localizedStringForKey] on
    the read in values to get the localized values. This seems like it
    would work, but it also means that I'll need to duplicate the
    functionality of genstrings to recognize the new markup that I'm
    using. Not to bad, but a little pain.

    Anyway please let me know if this approach seems to make sense, or if
    there are better solutions to the problem that I don't know about. My
    end goal is to make the translation as strait forward and OS X like
    as possible. I think that means that the translation probably
    shouldn't have to know about the XML at all, so just localizing the
    XML files doesn't seem to make sense.

    Thanks,
    Jesse
  • Another approach would be to use DTD entities. This is how Mozilla
    localizes XUL (XML) files.

    The Blocks XML file would look something like this:

          <menu id="com.blocks.BUserInterface.menus.main">
                  <menuitem title="" id="application"
    submenu="com.blocks.BUserInterface.menus.main.application" />
                  <menuitem title="&menuFile.label;" id="file"
    submenu="com.blocks.BUserInterface.menus.main.file" />
                  <menuitem title="&menuEdit.label;" id="edit"
    submenu="com.blocks.BUserInterface.menus.main.edit" />
                  <menuitem title="&menuWindow.label;" id="window"
    submenu="com.blocks.BUserInterface.menus.main.window" />
                  <menuitem title="&menuHelp.label;" id="help"
    submenu="com.blocks.BUserInterface.menus.main.help" />
          </menu>

    And the entity definitions in the English DTD would look like this:

    <!ENTITY menuFile.label "File">
    <!ENTITY menuEdit.label "Edit">
    <!ENTITY menuWindow.label "Window">
    <!ENTITY menuHelp.label "Help">

    For more information, check out the following:

    http://books.mozdev.org/html/mozilla-chp-11.html
    http://books.mozdev.org/html/mozilla-chp-11-sect-2.html#mozilla-CHP-11-SECT
    -2.1


    -Steve

    On 9/6/07, Jesse Grosjean <jesse...> wrote:
    > I"m working on a new version of my Blocks plugin framework. This
    > version will allow for a lot more configuration info to be declared
    > in the plugin.xml configuration files. And some of that information
    > will need to be localized. For instance here's some markup that
    > defines a main menu:
    >
    > <menu id="com.blocks.BUserInterface.menus.main">
    > <menuitem title="" id="application"
    > submenu="com.blocks.BUserInterface.menus.main.application" />
    > <menuitem title="File" id="file"
    > submenu="com.blocks.BUserInterface.menus.main.file" />
    > <menuitem title="Edit" id="edit"
    > submenu="com.blocks.BUserInterface.menus.main.edit" />
    > <menuitem title="Window" id="window"
    > submenu="com.blocks.BUserInterface.menus.main.window" />
    > <menuitem title="Help" id="help"
    > submenu="com.blocks.BUserInterface.menus.main.help" />
    > </menu>
    >
    > And in this case the File, Edit, Window, and Help titles all need to
    > be localized. If I were working in a .m file I would just use
    > NSLocalizedString, but I'm not sure what the best way to handle
    > string localization in an XML file is.
    >
    > Basically I want something that will work like NSLocalizedString(),
    > but has friendlier XML syntax. I'm thinking that I might just append
    > attribute strings that should be localized with a '%'. So the above
    > would turn into:
    >
    > <menu id="com.blocks.BUserInterface.menus.main">
    > <menuitem title="" id="application"
    > submenu="com.blocks.BUserInterface.menus.main.application" />
    > <menuitem title="%File" id="file"
    > submenu="com.blocks.BUserInterface.menus.main.file" />
    > <menuitem title="%Edit" id="edit"
    > submenu="com.blocks.BUserInterface.menus.main.edit" />
    > <menuitem title="%Window" id="window"
    > submenu="com.blocks.BUserInterface.menus.main.window" />
    > <menuitem title="%Help" id="help"
    > submenu="com.blocks.BUserInterface.menus.main.help" />
    > </menu>
    >
    > That would make the strings easy to detect when I read them into my
    > program, and then I would call [NSBundle localizedStringForKey] on
    > the read in values to get the localized values. This seems like it
    > would work, but it also means that I'll need to duplicate the
    > functionality of genstrings to recognize the new markup that I'm
    > using. Not to bad, but a little pain.
    >
    > Anyway please let me know if this approach seems to make sense, or if
    > there are better solutions to the problem that I don't know about. My
    > end goal is to make the translation as strait forward and OS X like
    > as possible. I think that means that the translation probably
    > shouldn't have to know about the XML at all, so just localizing the
    > XML files doesn't seem to make sense.
    >
    > Thanks,
    > Jesse
    >
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today