开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 8 Fork 3

dwing/sharecode

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
main
分支 (1)
main
main
分支 (1)
main
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
main
分支 (1)
main
sharecode
/
java
/
src
/
WeblateTrans.java
sharecode
/
java
/
src
/
WeblateTrans.java
WeblateTrans.java 8.78 KB
一键复制 编辑 原始数据 按行查看 历史
dwing 提交于 2025年12月10日 11:00 +08:00 . java: fix WeblateTrans
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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.HashMap;
import jason.JsonReader;
import jason.JsonWriter;
/* arg.json:
{
project: "******"
component: "******"
language: "zh_Hans"
auth: "Token wlu_************************************"
page: 1
overwrite: false
dicFiles: [
"******.txt"
"******.txt"
]
}
*/
public final class WeblateTrans {
static final class Arg {
String project;
String component;
String language; // "zh_Hans"
String auth; // "Token wlu_......"
int page; // base-1
boolean overwrite; // 覆盖已存在的译文
final ArrayList<String> dicFiles = new ArrayList<>();
}
static final class Units {
static String urlFmt = "https://hosted.weblate.org/api/translations/%s/%s/%s/units/"; // project, component, language
static String urlPageFmt = "https://hosted.weblate.org/api/translations/%s/%s/%s/units/?page=%d"; // project, component, language, page
int count;
String next;
// String previous;
final ArrayList<Unit> results = new ArrayList<>();
}
static final class Unit {
// static String urlFmt = "https://hosted.weblate.org/api/units/%d/"; // id
long id;
String context; // key
final ArrayList<String> source = new ArrayList<>(); // 通常只有1个
final ArrayList<String> target = new ArrayList<>(); // 通常只有1个,空字符串表示未翻译
String url; // "https://hosted.weblate.org/api/units/%d/"
int state; // 单元状态,0——未翻译、10——需要编辑、20——已翻译、30——已核准、100——只读
// final ArrayList<String> labels = new ArrayList<>(); // 译文单元标签,在原文单元上可用
// String translation; // "https://hosted.weblate.org/api/translations/%s/%s/%s/", project, component, language
// String language_code; // "zh_Hans"
// String explanation; // 字符串的解释,可在源单元获得
// String flags; // 翻译单元的标记
// String extra_flags; // 另外的字符串标记,可在源单元获得
// int position; // 单元在翻译文件中的位次. base-1
// boolean translated; // 单元是否被翻译
// boolean approved; // 译文是否已核准
}
static final class UnitMod {
int state; // 单元状态,0——未翻译、10——需要编辑、20——已翻译、30——已核准(需启用审校流程)
final ArrayList<String> target = new ArrayList<>(); // 目标字符串. 通常只有1个,空字符串表示未翻译
// String explanation; // 字符串的解释,可在源单元获得
// String extra_flags; // 另外的字符串标记,可在源单元获得
// final ArrayList<String> labels = new ArrayList<>(); // 标签,在源单元上可用
}
record KV(String k, String v) {
}
static final long INTERVAL_MS = 3_000;
static long lastMs;
static void delay() throws InterruptedException {
var curMs = System.nanoTime() / 1_000_000;
final var delayMs = INTERVAL_MS - (curMs - lastMs);
if (delayMs > 0) {
System.out.format("(wait %s ms) ... ", delayMs);
Thread.sleep(delayMs);
curMs = System.nanoTime() / 1_000_000;
}
lastMs = curMs;
}
static void parseDicFile(final HashMap<String, KV> dicMap, final String dicFile) throws IOException {
String key = null, k = null, v = null;
final var sb = new StringBuilder();
for (var line : Files.readAllLines(Path.of(dicFile), StandardCharsets.UTF_8)) {
if (key == null) {
if (!line.isEmpty() && line.charAt(0) == '>') {
key = line.substring(1).trim();
final int p = key.indexOf(' ');
if (p >= 0)
key = key.substring(p + 1) + '.' + key.substring(0, p);
}
} else {
int p = line.indexOf("\"\"\"");
if (sb.isEmpty()) {
if (line.isEmpty())
key = k = null;
else if (line.charAt(0) != ';') {
if (p != 0)
v = line;
else {
p = line.indexOf("\"\"\"", 3);
if (p >= 0)
v = line.substring(3, p);
else
sb.append(line, 3, line.length()).append('\n');
}
}
} else if (p < 0)
sb.append(line).append('\n');
else {
sb.append(line, 0, p);
v = sb.toString();
sb.setLength(0);
}
}
if (v != null) {
if (k == null)
k = v;
else {
if (!k.equals(v))
dicMap.put(key, new KV(k, v));
key = k = null;
}
v = null;
}
}
}
static void main(final String[] args) throws Exception {
if (args.length != 1) {
System.out.println("USAGE: java -cp jason.jar;. WeblateTrans <arg.json>");
return;
}
final var arg = JsonReader.local().buf(Files.readAllBytes(Path.of(args[0]))).parse(Arg.class);
assert arg != null;
if (arg.page < 1)
arg.page = 1;
final var dicMap = new HashMap<String, KV>();
for (final var dicFile : arg.dicFiles)
parseDicFile(dicMap, dicFile);
System.out.format(" INFO: loaded %d translations\n", dicMap.size());
try (final var hc = HttpClient.newHttpClient()) {
var url = arg.page > 1
? String.format(Units.urlPageFmt, arg.project, arg.component, arg.language, arg.page)
: String.format(Units.urlFmt, arg.project, arg.component, arg.language);
final var unitMod = new UnitMod();
unitMod.state = 20;
unitMod.target.add("");
for (; ; arg.page++) {
System.out.format(" INFO: units: %s ... ", url);
delay();
var req = HttpRequest.newBuilder().uri(URI.create(url)).setHeader("Authorization", arg.auth).GET().build();
var res = hc.send(req, HttpResponse.BodyHandlers.ofByteArray());
if (res.statusCode() != 200) {
System.out.format("ERROR: HTTP Request Failed(%d): %s\n", res.statusCode(), req.uri());
break;
}
final var resBody = res.body();
Files.write(Path.of(String.format("units.%s.%s.%s.%d.json", arg.project, arg.component, arg.language, arg.page)),
resBody, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
final var units = JsonReader.local().buf(resBody).parse(Units.class);
assert units != null;
System.out.format("OK (%d units, %d ms)\n", units.count, System.nanoTime() / 1_000_000 - lastMs);
for (final var unit : units.results) {
if (unit.source.size() != 1) {
System.out.format("ERROR: unit: id=%d key=%s state=%d res=sourceSize=%d!=1\n",
unit.id, unit.context, unit.state, unit.source.size());
} else if (unit.target.size() > 1) {
System.out.format("ERROR: unit: id=%d key=%s state=%d res=targetSize=%d>1\n",
unit.id, unit.context, unit.state, unit.target.size());
} else {
final var kv = dicMap.get(unit.context);
if (kv == null) {
System.out.format(" WARN: unit: id=%d key=%s state=%d res=undefined source='%s'\n",
unit.id, unit.context, unit.state, unit.source.getFirst());
} else if (!kv.k.trim().equals(unit.source.getFirst().trim())) {
System.out.format(" WARN: unit: id=%d key=%s state=%d res=sourceUnmatched source='%s'!='%s'\n",
unit.id, unit.context, unit.state, unit.source.getFirst(), kv.k);
} else if (kv.v.equals(unit.target.getFirst())) {
System.out.format(" INFO: unit: id=%d key=%s state=%d res=matched\n",
unit.id, unit.context, unit.state);
} else {
final String r;
if (!unit.target.getFirst().isEmpty()) {
if (!arg.overwrite) {
System.out.format(" WARN: unit: id=%d key=%s state=%d res=targetUnmatched source='%s' target='%s'!='%s'\n",
unit.id, unit.context, unit.state, unit.source.getFirst(), unit.target.getFirst(), kv.v);
continue;
}
r = "overwriteTranslation";
} else
r = "needTranslation";
System.out.format(" INFO: unit: id=%d key=%s state=%d res=%s source='%s' target='%s' ... ",
unit.id, unit.context, unit.state, r, kv.k, kv.v);
unitMod.target.set(0, kv.v);
final var modJson = JsonWriter.local().clear().write(unitMod).toBytes();
delay();
req = HttpRequest.newBuilder().uri(URI.create(unit.url)).setHeader("Authorization", arg.auth)
.setHeader("Content-Type", "application/json; charset=utf-8")
.method("PATCH", HttpRequest.BodyPublishers.ofByteArray(modJson)).build();
res = hc.send(req, HttpResponse.BodyHandlers.ofByteArray());
if (res.statusCode() != 200) {
System.out.format("ERROR: HTTP Request Failed(%d): %s\n%s\n",
res.statusCode(), req.uri(), new String(res.body(), StandardCharsets.UTF_8));
} else
System.out.format("OK (%d ms)\n", System.nanoTime() / 1_000_000 - lastMs);
}
}
}
url = units.next;
if (url == null || url.isBlank() || url.equalsIgnoreCase("null")) {
System.out.println(" INFO: No Next");
break;
}
}
System.out.println(" INFO: Done!");
}
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

分享个人编写的各种编程语言的短代码
暂无标签
MIT
使用 MIT 开源许可协议
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
其他
1
https://gitee.com/dwing/sharecode.git
git@gitee.com:dwing/sharecode.git
dwing
sharecode
sharecode
main
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /