You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

82 lines
1.9 KiB

  1. // Copyright 2016 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package translate_test
  15. import (
  16. "context"
  17. "fmt"
  18. "cloud.google.com/go/translate"
  19. "golang.org/x/text/language"
  20. )
  21. func Example_NewClient() {
  22. ctx := context.Background()
  23. client, err := translate.NewClient(ctx)
  24. if err != nil {
  25. // TODO: handle error.
  26. }
  27. // Use the client.
  28. // Close the client when finished.
  29. if err := client.Close(); err != nil {
  30. // TODO: handle error.
  31. }
  32. }
  33. func Example_Translate() {
  34. ctx := context.Background()
  35. client, err := translate.NewClient(ctx)
  36. if err != nil {
  37. // TODO: handle error.
  38. }
  39. translations, err := client.Translate(ctx,
  40. []string{"Le singe est sur la branche"}, language.English,
  41. &translate.Options{
  42. Source: language.French,
  43. Format: translate.Text,
  44. })
  45. if err != nil {
  46. // TODO: handle error.
  47. }
  48. fmt.Println(translations[0].Text)
  49. }
  50. func Example_DetectLanguage() {
  51. ctx := context.Background()
  52. client, err := translate.NewClient(ctx)
  53. if err != nil {
  54. // TODO: handle error.
  55. }
  56. ds, err := client.DetectLanguage(ctx, []string{"Today is Monday"})
  57. if err != nil {
  58. // TODO: handle error.
  59. }
  60. fmt.Println(ds)
  61. }
  62. func Example_SupportedLanguages() {
  63. ctx := context.Background()
  64. client, err := translate.NewClient(ctx)
  65. if err != nil {
  66. // TODO: handle error.
  67. }
  68. langs, err := client.SupportedLanguages(ctx, language.English)
  69. if err != nil {
  70. // TODO: handle error.
  71. }
  72. fmt.Println(langs)
  73. }