TranslateService

Struct TranslateService 

Source
pub struct TranslateService { /* private fields */ }

Implementations§

Source§

impl TranslateService

Source

pub async fn translate( &mut self, text: Vec<&str>, target: &str, params: Option<HashMap<String, Value>>, ) -> Result<TranslateTextResponse>

Translates text into the target language.
See https://g.co/cloud/translate/v2/translate-reference#supported_languages https://cloud.google.com/translate/docs/reference/rest/v2/translate https://cloud.google.com/translate/docs/basic/translating-text#translate_translate_text-drest

  • text - an array of strings to be translated.
  • target - The language to use for translation of the input text.
  • params - Optional Additional Parameter. Keys accepted are the following.
    • format - The format of the source text, in either HTML (default) or plain-text. A value of html indicates HTML and a value of text indicates plain-text.
    • source - The language of the source text.
    • model - The translation model. Cloud Translation - Basic offers only the nmt Neural Machine Translation (NMT) model. If the model is base, the request is translated by using the NMT model.
Examples found in repository?
examples/translate_service.rs (line 30)
24async fn translate() -> Result<()> {
25    dotenv().ok();
26
27    // api auth
28    let api_key = env::var("API_KEY")?;
29    let mut translation_service = TranslateService::new_with_api_key(api_key);
30    let response = translation_service.translate(vec!["test"], "ja", None).await?;
31    println!("response: {}", serde_json::to_string(&response)?);
32
33    // service account auth
34    let filepath: PathBuf = PathBuf::from_str("credentials.json")?;
35    let credentials = ServiceAccountCredentials::from_service_account_file(filepath)?;
36    let mut translation_service = TranslateService::new_with_credentials(credentials);
37    let response = translation_service.translate(vec!["test"], "ja", None).await?;
38    println!("response: {}", serde_json::to_string(&response)?);
39    let mut params: HashMap<String, Value> = HashMap::new();
40    params.insert("format".to_string(), "text".into());
41    params.insert("source".to_string(), "en".into());
42    params.insert(" model".to_string(), "base".into());
43    let response_with_params = translation_service.translate(vec!["test2"], "ja", Some(params)).await?;
44    println!("response_with_params: {}", serde_json::to_string(&response_with_params)?);
45
46    Ok(())
47}
Source§

impl TranslateService

Source

pub async fn list_languages( &mut self, target: Option<&str>, model: Option<&str>, ) -> Result<ListLanguageResponse>

List supported languages.
See https://cloud.google.com/translate/docs/basic/discovering-supported-languages

  • target - The target language code for the results. If specified, then the language names are returned in the name field of the response, localized in the target language. If you do not supply a target language, then the name field is omitted from the response and only the language codes are returned.
  • model - The supported languages for a particular translation model. For Cloud Translation - Basic, the value can be nmt to return languages supported by the Neural Machine Translation (NMT) model.
Examples found in repository?
examples/translate_service.rs (line 56)
50async fn list_languages() -> Result<()> {
51    dotenv().ok();
52
53    // api auth
54    let api_key = env::var("API_KEY")?;
55    let mut translation_service = TranslateService::new_with_api_key(api_key);
56    let response = translation_service.list_languages(Some("ja"), None).await?;
57    println!("response: {}", serde_json::to_string(&response)?);
58
59    // service account auth
60    let filepath: PathBuf = PathBuf::from_str("credentials.json")?;
61    let credentials = ServiceAccountCredentials::from_service_account_file(filepath)?;
62    let mut translation_service = TranslateService::new_with_credentials(credentials);
63    let response = translation_service.list_languages(Some("ja"), None).await?;
64    println!("response: {}", serde_json::to_string(&response)?);
65
66    Ok(())
67}
Source§

impl TranslateService

Source

pub async fn detect_language( &mut self, text: Vec<&str>, ) -> Result<DetectLanguageResponse>

Detects the language of texts.
See https://cloud.google.com/translate/docs/basic/detecting-language

  • text - an array of strings to upon which to perform language detection.
Examples found in repository?
examples/translate_service.rs (line 76)
70async fn detect_language() -> Result<()> {
71    dotenv().ok();
72
73    // api auth
74    let api_key = env::var("API_KEY")?;
75    let mut translation_service = TranslateService::new_with_api_key(api_key);
76    let response = translation_service.detect_language(vec!["test", "テスト"]).await?;
77    println!("response: {}", serde_json::to_string(&response)?);
78
79    // service account auth
80    let filepath: PathBuf = PathBuf::from_str("credentials.json")?;
81    let credentials = ServiceAccountCredentials::from_service_account_file(filepath)?;
82    let mut translation_service = TranslateService::new_with_credentials(credentials);
83    let response = translation_service.detect_language(vec!["test", "テスト"]).await?;
84    println!("response: {}", serde_json::to_string(&response)?);
85
86    Ok(())
87}
Source§

impl TranslateService

Source

pub fn new_with_api_key(api_key: String) -> Self

Create TranslateService Authenticate by using API keys.

  • api_key - API key to use to authenticate to Google Cloud APIs and services that support API keys.
