将 Android Studio strings.xml 转换为 Eclipse MessagesBundle_en_US.properties

Convert Android Studio strings.xml to Eclipse MessagesBundle_en_US.properties

我有一个 Java 游戏,它有一个 (Android Studio 应用程序客户端) 和 (Eclipse pc 客户端)。我正在从事语言翻译工作。我通过 Eclipse 设置了 ResourceBundles.properties,并为 Android Studio 设置了 strings.xml。我喜欢 Android Studio Translations Editor 的 UI,但需要一种方法将其转换为资源包或使用 strings.xml 文件。是否有一种简单的方法来转换或加载 Android Studio 输出以便我可以管理一个已翻译字符串的主列表?任何允许我导出到 .properties 和 .xml 的基于 Web 的免费方法也可以。

Eclipse Java 使用语言文件的代码

public static void updateLocale() {
    Locale currentLocale = new Locale(language, country);
    try{
        File file = new File("res/gamedata");
        URL[] urls = {file.toURI().toURL()};
        ClassLoader loader = new URLClassLoader(urls);
        messages = ResourceBundle.getBundle("MessagesBundle", currentLocale, loader);
        //messages = ResourceBundle.getBundle("res/gamedata/MessagesBundle", currentLocale); //old way - needs to be in classpath
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                theDesktop.repaint(); //TODO:: test this when changing languages
            }
        });
    } catch (MissingResourceException e){
        KisnardOnline.LOGGER.error("Language files missing", e);
        //hardcoded values if you can't open the String lookups
        JOptionPane.showMessageDialog(theDesktop, "Language files missing. Please reload game.", "Error", JOptionPane.ERROR_MESSAGE);
    } catch (MalformedURLException e) {
        KisnardOnline.LOGGER.error("Language files missing", e);
        JOptionPane.showMessageDialog(theDesktop, "Language files missing. Please reload game.", "Error", JOptionPane.ERROR_MESSAGE);
    }
}

Android 使用语言文件的工作室代码

public final String getString(@StringRes int resId) Returns 来自应用程序包默认字符串的本地化字符串 table。

以下是我如何为有这个问题的其他人做这件事的:

示例 Android 工作室 strings.xml:

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <string name="app_name" translatable="false">Kisnard Online</string>
    <string name="drawable_banner_stacked_kisnard_online_description" translatable="false">Kisnard Online Banner</string>
    <string name="general_file_locks">"Can't open or access necessary game files.  Please ensure you are not modifying any game files."</string>
    <string name="general_success">Success</string>
    <string name="general_confirm">Confirm</string>
    <string name="general_error">Error</string>
</resources>

Java 从这个语法中读取的代码:

public static void loadStringsXML() {
    loadStringXML("res/gamedata/strings_de.xml", de_DE_Messages);
    loadStringXML("res/gamedata/strings.xml", en_US_Messages);
    loadStringXML("res/gamedata/strings_es.xml", es_ES_Messages);
    loadStringXML("res/gamedata/strings_fr.xml", fr_FR_Messages);
    loadStringXML("res/gamedata/strings_it.xml", it_IT_Messages);
    loadStringXML("res/gamedata/strings_ko.xml", ko_KR_Messages);
}

public static void loadStringXML(String filename, HashMap<String, String> hashMap) {
    // Instantiate the Factory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        // optional, but recommended
        // process XML securely, avoid attacks like XML External Entities (XXE)
        dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);

        // parse XML file
        DocumentBuilder db = dbf.newDocumentBuilder();

        Document doc = db.parse(new File(filename));

        // optional, but recommended
        // 
        doc.getDocumentElement().normalize();

        NodeList list = doc.getElementsByTagName("string");
        for (int temp = 0; temp < list.getLength(); temp++) {
            Node node = list.item(temp);

            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node;

                String name = element.getAttribute("name");
                String value = node.getTextContent();
                value = value.replace("\'", "'"); //Android string file requires single quotes to be escaped
                //System.out.println(name + "=" + value);
                hashMap.put(name, value);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}