Skip to content
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

Error in API call #6

Closed
ToferC opened this issue May 19, 2021 · 7 comments
Closed

Error in API call #6

ToferC opened this issue May 19, 2021 · 7 comments

Comments

@ToferC
Copy link

ToferC commented May 19, 2021

Hi Grant - first of all - great work on the crate. I had set it up and was using it in testing in an Actix-Web app and it was working well, but a few days ago it stopped working with the following error:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ParseError("Unable to find translatedText in parsed JSON")', src/main.rs:172:101

I'm thinking there might have been a change in the LibreTranslate API

Here's the test snippet that mirrors my app:

use libretranslate::{translate, Language};

#[tokio::main]
async fn main() {
    let mut translate_strings: Vec<String> = Vec::new();

    let nodes = vec![
        ("father", "person"),
        ("manager", "role"),
        ("gen x", "person"),
    ];
    
    for n in &nodes {
        translate_strings.push(format!("{}.\n", &n.0));
    };

    let source = Language::English;
    let target = Language::French;

    let input = translate_strings.concat();

    let data = translate(source, target, input)
        .await
        .unwrap();

    let mut phrases: Vec<(String, String)> = Vec::new();

    //let en = data.input.split(". ").into_iter();
    let fr = data.output.split(".\n");
    let en = data.input.split(".\n");

    for (f, e) in fr.zip(en) {
        if e != "" {
            phrases.push((e.to_lowercase().replace("/", ""), f.to_lowercase().replace("/","")));
        };
    };

    println!("{:?}", &phrases);
@PJ-Finlay
Copy link

PJ-Finlay commented May 19, 2021

@PJ-Finlay
Copy link

It may make sense to point the default rust bindings to one of the mirrors instead of libretranslate.com to take pressure off the main site.

@pierotofy
Copy link

pierotofy commented May 19, 2021

Yeah ideally any bindings library should allow developers to choose a host to point to (and to not default to libretranslate.com).

We'd love to be able to provide a public API translation for everyone, but it's just not sustainable.

You can still use the API endpoints from libretranslate.com, but now you need an API key for programmatic access.

@ToferC
Copy link
Author

ToferC commented May 20, 2021

Makes perfect sense, @pierotofy. I'd suggest the requirement should be documented in the crate and a clearer error message be transmitted back.

@grantshandy
Copy link
Owner

Hey thanks for all this, I'll make the changes now.

@grantshandy
Copy link
Owner

grantshandy commented May 20, 2021

Hey thanks for the issue, I should have solved the problem.

You can now translate using a custom URL, as well as get better errors from the website.

See the example.

Does this fix your problem? @ToferC, @pierotofy & @PJ-Finlay

@ToferC
Copy link
Author

ToferC commented May 21, 2021

Thanks @grantshandy - fixes look great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants