This recipe is something I have written about a couple times as well and is in fact why I was at GLSEC last year to talk about. This script is used as part of the LOUD technique for testing i18n and l10n within an application. This one happens to be for a Java 2 resource bundle, though the it could be easily modified for any language or format (though some are easier than others).

This script, like many that manipulate files, follows a pattern which is useful to keep in mind.

  • open file
  • deal with only one line at a time
  • do the interesting manipulation one chunk at a time
  • save the modified file

Again, the script is Python.

import os

my_class = """ public class MyResources extends ListResourceBundle {
      public Object[][] getContents() {
              return contents;
      }
      static final Object[][] contents = {
      // LOCALIZE THIS
              {"OkKey", "OK"},
              {"CancelKey", "Cancel"},
      // END OF MATERIAL TO LOCALIZE
      };
 }
"""

# normally you would open a file and iterate over it's contents
# but this is an example...
my_class_lines = my_class.split("\n")

# process each line
for line_num in range(0, len(my_class_lines) -1):
    # sure, a regex is likely the way to do this, but...
    key_sep = '", "'
    key_sep_at = my_class_lines[line_num].find(key_sep)
    end_point = '"}'
    end_point_at = my_class_lines[line_num].rfind(end_point)
    if key_sep_at is not -1:
        # break the line into chunks
        beginning = my_class_lines[line_num][0:key_sep_at + len(key_sep)]
        middle = my_class_lines[line_num][len(beginning):end_point_at]
        end = my_class_lines[line_num][end_point_at:]
        # LOUD the middle
        middle = "^%s$" % middle.upper()
        # put it back into our copy of the class in memory
        my_class_lines[line_num] = "%s%s%s" % (beginning, middle, end)

# write the file out to disk... okay, to the screen in this case
print "\n".join(my_class_lines)