forked from Ajatt-Tools/gd-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.cpp
105 lines (92 loc) · 2.94 KB
/
translate.cpp
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
/*
* gd-tools - a set of programs to enhance goldendict for immersion learning.
* Copyright (C) 2025 Ajatt-Tools
*
* 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/>.
*/
#include "precompiled.h"
#include "util.h"
using namespace std::literals;
namespace sp = subprocess;
static constexpr std::string default_to{ "en" };
static constexpr std::string_view help_text = R"EOF(usage: gd-translate [OPTIONS]
Translate text from Japanese to target language
OPTIONS
--to LANG target language (default: en)
--sentence SENTENCE japanese sentence to translate
--spoiler yes/no black out the sentence with a spoiler box (default: no)
EXAMPLES
gd-translate --spoiler yes --sentence %GDSEARCH%
gd-translate --spoiler yes --to fr --sentence %GDSEARCH%
)EOF";
static constexpr std::string_view css_style = R"EOF(<style>
.spoiler {
background-color: black;
padding: 6px;
width: fit-content;
}
.spoiler:hover {
background-color: white;
}
</style>)EOF";
struct translate_params
{
std::string to{ default_to };
std::string gd_word;
std::string spoiler{ "no" };
void assign(std::string_view const key, std::string_view const value)
{
if (key == "--to") {
to = value;
} else if (key == "--sentence") {
gd_word = value;
} else if (key == "--spoiler") {
if (value == "yes") {
spoiler = value;
}
}
}
};
void exec_translate(translate_params const& params)
{
auto cmd_argos = sp::Popen(
{
"argos-translate",
"-f",
"ja",
"-t",
params.to,
params.gd_word,
},
sp::output{ sp::PIPE }
);
auto cmd_tail = sp::Popen({ "tail", "-n1" }, sp::input{ cmd_argos.output() }, sp::output{ sp::PIPE });
auto resp = cmd_tail.communicate().first;
std::println("<div{}>", params.spoiler == "yes" ? " class=\"spoiler\"" : "");
std::println("{}", std::string_view(resp.buf.data(), resp.length));
std::println("</div>");
std::println("{}", css_style);
}
void translate(std::span<std::string_view const> const args)
{
try {
exec_translate(fill_args<translate_params>(args));
} catch (gd::help_requested const& ex) {
std::print(help_text);
} catch (gd::runtime_error const& ex) {
std::println("{}", ex.what());
} catch (std::runtime_error const& ex) {
std::println("subprocess error. {}", ex.what());
}
}