views:

32

answers:

4

I have the localized strings file that is used in the Iphone app that I work on to port to Android. Are there any tools that go through the file taken from the xcode project and build the xml needed to use the strings in android?

As the apple file is a simple key value file is there a tool that converts string in this format

"key"="value"

to this:

<string name="key"> value </string>

This tool should be easy to build but I appreciate any pointers to already working tools.

A: 

you should be able to accomplish this with some XSLT magic...

Ben
That "magic" would only apply in the opposite problem (from Android XML files, to flat iPhone text files). XSLT can only convert from XML to XML/TEXT/HTML. He is out of luck this time.
Fernando Miguélez
+1  A: 

This is one of the areas where the Unix mindset and toolset comes in handy. I don't know what the iPhone format is like, but if it's what you say, with each value one per line, a simple sed call could do this:

$ cat infile
"key"="value"
"key2"="value2"
$ sed 's/ *"\([^"]*\)" *= *"\([^"]*\)"/<string name="\1">\2<\/string>/' infile
<string name="key">value</string>
<string name="key2">value2</string>
$

I expect sed is available on your OSX install.

ydant
+1  A: 

The "tool" I've used to convert a Java properties file:

    BufferedReader br = new BufferedReader(new InputStreamReader(
            new FileInputStream("c:/messages_en.properties"), "utf-8"));
    String line = null;
    while ((line = br.readLine()) != null) {
        line = line.trim();
        if (line.length() > 0) {
            String[] parts = line.split(" = ");
            System.out.println("<string name=\"" + parts[0] + "\">"
                    + parts[1] + "</string>");
        }
    }
    br.close();
alex
+1  A: 

Ok i wrote my own little converter using a little bit from the code from alex.

public static void main(String[] args) throws IOException {
    Scanner fileScanner =
            new Scanner(new FileInputStream(args[0]), "utf-16");
    Writer writer =
            new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
                    new File(args[1])), "UTF8"));
    writer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?> <resources>");
    while (fileScanner.hasNextLine()) {
        String line = fileScanner.nextLine();
        if (line.contains("=")) {
            line = line.trim();
            line = line.replace("\"", "");
            line = line.replace(";", "");
            String[] parts = line.split("=");
            String nextLine =
                    "<string name=\"" + parts[0].trim() + "\">"
                            + parts[1].trim() + "</string>";
            System.out.println(nextLine);
            writer.append(nextLine);
        }
    }
    fileScanner.close();
    writer.append("</resources>");
    writer.close();
}

It was a little bit tricky to get Java to correctly read and write the UTF 16 input I got out of the xcode project but now it is working like a charm.

Janusz
It works, but I strongly encourage learning the command line tools you have available to you. In my opinion, things like this are so much easier if you can write one or two lines on the command line rather than making a new Java program for every itch you need to scratch.
ydant
It depends on what I want to do and which language I'm fastest in. I thought about doing it in python because it is task suitable for a nice scripting language but I was faster doing it in Java at the moment.
Janusz