0
from google.cloud import translate_v3 as translate

def translate_sentence(sentence, target_language):
  # Create a TranslationClient
  client = translate.TranslationClient()

  # Translate the sentence
  translation = client.translate(sentence, target_language=target_language)

  # Return the translated sentence
  return translation['translatedText']



import pandas as pd

# Create a Pandas dataframe with a single sentence
df = pd.DataFrame({'sentence': ['This is a sentence to be translated.']})

# Translate the sentence to French
df['translated_sentence'] = df['sentence'].apply(lambda x: translate_sentence(x, 'fr'))

# Print the translated sentence
print(df['translated_sentence'][0])

i am getting this error

it also says credentials is unknown. so i cant do credentials = credentials

AttributeError                            Traceback (most recent call last)
<ipython-input-11-f96ab99f032e> in <module>
      5 
      6 # Translate the sentence to French
----> 7 df['translated_sentence'] = df['sentence'].apply(lambda x: translate_sentence(x, 'fr'))
      8 
      9 # Print the translated sentence

5 frames
<ipython-input-10-ffde1fb2bc80> in translate_sentence(sentence, target_language)
      3 def translate_sentence(sentence, target_language):
      4   # Create a TranslationClient
----> 5   client = translate.TranslationClient()
      6 
      7   # Translate the sentence

AttributeError: module 'google.cloud.translate_v3' has no attribute 'TranslationClient'

Ive tried the regular version, but it did not work. i am using google colab notebook.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Please edit your question to make it legible, it is difficult to understand and answer your question the way it is currently written. – S. C. Dec 30 '22 at 20:56
  • [Under what circumstances may I add "urgent" or other similar phrases to my question, in order to obtain faster answers?](https://meta.stackoverflow.com/q/326569/11107541) – starball Dec 30 '22 at 21:01

1 Answers1

0

The method is TranslationServiceClient(), not TranslationClient().

I think that if you instantiate your client as:

from google.cloud import translate

client = translate.TranslationServiceClient()

Here one example from python console:

>>> from google.cloud import translate
>>> client = translate.TranslationServiceClient()
>>> client
<google.cloud.translate_v3.services.translation_service.client.TranslationServiceClient object at 0x7ff6db3905b0>

It will work correctly.

HTH, Luciano.

Luciano Martins
  • 421
  • 1
  • 9