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

发布于 2024-09-07 11:02:25 字数 1539 浏览 7 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

失眠症患者 2024-09-14 11:02:26

对上述解决方案添加了一些修改:

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

Added few modification to above solutions:

  • Android styled string names - lowercase worlds, separated by "_";
  • Convert iOS string format arguments to java format (%@, %@ to %1$s, %2$s ...)
  • Convert comments;

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();
}
兮子 2024-09-14 11:02:26

这并不能真正回答您的问题,但您可以考虑在未来的 iOS+Android 项目中使用 DMLocalizedString。目前源代码仅适用于 iOS,但我相信制作 Android 版本非常容易。

This don't really answer your question, but you may consider DMLocalizedString for your future iOS+Android project. Currently the source is for iOS only, but I believe that making the Android version is pretty easy.

静谧幽蓝 2024-09-14 11:02:26

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

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

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

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

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

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

For converting to Android, use my regex code. You can test it online (copy the replacement string):

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

regex used is ^"(.*)"\s=\s"(.*)";$/m , replacement string is <string name="$1">$2</string>

For converting to iOS, use my regex code (copy the replacement string):

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

regex used is ^\<string name=\"(.*)\"\>(.*)\<\/string\>/m , replacement string is "$1" = "$2";

风蛊 2024-09-14 11:02:26

基于网络的在线转换器(双向):
https://gunhansancar.com/tools/converter/

Web based online converter (both ways):
https://gunhansancar.com/tools/converter/

迷路的信 2024-09-14 11:02:25

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

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

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

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

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

I think you might like this tool :

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

Also this one, but it lacks some features (like converting whitespace in name) :

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

It is free, no need of registration, and you won't need to build your own script!

苏别ゝ 2024-09-14 11:02:25

这是 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>
$

我希望 sed 在你的 OSX 安装上可用。

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.

小矜持 2024-09-14 11:02:25

好吧,我使用 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 输入有点棘手,但现在它工作得非常顺利。

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.

黯然#的苍凉 2024-09-14 11:02:25

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

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

}

I improved your main method above to work for me:

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

}
相思故 2024-09-14 11:02:25

我用来转换 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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文