Package {textrar}


Title: Interface to 'TexTra' from R
Version: 0.9.0
Description: A wrapper for the 'TexTra' API https://mt-auto-minhon-mlt.ucri.jgn-x.jp/, a web service for translating texts between different languages. 'TexTra' API account is required to use the service.
License: MIT + file LICENSE
Encoding: UTF-8
URL: https://github.com/matutosi/textrar, https://matutosi.github.io/textrar/
Imports: jsonlite, httr
Suggests: knitr, rmarkdown, spelling, testthat (≥ 3.2.0), withr
Language: en-US
Depends: R (≥ 3.6)
Config/roxygen2/version: 8.1.0
Config/testthat/edition: 3
NeedsCompilation: no
Packaged: 2026-08-28 19:37:15 UTC; matutosi
Author: Toshikazu Matsumura ORCID iD [aut, cre]
Maintainer: Toshikazu Matsumura <matutosi@gmail.com>
Repository: CRAN
Date/Publication: 2026-08-28 20:20:02 UTC

Return the base URL for the API

Description

Return the base URL for the API

Usage

base_url()

Value

A string containing the base URL.

Deprecated

base_url() is a low level building block that was never meant to be part of the public interface. It is deprecated as of textrar 0.9.0 and will be removed in a future release. If you rely on it, please open an issue at https://github.com/matutosi/textrar/issues.

Examples

## Not run: 
base_url()

## End(Not run)


Extract Translated Text from Response

Description

This function extracts the translated text from the response. returned by the post_request() function.

Usage

extract_result(res)

Arguments

res

The response object returned by the post_request().

Value

A character string containing the translated text. An error is raised when the API reports a failure. Note that the API answers with HTTP 200 even when it fails, and reports the failure in resultset$code, so the status code alone cannot be relied upon.

Deprecated

extract_result() is a low level building block that was never meant to be part of the public interface. It is deprecated as of textrar 0.9.0 and will be removed in a future release. Use textra() instead. If you rely on it, please open an issue at https://github.com/matutosi/textrar/issues.

Examples

## Not run: 
res <- post_request(params, "Hello world!")
translated <- extract_result(res)

## End(Not run)


Generate parameters for API call

Description

This function generates a list of parameters that can be used to make an API call.

Usage

gen_params(
  key = textra_env("TEXTRA_API_KEY"),
  secret = textra_env("TEXTRA_API_SECRET"),
  name = textra_env("TEXTRA_NAME"),
  api_name = "mt"
)

Arguments

key

The API key. Defaults to the environment variable TEXTRA_API_KEY.

secret

The API secret. Defaults to the environment variable TEXTRA_API_SECRET.

name

The login ID of the 'TexTra' account. Defaults to the environment variable TEXTRA_NAME.

api_name

The name of the API to use. Defaults to "mt".

Details

Storing the credentials in the environment rather than in your scripts keeps them out of version control and out of shared files. Add the following lines to your user .Renviron file, which usethis::edit_r_environ() opens, and restart R.

TEXTRA_API_KEY=your_api_key
TEXTRA_API_SECRET=your_api_secret
TEXTRA_NAME=your_login_id

Value

A list of parameters.

Examples

## Not run: 
# Using the credentials stored in .Renviron (recommended).
params <- gen_params()

# Or passing them explicitly.
key <- "abcdefghijklmnopqrstuvw01234567890abcdef1" # API key
secret <- "xyzabcdefghijklmnopqrstuvw012345"       # API secret
name <- "login_ID"                                 # login_ID
params <- gen_params(key = key, secret = secret, name = name)

## End(Not run)


Get Access Token

Description

This function retrieves an access token from the API using the provided key and secret.

Usage

get_token(
  key = textra_env("TEXTRA_API_KEY"),
  secret = textra_env("TEXTRA_API_SECRET")
)

Arguments

key

The API key. Defaults to the environment variable TEXTRA_API_KEY.

secret

The API secret. Defaults to the environment variable TEXTRA_API_SECRET.

Value

A character string containing the access token. An error is raised when 'TexTra' rejects the credentials.

Examples

## Not run: 
# Using the credentials stored in .Renviron (recommended).
token <- get_token()

# Or passing them explicitly.
key <- "abcdefghijklmnopqrstuvw01234567890abcdef1" # API key
secret <- "xyzabcdefghijklmnopqrstuvw012345"       # API secret
token <- get_token(key = key, secret = secret)

## End(Not run)


Send a POST request to the API

Description

This function sends a POST request to the API with the specified parameters and text.

Usage

post_request(params, text)

Arguments

params

A list of parameters to be passed to the API.

text

The text to be translated.

Value

The response from the API.

Deprecated

post_request() is a low level building block that was never meant to be part of the public interface. It is deprecated as of textrar 0.9.0 and will be removed in a future release. Use textra() instead. If you rely on it to reach an endpoint that textra() does not cover, please open an issue at https://github.com/matutosi/textrar/issues so that a supported way of doing so can be provided.

Examples

## Not run: 
post_request(params, text = "Hello, world!")

## End(Not run)


Translate text with 'TexTra'

Description

This function translates text with the 'TexTra' machine translation API.

Usage

textra(text, params, model = "transLM", from = "en", to = "ja")

Arguments

text

The text to be translated.

params

A list of parameters to be passed to the API.

model

The model to be used for translation.

from

The source language.

to

The target language.

Value

The translated text.

Examples

## Not run: 
text <- "Hello world"
key <- "abcdefghijklmnopqrstuvw01234567890abcdef1" # API key
secret <- "xyzabcdefghijklmnopqrstuvw012345"       # API secret
name <- "login_ID"                                 # login_ID
params <- gen_params(key = key, secret = secret, name = name)
translated <- 
  textra(text, params, model = "transLM", from = "en", to = "ja")
translated

## End(Not run)