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

问题描述

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

Eclipse Java 代码以使用语言文件

enter image description here

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,JOptionPane.ERROR_MESSAGE);
    }
}

Android Studio 使用语言文件代码

public final String getString(@StringRes int resId) 从应用程序包的认字符串表中返回一个本地化的字符串。

enter image description here

解决方法

以下是我如何为其他有此问题的人做到这一点:

示例 Android Studio 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
        // http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
        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();
    }
}