有没有工具可以将Iphone本地化的字符串文件转换为可在Android中使用的字符串资源文件?

时间:2010-06-29 13:32:44

标签: android localization port

我有Iphone应用程序中使用的本地化字符串文件,用于移植到Android。是否有任何工具通过从xcode项目中获取的文件并构建在android中使用字符串所需的xml?

由于apple文件是一个简单的键值文件,因此有一个工具可以转换此格式的字符串

  

“键”= “值”

到此:

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

这个工具应该很容易构建,但我很欣赏任何已经有效的工具。

9 个答案:

答案 0 :(得分:9)

我想你可能会喜欢这个工具:

http://members.home.nl/bas.de.reuver/files/stringsconvert.html

也是这个,但它缺少一些功能(比如在名称中转换空格):

http://localise.biz/free/converter/ios-to-android

它是免费的,无需注册,您也不需要构建自己的脚本!

答案 1 :(得分:6)

这是Unix思维模式和工具集派上用场的一个领域。我不知道iPhone格式是什么样的,但是如果它就是你所说的,每行每个值一个,那么一个简单的sed调用可以做到这一点:

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

我希望您的OSX安装上可以使用sed。

答案 2 :(得分:4)

好的,我使用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();
}

让Java正确读取和编写我从xcode项目中获得的UTF 16输入有点棘手,但现在它的工作就像一个魅力。

答案 3 :(得分:4)

我改进了上面的主要方法,为我工作:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Scanner;

public class StringsConverter {

    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\"?>\n<resources>\n");
        while (fileScanner.hasNextLine()) {
            String line = fileScanner.nextLine();
            if (line.contains("=")) {               
                String[] parts = line.split("=");

                parts[0] = parts[0].trim()
                    .replace(" ", "_")
                    .replace("\\n", "_")
                    .replace("-", "_")
                    .replace("\"", "")
                    .replace(";", "")
                    .replace("'", "")
                    .replace("/", "")
                    .replace("(", "")
                    .replace(")", "")
                    .replace("?", "_Question");

                parts[1] = parts[1].trim().substring(1, parts[1].length()-3);
                parts[1] = parts[1].replace("'", "\\'");

                String nextLine = "<string name=\"" + parts[0] + "\">" + parts[1].trim() + "</string>";
                System.out.println(nextLine);
                writer.append(nextLine + "\n");
            }
        }
        fileScanner.close();
        writer.append("</resources>");
        writer.close();
    }

}

答案 4 :(得分:3)

我用来转换Java属性文件的“工具”:

    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();

答案 5 :(得分:3)

对上述解决方案进行了少量修改:

  • Android样式的字符串名称 - 小写字母,以“_”分隔;
  • 将iOS字符串格式参数转换为java格式(%@,%@至%1 $ s,%2 $ s ...)
  • 转换评论;

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\"?>\n<resources>");
    writer.append("\n");
    while (fileScanner.hasNextLine())
    {
        String line = fileScanner.nextLine();
        if (line.contains("="))
        {
            line = line.trim();
            line = line.replace("\"", "");
            line = line.replace(";", "");
            String[] parts = line.split("=");
            String resultName = processName(parts[0]);
            String resultValue = processValue(parts[1]);
            String nextLine =
                    "<string name=\"" + resultName.toLowerCase() + "\">"
                            + resultValue + "</string>";
            System.out.println(nextLine);
            writer.append(nextLine);
            writer.append("\n");
        } else
        {

            line = line.replace("/*", "<!--");
            line = line.replace("*/", "-->");
            writer.append(line);
            writer.append("\n");
        }
    }
    fileScanner.close();
    writer.append("</resources>");
    writer.close();
}

private static String processValue(String part)
{
    String value = part.trim();
    StringBuilder resultValue = new StringBuilder();
    if (value.contains("%@"))
    {
        int formatCnt = 0;
        for (int i = 0; i < value.length(); i++)
        {
            char c = value.charAt(i);
            char next = value.length() > i + 1 ? value.charAt(i + 1) : '\0';
            if (c == '%' && next == '@')
            {
                formatCnt++;
                resultValue.append('%');
                resultValue.append(formatCnt);
                resultValue.append("$s");
                i++;
            } else
            {
                resultValue.append(value.charAt(i));
            }
        }
    }   else{
        resultValue.append(value);
    }
    return resultValue.toString();
}

private static String processName(String part)
{
    String name = part.trim();
    name = name.replace(" ", "_");
    name = name.replace("-", "_");
    name = name.replace("\n", "_");
    name = name.replaceAll("[^A-Za-z0-9 _]", "");
    if (Character.isDigit(name.charAt(0)))
    {
        name = "_" + name;
    }
    StringBuilder resultName = new StringBuilder();
    for (int i = 0; i < name.length(); i++)
    {
        char c = name.charAt(i);
        if (Character.isUpperCase(c))
        {
            char prev = i > 0 ? name.charAt(i - 1) : '\0';
            if (prev != '_' && !Character.isUpperCase(prev) && prev != '\0')
            {
                resultName.append('_');
            }
            resultName.append(Character.toLowerCase(c));


        } else
        {
            resultName.append(Character.toLowerCase(c));
        }
    }
    return resultName.toString();
}

答案 6 :(得分:0)

这并不能解答您的问题,但您可以考虑将DMLocalizedString用于未来的iOS + Android项目。目前该源仅适用于iOS,但我相信制作Android版本非常简单。

答案 7 :(得分:0)

要转换为 Android ,请使用我的正则表达式代码。您可以在线测试(复制替换字符串):

http://www.phpliveregex.com/p/6dA

使用的正则表达式为^"(.*)"\s=\s"(.*)";$/m,替换字符串为<string name="$1">$2</string>

要转换为 iOS ,请使用我的正则表达式代码(复制替换字符串):

http://www.phpliveregex.com/p/6dC

使用的正则表达式为^\<string name=\"(.*)\"\>(.*)\<\/string\>/m,替换字符串为"$1" = "$2";

答案 8 :(得分:0)

基于Web的在线转换器(两种方式): https://gunhansancar.com/tools/converter/