Skip to content

Commit

Permalink
feat: check validity of google language codes using internal data
Browse files Browse the repository at this point in the history
  • Loading branch information
tin900 committed Dec 6, 2023
1 parent 72994d2 commit 68f305c
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 0 deletions.
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ Suggests:
text2vec
Config/testthat/edition: 3
VignetteBuilder: knitr
Depends:
R (>= 2.10)
LazyData: true
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export("%>%")
export(create_translation_table)
export(google_get_supported_languages)
export(google_is_valid_language_code)
export(google_translate)
export(language_detect)
export(linguee_external_sources)
Expand Down
23 changes: 23 additions & 0 deletions R/google_is_valid_language_code.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#' Check if a language code is valid
#'
#' This function checks if a given language code is in the `google_supported_languages` dataset.
#'
#' @param language_code The language code to check.
#' @return A logical value indicating if the language code is valid.
#' @export
#'
#' @examples
#' google_is_valid_language_code("en") # TRUE
#' google_is_valid_language_code("fr") # TRUE
#' google_is_valid_language_code("xx") # FALSE
google_is_valid_language_code <- function(language_code) {
if (language_code == "auto") {
return(TRUE)
}

if (language_code %in% polyglotr::google_supported_languages$`ISO-639 code`) {
return(TRUE)
} else {
return(FALSE)
}
}
8 changes: 8 additions & 0 deletions R/google_supported_languages.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#' Google Supported Languages
#'
#' This dataset contains the language names and iso codes of languages supported by Google Translate API.
#'
#' @format A data frame with two variables: language_name and iso_code
#' @source Google Translate API
#' @keywords data
"google_supported_languages"
9 changes: 9 additions & 0 deletions R/google_translate.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
#' google_translate(text_to_translate, "fr", "en")
#' }
google_translate <- function(text, target_language = "en", source_language = "auto") {

if (!google_is_valid_language_code(target_language)) {
stop("Invalid target language code.")
}

if (!google_is_valid_language_code(source_language)) {
stop("Invalid source language code.")
}

is_vector <- is.vector(text) && length(text) > 1

formatted_text <- urltools::url_encode(text)
Expand Down
Binary file added data/google_supported_languages.rda
Binary file not shown.
22 changes: 22 additions & 0 deletions man/google_is_valid_language_code.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions man/google_supported_languages.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/testthat/test-google_translate.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,15 @@ test_that("google translate returns translations with special characters", {
expected_translation <- "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..."
expect_equal(translation, expected_translation)
})

# Unit tests for checking supported languages in Google Translate
test_that("google_is_valid_language_code: valid codes", {
expect_true(google_is_valid_language_code("en"))
expect_true(google_is_valid_language_code("fr"))
expect_true(google_is_valid_language_code("auto"))
expect_true(google_is_valid_language_code("iw"))
})

test_that("google_is_valid_language_code: invalid codes", {
expect_false(google_is_valid_language_code("xx"))
})

0 comments on commit 68f305c

Please sign in to comment.