Custom translate source (Webpage Translation) #50
chunibyocola
started this conversation in
Show and tell
Replies: 2 comments
-
能否将这部分修改的更加便捷一点, 例如提供如彩云小译的token 或者百度的tokenid之类的即可实现对接, 因为本地起一个这样的翻译服务实在不现实 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
You can add translate source for webpage translation which built by yourself or others in the Sc Translator Options page.
If you want your webpage get translated by not only Google and Microsoft Webpage Translator but also DeepL and the others, you should use this feature.
Build your translate source (Based on Node.js)
Initialize project
Environment (The environment that I develop this example):
Scripts:
Create a file named index.js in folder server-example.
Add
"main": "index.js"
and"type": "module"
inpackage.json
:Copy the codes in
index.js
:Run this project:
Explanation
supportedTargetLangCodes
supportedTargetLangCodes
is a map to check the language that server supports or not from client's request.The language code from extension could only be one of the language codes map except
auto
.Request body from client
You can see there is deconstruct syntax in
app.post
:The request body from client will follow the type:
How Sc Translator calculate
paragraphs
:HTML from webpage like:
Will be converted to (paragraphs):
Errors
If there are errors happened in server or you want to tell user something, you can send fixed strings to client like:
or
, etc.
You can also send sentence like:
Don't assign to code with anything if there are no errors!
Response
Response from server must follow the type:
And the length of
result
must be the same as the length ofparagraphs
.translations
in item of resulttranslations
must be an array of strings and must be the same length as the item's ofparagraphs
.translations
will be displayed in "Original Text + Translation mode" (comparisons
isundefined
) and "Translation only mode".comparisons
in item of resultcomparisons
is an optional param. Whencomparisons
is notundefined
,comparisons
will be displayed in "Original Text + Translation mode".What if my translate source doesn't support XML/HTML format translating?
If the item of
paragraphs
contains 3 string like:You can response with translated Chinese in two empty string and a translation like:
With this situation, you should not make Sc Translator display the translations in "Translation only mode" way.
Beta Was this translation helpful? Give feedback.
All reactions