Here’s what I did for an app I have in which I have stories I automatically translate to several languages.
1- Database structure.
This is the view of a story.
- The body field captures the text of the current story
- The language field captures the language of the current story (more on that later)
- Then I have many localized fields to capture translations for Indonesian (id), French (fr), Arabic (ar), English (en) … and others
2- When a story is created:
As users create new stories, I run several actions as soon as users hit the create a story button.
- 2.a Unmask a Lottie Spinner
- 2.b Detect the language of the current story (API call to Google Translate’s Natural Language detection) - More information below
- 2.c Now that we know which language the story is, we run several translations (IN, FR, EN, AR, …) - More information below
- 2.d Create a new story feeding all fields - More below
- 2.e Send notifications to the current user’s followers
- 2.f Mask the Lottie Spinner
More info regarding 2.b (language detection)
This API call takes the current story input as an input to the call.
This is how I set up the API call:
This is where I pass the story input to the API
Note that the API Key is added to the URL endpoint - You can get yours as you create your Google Platform account and ask for a Google Translate API key.
The API will send back the detected language of the story.
Let’s move to 2.c
This is where you call the Google Translate API for each of your target languages.
Note that the API call take 3 fields. The text to translate, the source language (which is the result of the previous call - Thanks Adalo for making that result available), and the target language (fr for French in this case).
Here’s the structure of the API call:
The API call requires these 3 fields at a minimum to be able to work.
The API call send back the translation field which you can use to store in the DB
Last stop, 2.d.
You can run the Google Translate API call as many times you have target languages. You only need to change the target language to the google code of that language.
Now that you have everything done, all you have to do is create a Story object in the DB (in my case) i which you store all of your data.
There are some tips along the way that I haven’t described yet but I hope at least this will get you going and excited.
I also want to point out that I do the same thing when users:
- edit existing stories
- create comments (seme structure)
- edit existing comments
Let me know
Best and keep building.