Exemplo n.º 1
0
def main():
    lang = 'zh'
    if len(sys.argv) == 2:
        lang = sys.argv[1]

    cd = sys.path[0]
    translation_path = os.path.join(cd, '../translation')

    # load lua
    pregame_file = os.path.join(translation_path, 'en_pregame.lua')
    client_file = os.path.join(translation_path, 'en_client.lua')

    ui_mgr = UiMgr()
    log.debug('loading lua file %s' % pregame_file)
    ui_mgr.load_lua_file(pregame_file)
    log.debug('loading lua file %s' % client_file)
    ui_mgr.load_lua_file(client_file)
    log.info('read %d lines.' % len(ui_mgr.ui_lines))

    # save merged lines
    translate_file = os.path.join(translation_path, '%s_translate.txt' % lang)
    if os.path.exists(translate_file):
        choose = input('%s_translate.txt file exists, merge? [y/N]' % lang)
        choose = choose.lower().strip()
        if choose != '' and choose[0] == 'y':
            log.info('merging to translate file.')
            ui_mgr.apply_translate_from_txt_file(translate_file)
        else:
            log.info('skipped.')
            return

    with open(translate_file, 'wt', encoding='utf-8') as fp:
        fp.writelines(ui_mgr.get_txt_lines(replace=True))
        log.info('save translate file succeed.')
Exemplo n.º 2
0
def main():
    lang = 'zh'

    if len(sys.argv) != 2:
        usage()
        sys.exit(2)

    xls_path = sys.argv[1]

    cd = sys.path[0]
    translation_path = os.path.join(cd, '../translation')
    translate_file = os.path.join(translation_path, '%s_translate.txt' % lang)

    ui_mgr = UiMgr()

    # load lua
    pregame_src = os.path.join(translation_path, 'en_pregame.lua')
    ui_mgr.load_lua_file(pregame_src)

    client_src = os.path.join(translation_path, 'en_client.lua')
    ui_mgr.load_lua_file(client_src)

    # load translation
    data_from_xls = load_xls(xls_path)
    ui_mgr.apply_translate_from_xls(data_from_xls, need_check=False)

    # save result
    txt_lines = ui_mgr.get_txt_lines(replace=True)
    with open(translate_file, 'wt', encoding='utf-8') as fp:
        fp.writelines(txt_lines)
        log.info('save translate file succeed.')
Exemplo n.º 3
0
def main():
    lang = 'zh'

    if len(sys.argv) != 2:
        usage()
        sys.exit(2)

    xls_path = sys.argv[1]

    cd = sys.path[0]
    translation_path = os.path.join(cd, '../translation')
    translate_file = os.path.join(translation_path, '%s_translate.txt' % lang)

    ui_mgr = UiMgr()

    # load lua
    pregame_src = os.path.join(translation_path, 'en_pregame.lua')
    ui_mgr.load_lua_file(pregame_src)

    client_src = os.path.join(translation_path, 'en_client.lua')
    ui_mgr.load_lua_file(client_src)

    # load translation
    data_from_xls = load_xls(xls_path)
    ui_mgr.apply_translate_from_xls(data_from_xls, need_check=False)

    # save result
    txt_lines = ui_mgr.get_txt_lines(replace=True)
    with open(translate_file, 'wt', encoding='utf-8') as fp:
        fp.writelines(txt_lines)
        log.info('save translate file succeed.')
