# Mutlilingual bot (capturing user input and intercepting bot's output for translation)

**URL:** <https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142>\
**Category:** Rasa Open Source\
**Created:** [July 25, 2021, 10:33pm UTC](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142 "2021-07-25T22:33:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![amaelbogne](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/amaelbogne/32/13795_2.png) [@amaelbogne](https://forum.rasa.com/u/amaelbogne)\
**Post date:** [July 25, 2021, 10:33pm UTC](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142/1 "2021-07-25T22:33:49Z")

</div>

HI I build a bot in English and now I want it to be able to dialogue in French as well.

my approach is to capture user input, detect the language and reply accordingly.

so I would like to know: 1- how can I capture user input so as to detect the language? 2- how can I intercept the bot’s output so as to translate the reply before sending it to user if need be ?

---

<div class="post-metadata">

**Author:** ![ChrisRahme](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/chrisrahme/32/15572_2.png) [@ChrisRahme](https://forum.rasa.com/u/ChrisRahme)\
**Post date:** [July 26, 2021, 6:19am UTC](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142/2 "2021-07-26T06:19:16Z")

</div>

You’ll need to build a [Custom Pipeline Component](https://rasa.com/docs/rasa/components/#custom-components) ([read more](https://blog.rasa.com/enhancing-rasa-nlu-with-custom-components/)) that detects the language of the input.

You have 2 possibilities:

**1. Understand and respond in only French and English:**

Introduce intents with examples in both English and French.

```auto
- intent: greet
  examples: | 
  - hello
  - bonjour
etc...

```

Your Component will only detect the language only and save it.

All your responses will need to be [Custom Actions](https://rasa.com/docs/rasa/custom-actions); in which you access the language and dispatch the correct message accordingly:

```auto
if language == "fr":
    dispatcher.utter_message("Salut! Comment puis-je vous aider?")
    #/* or dispatcher.utter_message(response = "utter_greet_fr") */
else:
     dispatcher.utter_message("Hey! How can I help you?")
    #/* or dispatcher.utter_message(response = "utter_greet_en") */

```

**UPDATE:** You can now use [Conditional Response Variations](https://rasa.com/docs/rasa/responses#conditional-response-variations) instead:

```auto
responses:
  utter_greet:
    - condition:
        - type: slot
          name: language
          value: fr
      text: "Salut! Comment puis-je vous aider?"
    - text: "Hey! How can I help you?"

```

**2. Understand and respond in any language:**

Introduce intents with examples in English only.

```auto
- intent: greet
  examples: | 
  - hello
  - hey there
etc...

```

Your Component will detect the language, save it, and then translate the user input into English before passing it to the rest of the Pipeline, which will therefore process the translated English text and match it to an intent.

All your responses will need to be [Custom Actions](https://rasa.com/docs/rasa/custom-actions); in which you access the language, and translate then dispatch the message accordingly:

```auto
english_text = translateEnglish("Hey! How can I help you?", language)
dispatcher.utter_message(english_text)

```

---

<div class="post-metadata">

**Author:** ![amaelbogne](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/amaelbogne/32/13795_2.png) [@amaelbogne](https://forum.rasa.com/u/amaelbogne)\
**Post date:** [July 26, 2021, 9:35am UTC](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142/3 "2021-07-26T09:35:55Z")

</div>

Thanks a lot for your detailed reply.

I’ll go for the 2nd approach. And surely reach back out if I encounter some difficulties

thanks again

---

<div class="post-metadata">

**Author:** ![ChrisRahme](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/chrisrahme/32/15572_2.png) [@ChrisRahme](https://forum.rasa.com/u/ChrisRahme)\
**Post date:** [August 18, 2021, 6:28pm UTC](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142/4 "2021-08-18T18:28:51Z")

</div>

Did you succeed?

If yes, please mark the answer as solution. If not, tell me what’s the problem and I’ll try to help 🙂

---

<div class="post-metadata">

**Author:** ![amaelbogne](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/amaelbogne/32/13795_2.png) [@amaelbogne](https://forum.rasa.com/u/amaelbogne)\
**Post date:** [August 19, 2021, 5:04pm UTC](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142/5 "2021-08-19T17:04:31Z")

</div>

Yes I did succeed

but I have a little issue. my bot has a form and when prompting for slots forms look for the utter\_ask\_slot response that is defined. So I don’t know how to handle these as custom actions. I tried in the custom actions to return “utter\_ask\_slot” as names for the actions but it doesn’t seem to work. I get the following error for each slot (below the error for the slot first\_name)

`rasa.core.actions.action - Couldn't create message for response 'utter_ask_first_name'.`

And for your reply I tried to mark as solution but I couldn’t find the button, don’t know why, that’s why I just liked ❤ it

---

<div class="post-metadata">

**Author:** ![ChrisRahme](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/chrisrahme/32/15572_2.png) [@ChrisRahme](https://forum.rasa.com/u/ChrisRahme)\
**Post date:** [August 19, 2021, 5:15pm UTC](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142/6 "2021-08-19T17:15:00Z")

</div>

Please create a new thread for your issue so that people can see it better… Unless this is the solution: 🙂

- Don’t name your action `utter_ask_first_name` but `action_ask_first_name`

---

<div class="post-metadata">

**Author:** ![amaelbogne](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/amaelbogne/32/13795_2.png) [@amaelbogne](https://forum.rasa.com/u/amaelbogne)\
**Post date:** [August 19, 2021, 5:22pm UTC](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142/7 "2021-08-19T17:22:53Z")

</div>

ok let me try that out

---

<div class="post-metadata">

**Author:** ![amaelbogne](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/amaelbogne/32/13795_2.png) [@amaelbogne](https://forum.rasa.com/u/amaelbogne)\
**Post date:** [August 19, 2021, 7:49pm UTC](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142/8 "2021-08-19T19:49:39Z")

</div>

Thanks It worked.

but after the first slot (first\_name) is filled the bot can’t accurately detect a language using that answer so in this case it brought me back to English.

Is there a way I can access the Tracker in my custom component so that I just use it to globally define a language slot after the user’s first message to the bot? Or any other way I can go around this ?

---

<div class="post-metadata">

**Author:** ![Amanda](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@Amanda](https://forum.rasa.com/u/Amanda)\
**Post date:** [September 9, 2021, 4:12pm UTC](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142/9 "2021-09-09T16:12:12Z")

</div>

Can you please share your code here(the code for the 2nd solution), I’m working on a project like yours and I didn’t know how to handle this problem

---

<div class="post-metadata">

**Author:** ![ChrisRahme](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/chrisrahme/32/15572_2.png) [@ChrisRahme](https://forum.rasa.com/u/ChrisRahme)\
**Post date:** [September 9, 2021, 4:18pm UTC](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142/10 "2021-09-09T16:18:01Z")

</div>

Sorry, I haven’t noticed your last message! Hope you have solved it by now.

And as Amanda requested, it would be awesome if you could share your solution since there is so little information available for both multilingual bots as well as custom components 🙂

---

<div class="post-metadata">

**Author:** ![amaelbogne](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/amaelbogne/32/13795_2.png) [@amaelbogne](https://forum.rasa.com/u/amaelbogne)\
**Post date:** [September 12, 2021, 10:34am UTC](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142/12 "2021-09-12T10:34:12Z")

</div>

Hi

actually a custom component is not defined the same way as a custom action. there are a set a methods that are required in the custom component’s class you are defining of which the Process() method which does all the magic. Here’s my code [language.py](https://forum.rasa.com/uploads/short-url/m0R4MKBwe5VRxjg138uhzx82jNX.py) (5.3 KB) .

I used google translation api for translation but looking at my process() method you can adapt to whatever translation api you’re using.

if you try to run my code it won’t work because you need my google translation api key, so just look at the process() method and adapt it.

Here’s a good ressource that helped me understand [custom components](https://rasa.com/blog/enhancing-rasa-nlu-with-custom-components/)

---

<div class="post-metadata">

**Author:** ![Amanda](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@Amanda](https://forum.rasa.com/u/Amanda)\
**Post date:** [September 12, 2021, 10:36am UTC](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142/13 "2021-09-12T10:36:29Z")

</div>

I will try it, thanks for your help 😊

---

<div class="post-metadata">

**Author:** ![amaelbogne](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/amaelbogne/32/13795_2.png) [@amaelbogne](https://forum.rasa.com/u/amaelbogne)\
**Post date:** [September 12, 2021, 10:39am UTC](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142/14 "2021-09-12T10:39:26Z")

</div>

You’re welcome. I hope it’ll help

feel free to ask questions if anything seems blurry

---

<div class="post-metadata">

**Author:** ![Rishabhh17](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/rishabhh17/32/16105_2.png) [@Rishabhh17](https://forum.rasa.com/u/Rishabhh17)\
**Post date:** [September 20, 2021, 7:02pm UTC](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142/15 "2021-09-20T19:02:00Z")

</div>

@amaelbogne Can you help me ? I want to make a bot in Arabic as well as in English , I have my English data is ready and bot is trained in English but now I want to just use that English data and translate that into Arabic and give response in Arabic

---

<div class="post-metadata">

**Author:** ![Amanda](https://avatars.discourse-cdn.com/v4/letter/a/67e7ee/32.png) [@Amanda](https://forum.rasa.com/u/Amanda)\
**Post date:** [October 2, 2021, 8:54pm UTC](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142/16 "2021-10-02T20:54:44Z")

</div>

you can use language.py posted by @amaelbogne , in another file create a function to do the translation (I used easyNMT), call this function in language.py so it can traslate user’s input to english after that in actions.py use the entity “language” already defined in language.py to detect user’s message language and call the translation function to do the job, don’t forget to change your pipeline

---

<div class="post-metadata">

**Author:** ![ScienceGuy](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@ScienceGuy](https://forum.rasa.com/u/ScienceGuy)\
**Post date:** [December 1, 2021, 6:55pm UTC](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142/17 "2021-12-01T18:55:09Z")

</div>

Concerning your second approach (translation), I think this is a technical solution but not one that users will find attractive. In my project, we have to support English, Dutch, and Italian. Plain translation will almost certainly end in bad conversations because the translations do not consider context.

Also, I assume the way people talk in their language is not the same as how people talk in English (natively). So instead if solving the technical issue only, the real problem is that training data among languages are not a 1-to-1 translation. Would love to hear other opinions on this.

---

<div class="post-metadata">

**Author:** ![amaelbogne](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/amaelbogne/32/13795_2.png) [@amaelbogne](https://forum.rasa.com/u/amaelbogne)\
**Post date:** [December 2, 2021, 5:35am UTC](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142/18 "2021-12-02T05:35:07Z")

</div>

Hi Rishabhh17, Sorry for not getting back to you. Did @Amanda 's indication work for you? If No please could you share with us how you solved the problem, and if no let me know so I can help

---

<div class="post-metadata">

**Author:** ![amaelbogne](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/amaelbogne/32/13795_2.png) [@amaelbogne](https://forum.rasa.com/u/amaelbogne)\
**Post date:** [December 2, 2021, 5:44am UTC](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142/19 "2021-12-02T05:44:20Z")

</div>

Hi @ScienceGuyI don’t necessarily think using plain translation ends in bad conversations. translation APIs are doing a great job nowadays (even without context, which is a good remark though). Best way to know is to ship the assistant and get real user feedback.

Would love to know your approach on handling the multilingual aspect of your assistant

---

<div class="post-metadata">

**Author:** ![ChrisRahme](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/chrisrahme/32/15572_2.png) [@ChrisRahme](https://forum.rasa.com/u/ChrisRahme)\
**Post date:** [December 2, 2021, 6:44am UTC](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142/20 "2021-12-02T06:44:38Z")

</div>

> [@ScienceGuy](#):
>
> Plain translation will almost certainly end in bad conversations because the translations do not consider context.

I agree, and this is why I didn’t go with this approach for my bot 🙂

But as @amaelbogne, translators are getting better and I don’t think it would be that much of a huge deal.

On the downside, good translation APIs are often limited and/or paid, like Google’s (I think).

---

<div class="post-metadata">

**Author:** ![amaelbogne](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/amaelbogne/32/13795_2.png) [@amaelbogne](https://forum.rasa.com/u/amaelbogne)\
**Post date:** [December 2, 2021, 7:13am UTC](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142/21 "2021-12-02T07:13:23Z")

</div>

Which approach did you use please ?

[Next page](https://forum.rasa.com/t/mutlilingual-bot-capturing-user-input-and-intercepting-bots-output-for-translation/46142.md?page=2)
