Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了【Cocos2d-x】支持 i18n 国际化(2)——i18n XML 解析生成头文件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

【Cocos2d-x】支持 i18n 国际化(2)——i18n XML 解析生成头文件


转载请注明出处http://www.voidcn.com/article/p-tdcedetl-ue.html


1、概述

上一节,本节将使用 Python 脚本对 resources/i18n 文件夹下的文件进行解析,将各个字符串的 key 提取出来,生成一个头文件,供我们使用。解决的问题:

  • 自动解析 XML 生成 key 供我们使用;
  • C++ 代码中,我们直接使用 namespace 下的常量替代 Hardcode 字符串,防止 key 值变化后,需要替换源代码中所有的 key;
  • 遇到不认识的 key,直接可以在编译期发现,而不是在运行期得不到字符串了。

比如,我们有一个配置文件如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <String name="app_name">射击场</String>
    <String name="start_game">开始游戏</String>
    <String name="exit_game">结束游戏</String>
    <String name="exit_comfirm_tips">确定要退出游戏吗?</String>
    <String name="cancel">取消</String>
    <String name="exit">结束</String>
    <String name="score">得分:</String>
</resources>

我们希望在最终的 Classes/i18n.h 头文件中生成所有的 key:

//
// i18n.h
//
#pragma once

namespace res {

    namespace String {

        const char* const app_name = "app_name";
        const char* const start_game = "start_game";
        const char* const exit_game = "exit_game";
        const char* const exit_comfirm_tips = "exit_comfirm_tips";
        const char* const cancel = "cancel";
        const char* const exit = "exit";
        const char* const score = "score";
    }
}

2、使用方法

那么使用的方法就非常简单了:

auto label = Label::create();
label->setString(res::string::app_Name);
label->setTextColor(Color4b::rED);
label->setSystemFontSize(40);

// ...

layer->addChild(label);

3、Python 脚本

Python 脚本也非常简单,通过 os.listdir 列举出 resources/i81n/ 文件夹下的配置文件,将各个文件进行解析,把 key 归类去重,按 namespace 规整,写到 Classes/i18n.h 头文件中即可。

#!/usr/bin/env python
#-*-conding:utf8-*-
#
# @author arnozhang
# @date 2015.11.11
# @email zyfgood12@163.com

import sys;
import os;
import xml.dom.minidom;


I18N_PATH = './resources/i18n/'
i18n_key_list = []


def new_indent_str(indent = 1):
    return ''.join([' ' for i in range(indent * 4)])


def is_xml_node(node,Name):
    return node \
        and node.nodeType == xml.dom.Node.ELEMENT_NODE \
        and node.nodename == name


#
# namespace res {
# namespace String {
# const char* const KEY_XXX = "xxx";
# }
# }
#
def generate_i18n(filE):
    indent_str = new_indent_str()
    res_indent_str = indent_str + indent_str

    file.write('namespace res {\n\n')
    file.write(indent_str + 'namespace String {\n\n')

    list = os.listdir(I18N_PATH)
    if list:
        for path in list:
            generate_by_file(file,I18N_PATH + path,res_indent_str)

    file.write(indent_str + '}\n')
    file.write('}\n')


def generate_by_file(file,res_file_path,res_indent_str):
    dom_tree = xml.dom.minidom.parse(res_file_path)
    if not dom_tree:
        return

    #
    # <resources>
    # <String name="KEY_XXX">xxx</String>
    # </resources>
    #
    for res in dom_tree.documentElement.childNodes:
        if is_xml_node(res,'String'):
            key_name = res.getAttribute('name')
            if not (key_name in i18n_key_list):
                i18n_key_list.append(key_Name)
                write_String_res(file,key_name,res_indent_str)


def write_String_res(file,res_indent_str):
    file.write(res_indent_str)
    file.write('const char* const %s = "%s";\n' % (key_name,key_Name))


if __name__ == '__main__':
    file = open('./Classes/I18n.h',mode = 'w')
    file.write('#pragma once\n\n')

    generate_i18n(filE)

    file.close()

大佬总结

以上是大佬教程为你收集整理的【Cocos2d-x】支持 i18n 国际化(2)——i18n XML 解析生成头文件全部内容,希望文章能够帮你解决【Cocos2d-x】支持 i18n 国际化(2)——i18n XML 解析生成头文件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。