Exemplo n.º 4
0
class TestUiMgr(unittest.TestCase):
    def setUp(self):
        self.lua = [
            'SafeAddString(SI_ABILITYUPGRADELEVEL0, "None", 0)',
            '无',
            'SafeAddString(SI_ABILITYUPGRADELEVEL1, "Bronze", 0)\n',
            '青铜',
            'SafeAddString(SI_ABILITYUPGRADELEVEL2, "Silver", 0)\n',
            'SafeAddString(SI_ABILITYUPGRADELEVEL3, "Gold", 0)\n',
            '黄金\n',
        ]
        self.mgr = UiMgr()
        self.mgr.load_lua_lines(self.lua)

    def test_load_lua(self):
        self.assertEqual(4, len(self.mgr.ui_lines))
        self.assertEqual('None',
                         self.mgr.ui_lines['SI_ABILITYUPGRADELEVEL0'].origin)
        self.assertEqual(
            '', self.mgr.ui_lines['SI_ABILITYUPGRADELEVEL0'].translation)

    def test_apply_translation_txt(self):
        """测试从 .txt 文本文件中读入的翻译"""
        self.mgr.apply_translate_from_txt_lines(self.lua)

        self.assertEqual(4, len(self.mgr.ui_lines))
        self.assertEqual('None',
                         self.mgr.ui_lines['SI_ABILITYUPGRADELEVEL0'].origin)
        self.assertEqual(
            '无', self.mgr.ui_lines['SI_ABILITYUPGRADELEVEL0'].translation)
        self.assertEqual(
            '', self.mgr.ui_lines['SI_ABILITYUPGRADELEVEL2'].translation)
        self.assertEqual(
            '黄金', self.mgr.ui_lines['SI_ABILITYUPGRADELEVEL3'].translation)

    def test_apply_unknown_txt(self):
        """测试翻译 .txt 时的异常处理"""
        lua = [
            'SafeAddString(SI_ABILITYUPGRADELEVEL0, "Fake None", 0)',
            '无',
            'SafeAddString(SI_ABILITY_ACTION_CLEAR_SLOT, "Remove", 1)',
            '移除',
        ]
        self.mgr.load_lua_lines(self.lua)
        self.mgr.apply_translate_from_txt_lines(lua)

        self.assertEqual(
            '', self.mgr.ui_lines['SI_ABILITYUPGRADELEVEL0'].translation)
        self.assertFalse(
            'SI_ABILITY_ACTION_CLEAR_SLOT' in self.mgr.ui_lines.keys())

    def test_apply_translation_xls(self):
        """测试应用从 xls 读入的数据"""
        data = [
            ['1', 'SI_ABILITYUPGRADELEVEL0', 'None', '无', '', '', '', ''],
            ['2', 'SI_ABILITYUPGRADELEVEL1', 'None', '无', '', '', '', ''],
            ['3', 'SI_ABILITYUPGRADELE__FAKE', 'None', '无', '', '', '', ''],
        ]
        self.mgr.apply_translate_from_xls(data)

        self.assertEqual('None',
                         self.mgr.ui_lines['SI_ABILITYUPGRADELEVEL0'].origin)
        self.assertEqual(
            '无', self.mgr.ui_lines['SI_ABILITYUPGRADELEVEL0'].translation)
        self.assertEqual(
            '', self.mgr.ui_lines['SI_ABILITYUPGRADELEVEL1'].translation)
        self.assertFalse(
            'SI_ABILITYUPGRADELE__FAKE' in self.mgr.ui_lines.keys())

    def test_get_rows(self):
        """测试生成 xls 数据的能力"""
        rows = self.mgr.get_rows()
        rows_list = [ui_row.to_list() for name, ui_row in sorted(rows.items())]
        expected_rows_list = [
            ['1', 'SI_ABILITYUPGRADELEVEL0', 'None', '', '', '', '', ''],
            ['2', 'SI_ABILITYUPGRADELEVEL1', 'Bronze', '', '', '', '', ''],
            ['3', 'SI_ABILITYUPGRADELEVEL2', 'Silver', '', '', '', '', ''],
            ['4', 'SI_ABILITYUPGRADELEVEL3', 'Gold', '', '', '', '', ''],
        ]
        self.assertEqual(expected_rows_list, rows_list)

        self.mgr.apply_one_translate('SI_ABILITYUPGRADELEVEL0', 'None', '无')
        expected_rows_list[0][3] = '无'
        rows = self.mgr.get_rows()
        rows_list = [ui_row.to_list() for name, ui_row in sorted(rows.items())]
        self.assertEqual(expected_rows_list, rows_list)

    def test_get_txt_lines(self):
        """测试生成 .txt 的能力"""
        lines = self.mgr.get_txt_lines()
        expected_lines = [
            'SafeAddString(SI_ABILITYUPGRADELEVEL0, "None", 0)\n',
            'SafeAddString(SI_ABILITYUPGRADELEVEL1, "Bronze", 0)\n',
            'SafeAddString(SI_ABILITYUPGRADELEVEL2, "Silver", 0)\n',
            'SafeAddString(SI_ABILITYUPGRADELEVEL3, "Gold", 0)\n',
        ]
        self.assertEqual(expected_lines, lines)

        self.mgr.apply_one_translate('SI_ABILITYUPGRADELEVEL0', 'None', '无')
        lines = self.mgr.get_txt_lines()
        expected_lines[1:1] = [
            '无\n',
        ]
        self.assertEqual(expected_lines, lines)

    def test_get_str_lines(self):
        """测试生成 .str 的能力"""
        lines = self.mgr.get_str_lines()
        expected_lines = [
            '[SI_ABILITYUPGRADELEVEL0] = "None"\n',
            '[SI_ABILITYUPGRADELEVEL1] = "Bronze"\n',
            '[SI_ABILITYUPGRADELEVEL2] = "Silver"\n',
            '[SI_ABILITYUPGRADELEVEL3] = "Gold"\n',
        ]
        self.assertEqual(expected_lines, lines)

        self.mgr.apply_one_translate('SI_ABILITYUPGRADELEVEL2', 'Silver', '白银')

        lines = self.mgr.get_str_lines(mode='origin')
        self.assertEqual(expected_lines, lines)
        lines = self.mgr.get_str_lines(mode='translation')
        expected_lines[2] = '[SI_ABILITYUPGRADELEVEL2] = "白银"\n'
        self.assertEqual(expected_lines, lines)
        lines = self.mgr.get_str_lines()
        expected_lines[2] = '[SI_ABILITYUPGRADELEVEL2] = "Silver 白银"\n'
        self.assertEqual(expected_lines, lines)