-
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Any chance for an API? #37
Comments
It is absolutely possible: in the current version, you can access the currently active translator API using There is a little bit of documentation on how you can use the API (see here), but I realize I don't have a lot of examples (and it is rather out-of-date), so here's a specific example of how you could access the API and get translations: const translator = app.plugins.plugins['translate']?.translator
// Make sure to check whether translator is defined
const translation_response = await translator.translate(TEXT, FROM, TO, APPLY_GLOSSARY)
if (translation_response.status_code == 200) {
const translated_term = translation_response.output
// Do something with the translated term/sentence
} else {
// If translation API encounters errors, make sure to display it to the user
console.error(translation_response.message)
} Where Make sure to take a look at lingva-translate.ts (or any of the other service implementations) too. They'll give you a good idea on what the API returns.
I'd really like to be able to provide this too, but sadly, there is currently only one translation service that offers descriptions for a translation, and that is LingvaTranslate (and perhaps Fanyi Baidu/... too). So for now, this output is not returned in the API. However, you might be able to phibro0's obsidian-dictionary plugin to get term descriptions: const translated_term = 'SOMETHING';
const dictionary_plugin = app.plugins.plugins['obsidian-dictionary-plugin']?.manager
if (dictionary_plugin) {
const descriptions = dictionary_plugin.requestDefinitions(translated_term);
if (descriptions) {
// Do something with the word descriptions
}
} Though I believe that the dictionary_plugin will only look up the term in the language that the user has specified in the settings, so that approach might not work. In the future, I may make my own plugin for getting synonyms/descriptions/etc.. using any available API, obsidian-dictionary plugin already does a lot, but I'd need to have a more rigid API in order to get the functionaity I want for the translate plugin. Currently, I don't really have the time to work on it, so it wouldn't be finished by February, at the earliest. |
Hey @MMoMM-org, does this solve your issue? I'd be glad to help you further/improve upon the API docs wherever possible. |
Hi, thanks that helps a lot. You plugin is more for full sentences. But nevertheless:
And thanks again for writing this up, this helped a lot. |
That's true, I'll be trying to get some sort of dictionary integration going for the future which should give better translations for single words/terms, but as mentioned before, that's going to be a little ways off.
The API does handle all internal errors, but when you make a request, it isn't guaranteed that you get back a translation (due to service being offline, invalid authentication info, ...). So that's why I'm checking The reason why the message isn't automatically displayed, is so that people can decide what to do with it: completely ignoring it, throwing an error, putting it in console log, making a notice in Obsidian, etc.. Basically what I'm saying is, it's perfectly fine to just use the code below, but you will not know if/how the translation failed. // If an error occurs during translation, translated_text = undefined
const translated_text = await translator.translate(TEXT, FROM, TO, APPLY_GLOSSARY).translation
Don't worry, I won't be closing the issue till we're both satisfied! I've added a brief API documentation here, for everyone that needs it. If you have any suggestions on how I could improve it, please let me know! |
I would like to pass a word, source language, target language to the api and get back the translated word.
Probably the return value needs to be an array with the translation word and perhaps the descriptions of it.
Would that be possible?
The text was updated successfully, but these errors were encountered: