forked from FurryGamesIndex/games
-
Notifications
You must be signed in to change notification settings - Fork 4
/
zhconv.py
executable file
·117 lines (96 loc) · 4.42 KB
/
zhconv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2020 Utopic Panther
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
import os
import argparse
import yaml
from opencc import OpenCC
parser = argparse.ArgumentParser()
parser.add_argument('--no-builtin', default=False, action='store_true', help='Do not run builtin path conv')
parser.add_argument('extra_pathes', type=str, default=[], help='add extra path', nargs="*")
args = parser.parse_args()
root_dir = "."
games_dir = os.path.join("games", "l10n")
uil10n_dir = "uil10n"
doc_dir = "doc"
converter = OpenCC('s2twp.json')
origin = "zh-cn"
to = "zh-tw"
convertert2s = OpenCC('tw2sp.json')
def conv(of, tf, fixlinks = False, conv=converter):
print("Converting %s, fixlinks=%r" % (of, fixlinks))
with open(of) as stream:
data = conv.convert(stream.read())
if conv is not converter and fixlinks:
raise NotImplementedError("fix links is not implemented for non-default converter")
if fixlinks:
data = data.replace("." + origin + ".", "." + to + ".")
data = data.replace("/" + origin + "/", "/" + to + "/")
with open(tf, 'w') as stream:
stream.write(data)
def __convgamesl10n(cn, tw, f, fn):
cnf = os.path.join(cn, f)
twf = os.path.join(tw, f)
with open(fn) as stream:
data = yaml.safe_load(stream)
if "X-Chinese-Convertor-Hint" in data:
data = data["X-Chinese-Convertor-Hint"]
if "prefer" in data:
if data["prefer"] == "TW":
conv(twf, cnf, conv=convertert2s)
elif data["prefer"] == "CN":
conv(cnf, twf)
elif data["prefer"] == "ignored":
pass
else:
raise ValueError("unsupported cchint prefer %s" % prefer)
else:
conv(cnf, twf)
def convgamesl10n(cn, tw):
cnfiles = [i for i in os.listdir(cn) if os.path.isfile(os.path.join(cn, i)) and i[0] != '.']
for f in cnfiles:
fn = os.path.join(cn, f)
__convgamesl10n(cn, tw, f, fn)
twfiles = [i for i in os.listdir(tw) \
if os.path.isfile(os.path.join(tw, i)) and \
i[0] != '.' and \
i not in cnfiles]
for f in twfiles:
print("New TW file: %s" % f)
fn = os.path.join(tw, f)
__convgamesl10n(cn, tw, f, fn)
if not args.no_builtin:
#deprecated, use fgi/plugins/zhconv.py instead.
#convgamesl10n(os.path.join(games_dir, origin), os.path.join(games_dir, to))
conv(os.path.join(uil10n_dir, origin + ".yaml"), os.path.join(uil10n_dir, to + ".yaml"), True)
conv(os.path.join(root_dir, "README.%s.md" % origin), os.path.join(root_dir, "README.%s.md" % to), True)
conv(os.path.join(doc_dir, "Get-Involved.%s.md" % origin), os.path.join(doc_dir, "Get-Involved.%s.md" % to), True)
conv(os.path.join(doc_dir, "FGI-members.%s.md" % origin), os.path.join(doc_dir, "FGI-members.%s.md" % to), True)
conv(os.path.join(doc_dir, "Contribute.%s.md" % origin), os.path.join(doc_dir, "Contribute.%s.md" % to), True)
conv(os.path.join(doc_dir, "contribute_guide", "patches-submitting.%s.md" % origin), os.path.join(doc_dir, "contribute_guide", "patches-submitting.%s.md" % to), True)
conv(os.path.join(doc_dir, "contribute_guide", "zhconv.%s.md" % origin), os.path.join(doc_dir, "contribute_guide", "zhconv.%s.md" % to), False)
conv(os.path.join(doc_dir, "contribute_guide", "game.%s.md" % origin), os.path.join(doc_dir, "contribute_guide", "game.%s.md" % to), True)
conv(os.path.join(doc_dir, "faq.%s.md" % origin), os.path.join(doc_dir, "faq.%s.md" % to), True)
conv(os.path.join(doc_dir, "search_help.%s.md" % origin), os.path.join(doc_dir, "search_help.%s.md" % to), True)
for i in args.extra_pathes:
f = i.split(":")[0]
t = i.split(":")[1]
if os.path.isfile(f):
conv(f, t)
else:
raise ValueError("file not exists: %s" % f)