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

Auto translate #24

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@ Description: A number of utilities for developing and maintaining error, warning
and other messages in R packages, including checking for consistency across
messages, spell-checking messages, and building message translations into
various languages for purposes of localization.
Date: 2017-02-28
Date: 2017-06-25
Authors@R: c(person("Thomas J.", "Leeper",
role = c("aut", "cre"),
email = "[email protected]"))
email = "[email protected]"),
person("Richard", "Cotton",
role = "aut",
email = "[email protected]"))
URL: https://github.com/RL10N/msgtools/
BugReports: https://github.com/RL10N/msgtools/issues
License: MIT + file LICENSE
Version: 0.2.7
Version: 0.2.8
Depends: R (>= 3.2.0)
Imports:
tools,
utils,
poio (>= 0.0-3),
autotranslate,
devtools,
digest,
tibble,
hunspell,
devtools
poio (>= 0.0-3),
tibble,
translateR
Suggests:
testthat,
knitr,
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ export(dummy_pkg)
export(edit_translation)
export(get_message_distances)
export(get_messages)
export(google_translate)
export(install_gettext)
export(install_translations)
export(make_template)
export(make_translation)
export(microsoft_translate)
export(read_template)
export(read_translation)
export(spell_check_msgs)
Expand All @@ -22,6 +24,7 @@ export(use_localization)
export(write_template)
export(write_translation)
import(poio)
importFrom(autotranslate,get_translations)
importFrom(devtools,as.package)
importFrom(devtools,create)
importFrom(digest,digest)
Expand Down
130 changes: 130 additions & 0 deletions R/auto_translate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#' Auto translate messages
#'
#' Automaticlly translate messages using Google Translate or Microsoft
#' Translator.
#' @param translation An object of class \code{"po"} containing a message
#' translation.
#' @param overwrite Logical. If \code{TRUE}, translations will be found for all
#' messages. If \code{FALSE}, only the messages that do not have an existing
#' translation will be translated.
#' @param api_key A string describing an API key for Google Translate or Microsoft
#' Translator.
#' @param parallelization_strategy A string naming a parallelization strategy,
#' passed to \code{\link[future]{plan}}.
#' @return An object of class \code{"po"} containing a message translation.
#' @note To get a Google Translate API KEY, you must create a Google Cloud
#' account. See \url{https://cloud.google.com/translate/docs/getting-started}
#' to get started.
#' Likewise, to use Microsfot Translator, you need a Microsoft Azure account.
#' See \url{https://www.microsoft.com/en-us/translator/getstarted.aspx} to get
#' started.#' @examples
#' \dontrun{
#' # create example package
#' pkg <- dummy_pkg()
#'
#' # setup pkg for localization
#' use_localization(pkg)
#'
#' # generate Spanish translation in memory
#' (tran <- make_translation("es", translator = "Some Person <[email protected]>"))
#'
#' # You need a Google Translate API key before running this
#' auto_tran_google <- google_translate(tran)
#' View(auto_tran_google$direct)
#' View(auto_tran_google$countable)
#'
#' # You need a Microsoft Cognitive Services Translator API
#' # key before running this
#' auto_tran_microsoft <- microsoft_translate(tran)
#' View(auto_tran_microsoft$direct)
#' View(auto_tran_microsoft$countable)
#' }
#' @importFrom autotranslate get_translations
auto_translate <- function(translation, overwrite = FALSE, api_key,
parallelization_strategy = c("sequential", "multicore", "cluster"),
engine = c("google", "microsoft")) {
engine <- match.arg(engine)

to_translate <- if(overwrite) {
rep.int(TRUE, nrow(translation$direct))
} else {
translation$direct$msgstr == ""
}
direct_translations <- get_translations(
x = translation$direct$msgid[to_translate],
lang_to = translation$lang,
lang_from = "en",
api_key = api_key,
engine = engine
)
translation$direct$msgstr[to_translate] <- direct_translations

# There may be a more sophisticated way of getting countable translations
# for languages with multiple plural forms. For now if there is 1 plural form
# just pass the singular form, and return it for both values.
# In other cases, use the plural form multiple times.
to_translate <- if (overwrite) {
rep.int(TRUE, nrow(translation$countable))
} else {
vapply(
translation$countable$msgstr,
function(msgstri) {
all(msgstri == "")
},
logical(1L)
)
}
singular_countable_translations <- get_translations(
x = translation$countable$msgid[to_translate],
lang_to = translation$lang,
lang_from = "en",
api_key = api_key,
engine = engine
)
plural_countable_translations <- if (translation$n_plural_forms > 1L) {
get_translations(
x = translation$countable$msgid_plural[to_translate],
lang_to = translation$lang,
lang_from = "en",
api_key = api_key,
engine = engine
)
} else {
character(sum(to_translate))
}
m <- translation$n_plural_forms - 1L
translation$countable$msgstr[to_translate] <- Map(
function(singular, plural) {
c(singular, rep.int(plural, m))
},
singular_countable_translations,
plural_countable_translations
)
translation
}

#' @rdname auto_translate
#' @export
google_translate <- function(translation, overwrite = FALSE,
api_key = Sys.getenv("GOOGLE_TRANSLATE_API_KEY"),
parallelization_strategy = c("sequential", "multicore", "cluster")) {
auto_translate(
translation,
overwrite = overwrite,
api_key = api_key,
engine = "google"
)
}

#' @rdname auto_translate
#' @export
microsoft_translate <- function(translation, overwrite = FALSE,
api_key = Sys.getenv("MICROSOFT_TRANSLATOR_API_KEY"),
parallelization_strategy = c("sequential", "multicore", "cluster")) {
auto_translate(
translation,
overwrite = overwrite,
api_key = api_key,
engine = "microsoft"
)
}
2 changes: 1 addition & 1 deletion R/translations.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#'
#' \code{\link{edit_translation}} is a very simple interactive interface for editing a translation object in memory.
#'
#' @return \code{make_translation} and \code{read_translation} reutrn an object of class \dQuote{po}. \code{write_translation} returns the path to the file, invisibly.
#' @return \code{make_translation} and \code{read_translation} return an object of class \dQuote{po}. \code{write_translation} returns the path to the file, invisibly.
#' @note These functions require that gettext is installed on your system.
#' @author Thomas J. Leeper
#' @examples
Expand Down
70 changes: 70 additions & 0 deletions man/auto_translate.Rd

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

2 changes: 1 addition & 1 deletion man/translations.Rd

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

8 changes: 4 additions & 4 deletions po/R-msgtools.pot
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
msgid ""
msgstr ""
"Project-Id-Version: msgtools 0.2.7\n"
"Project-Id-Version: msgtools 0.2.8\n"
"Report-Msgid-Bugs-To: https://github.com/RL10N/msgtools/issues\n"
"POT-Creation-Date: 2017-03-01 14:52\n"
"PO-Revision-Date: 2017-03-01 14:52\n"
"POT-Creation-Date: 2017-06-18 15:45\n"
"PO-Revision-Date: 2017-06-18 15:45\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: LL\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=C:\Users\THOMAS\AppData\Local\Temp\RtmpySZ1eX/translateme\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

msgid "English message is:"
Expand Down