Page updated Jan 16, 2024

Translate language

Amplify iOS v1 is now in Maintenance Mode until May 31st, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v1.

Please use the latest version (v2) of Amplify Library for Swift to get started.

If you are currently using v1, follow these instructions to upgrade to v2.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for iOS, you can access the documentation here.

If you haven't already done so, run amplify init inside your project and then amplify add auth (we recommend selecting the default configuration).

Run amplify add predictions, then use the following answers:

1? Please select from one of the categories below
2 Identify
3❯ Convert
4 Interpret
5 Infer
6 Learn More
7
8? What would you like to convert? (Use arrow keys)
9❯ Translate text into a different language
10 Generate speech audio from text
11 Transcribe text from audio
12
13? Provide a friendly name for your resource
14 <Enter a friendly name here>
15
16? What is the source language? (Use arrow keys)
17 <Select your default source language>
18
19? What is the target language? (Use arrow keys)
20 <Select your default target language>
21
22? Who should have access?
23 Auth users only
24❯ Auth and Guest users

Run amplify push to create the resources in the cloud

Working with the API

Here is an example of translating text. In order to override any choices you made in regards to target or source languages while adding this resource using the Amplify CLI, you can pass in them in directly as parameters as shown below.

1func translateText(text:String) {
2 Amplify.Predictions.convert(textToTranslate: text,
3 language: .english,
4 targetLanguage: .italian) { event in
5 switch event {
6 case let .success(result):
7 print(result.text)
8 case let .failure(error):
9 print(error)
10 }
11 }
12}
1func translateText(text:String) -> AnyCancellable {
2 Amplify.Predictions.convert(
3 textToTranslate: text,
4 language: .english,
5 targetLanguage: .italian
6 )
7 .resultPublisher
8 .sink {
9 if case let .failure(error) = $0 {
10 print(error)
11 }
12 }
13 receiveValue: { result in
14 print(result.text)
15 }
16}

As a result of running this code, you will see the translated text printed to the console.