Examples found in repository?
examples/translate_service.rs (line 29)
24async fn translate() -> Result<()> {
25    dotenv().ok();
26
27    // api auth
28    let api_key = env::var("API_KEY")?;
29    let mut translation_service = TranslateService::new_with_api_key(api_key);
30    let response = translation_service.translate(vec!["test"], "ja", None).await?;
31    println!("response: {}", serde_json::to_string(&response)?);
32
33    // service account auth
34    let filepath: PathBuf = PathBuf::from_str("credentials.json")?;
35    let credentials = ServiceAccountCredentials::from_service_account_file(filepath)?;
36    let mut translation_service = TranslateService::new_with_credentials(credentials);
37    let response = translation_service.translate(vec!["test"], "ja", None).await?;
38    println!("response: {}", serde_json::to_string(&response)?);
39    let mut params: HashMap<String, Value> = HashMap::new();
40    params.insert("format".to_string(), "text".into());
41    params.insert("source".to_string(), "en".into());
42    params.insert(" model".to_string(), "base".into());
43    let response_with_params = translation_service.translate(vec!["test2"], "ja", Some(params)).await?;
44    println!("response_with_params: {}", serde_json::to_string(&response_with_params)?);
45
46    Ok(())
47}
48
49
50async fn list_languages() -> Result<()> {
51    dotenv().ok();
52
53    // api auth
54    let api_key = env::var("API_KEY")?;
55    let mut translation_service = TranslateService::new_with_api_key(api_key);
56    let response = translation_service.list_languages(Some("ja"), None).await?;
57    println!("response: {}", serde_json::to_string(&response)?);
58
59    // service account auth
60    let filepath: PathBuf = PathBuf::from_str("credentials.json")?;
61    let credentials = ServiceAccountCredentials::from_service_account_file(filepath)?;
62    let mut translation_service = TranslateService::new_with_credentials(credentials);
63    let response = translation_service.list_languages(Some("ja"), None).await?;
64    println!("response: {}", serde_json::to_string(&response)?);
65
66    Ok(())
67}
68
69
70async fn detect_language() -> Result<()> {
71    dotenv().ok();
72
73    // api auth
74    let api_key = env::var("API_KEY")?;
75    let mut translation_service = TranslateService::new_with_api_key(api_key);
76    let response = translation_service.detect_language(vec!["test", "テスト"]).await?;
77    println!("response: {}", serde_json::to_string(&response)?);
78
79    // service account auth
80    let filepath: PathBuf = PathBuf::from_str("credentials.json")?;
81    let credentials = ServiceAccountCredentials::from_service_account_file(filepath)?;
82    let mut translation_service = TranslateService::new_with_credentials(credentials);
83    let response = translation_service.detect_language(vec!["test", "テスト"]).await?;
84    println!("response: {}", serde_json::to_string(&response)?);
85
86    Ok(())
87}
Source

pub fn new_with_credentials( service_account_credentials: ServiceAccountCredentials, ) -> Self

Create TranslateService Authenticate by using API keys.

  • service_account_credentials - ServiceAccountCredentials to use to authenticate to Google Cloud APIs.
Examples found in repository?
examples/translate_service.rs (line 36)
24async fn translate() -> Result<()> {
25    dotenv().ok();
26
27    // api auth
28    let api_key = env::var("API_KEY")?;
29    let mut translation_service = TranslateService::new_with_api_key(api_key);
30    let response = translation_service.translate(vec!["test"], "ja", None).await?;
31    println!("response: {}", serde_json::to_string(&response)?);
32
33    // service account auth
34    let filepath: PathBuf = PathBuf::from_str("credentials.json")?;
35    let credentials = ServiceAccountCredentials::from_service_account_file(filepath)?;
36    let mut translation_service = TranslateService::new_with_credentials(credentials);
37    let response = translation_service.translate(vec!["test"], "ja", None).await?;
38    println!("response: {}", serde_json::to_string(&response)?);
39    let mut params: HashMap<String, Value> = HashMap::new();
40    params.insert("format".to_string(), "text".into());
41    params.insert("source".to_string(), "en".into());
42    params.insert(" model".to_string(), "base".into());
43    let response_with_params = translation_service.translate(vec!["test2"], "ja", Some(params)).await?;
44    println!("response_with_params: {}", serde_json::to_string(&response_with_params)?);
45
46    Ok(())
47}
48
49
50async fn list_languages() -> Result<()> {
51    dotenv().ok();
52
53    // api auth
54    let api_key = env::var("API_KEY")?;
55    let mut translation_service = TranslateService::new_with_api_key(api_key);
56    let response = translation_service.list_languages(Some("ja"), None).await?;
57    println!("response: {}", serde_json::to_string(&response)?);
58
59    // service account auth
60    let filepath: PathBuf = PathBuf::from_str("credentials.json")?;
61    let credentials = ServiceAccountCredentials::from_service_account_file(filepath)?;
62    let mut translation_service = TranslateService::new_with_credentials(credentials);
63    let response = translation_service.list_languages(Some("ja"), None).await?;
64    println!("response: {}", serde_json::to_string(&response)?);
65
66    Ok(())
67}
68
69
70async fn detect_language() -> Result<()> {
71    dotenv().ok();
72
73    // api auth
74    let api_key = env::var("API_KEY")?;
75    let mut translation_service = TranslateService::new_with_api_key(api_key);
76    let response = translation_service.detect_language(vec!["test", "テスト"]).await?;
77    println!("response: {}", serde_json::to_string(&response)?);
78
79    // service account auth
80    let filepath: PathBuf = PathBuf::from_str("credentials.json")?;
81    let credentials = ServiceAccountCredentials::from_service_account_file(filepath)?;
82    let mut translation_service = TranslateService::new_with_credentials(credentials);
83    let response = translation_service.detect_language(vec!["test", "テスト"]).await?;
84    println!("response: {}", serde_json::to_string(&response)?);
85
86    Ok(())
87}

Trait Implementations§

Source§

impl Clone for TranslateService

Source§

fn clone(&self) -> TranslateService

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TranslateService

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,