pub struct TranslateService { /* private fields */ }Implementations§
Source§impl TranslateService
impl TranslateService
Sourcepub async fn translate(
&mut self,
text: Vec<&str>,
target: &str,
params: Option<HashMap<String, Value>>,
) -> Result<TranslateTextResponse>
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
impl TranslateService
Sourcepub async fn list_languages(
&mut self,
target: Option<&str>,
model: Option<&str>,
) -> Result<ListLanguageResponse>
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
impl TranslateService
Sourcepub async fn detect_language(
&mut self,
text: Vec<&str>,
) -> Result<DetectLanguageResponse>
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
impl TranslateService
Sourcepub fn new_with_api_key(api_key: String) -> Self
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}Sourcepub fn new_with_credentials(
service_account_credentials: ServiceAccountCredentials,
) -> Self
pub fn new_with_credentials( service_account_credentials: ServiceAccountCredentials, ) -> Self
Create TranslateService Authenticate by using API keys.
service_account_credentials-ServiceAccountCredentialsto 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
impl Clone for TranslateService
Source§fn clone(&self) -> TranslateService
fn clone(&self) -> TranslateService
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for TranslateService
impl RefUnwindSafe for TranslateService
impl Send for TranslateService
impl Sync for TranslateService
impl Unpin for TranslateService
impl UnwindSafe for TranslateService
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more