Giter Club home page Giter Club logo

Comments (5)

Tomeriko96 avatar Tomeriko96 commented on August 20, 2024

Hi @lbajemon ,

Thank you for reporting this issue. I have tested the code and can confirm the problem you've described.

I'll be looking into it and will provide an update as soon as I have more information.

from polyglotr.

tin900 avatar tin900 commented on August 20, 2024

hi @lbajemon,

Could you try the following function:

google_translate_test <- function(text, target_language = "en", source_language = "auto") {
  is_vector <- is.vector(text) && length(text) > 1
  
  formatted_text <- urltools::url_encode(text)
  
  formatted_link <- paste0(
    "https://translate.google.com/m?tl=",
    target_language, "&sl=", source_language,
    "&q=",
    formatted_text
  )
  
  if (is_vector) {
    responses <- purrr::map(formatted_link, httr::GET)
    
    translations <- purrr::map(responses, ~ {
      translation <- httr::content(.x) %>%
        rvest::html_nodes("div.result-container") %>%
        rvest::html_text()
      
      translation <- urltools::url_decode(translation)
      translation <- gsub("\n", "", translation)
      
      translation
    })
    
    return(translations)
  } else {
    response <- httr::GET(formatted_link)
    
    translation <- httr::content(response) %>%
      rvest::html_nodes("div.result-container") %>%
      rvest::html_text()
    
    translation <- urltools::url_decode(translation)
    translation <- gsub("\n", "", translation)
    
    return(translation)
  }
}

Running your examples now returns the following:

> text_fr = "La Saône prend sa source à Vioménil dans les pré-Vosges à 405 m d'altitude. La rivière conflue avec le Rhône 473,3 km plus loin."
> fr_to_en = google_translate_test(text_fr, "en", "fr")
> fr_to_en
[1] "The Saône has its source at Vioménil in the pre-Vosges at an altitude of 405 m. The river confluences with the Rhône 473.3 km further."
> # in Arabic
> text_ar = "يتدفقُ النيل عبر الصحراء السودانية إلى مصر باتجاه الشمال ويمر في مدينةُ القاهرة الواقعة على دلتا النهر الكبيرة (دلتا النيل)، ثم يعبر النهر مدينتي دمياط ورشيد ويصب ..."
> ar_to_en = google_translate_test(text_ar, "en", "ar")
> ar_to_en
[1] "The Nile flows through the Sudanese desert to Egypt towards the north and passes through the city of Cairo, located on the large river delta (Nile Delta), then the river crosses the cities of Damietta and Rosetta and flows..."

from polyglotr.

lbajemon avatar lbajemon commented on August 20, 2024

Hi @tin900, it works with this function. Thank you very much

from polyglotr.

tin900 avatar tin900 commented on August 20, 2024

Awesome!

Commit 72994d2 implements the bugfix.

You can install the development version of the package to use the improved google_translate() function.

I will update the package on CRAN in the coming days as well.

from polyglotr.

Tomeriko96 avatar Tomeriko96 commented on August 20, 2024

The new version of the package is now available on CRAN

from polyglotr.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.