Add the Watson service

You now have your shell application and can add the Watson service.

    1. In Bluemix, add or bind the Watson Language Translation service. You don’t need to restart the application for now.

      Now, you see the service credentials.

 

    1. In the wl.py file, import the language translation component from the watson_developer_cloud SDK:# -*- coding: utf-8 -*-

      from django.shortcuts import render

      from django import forms

      from watson_developer_cloud import LanguageTranslationV2 as LanguageTranslation

      from watson_developer_cloud import WatsonException

      import logging

      import json

      The final change is in the placeholder that you created in the wl.py file. Remember to use the service credentials that you got for Bluemix as the language translation user name and password.

 

    1. Open the wl.ph file and find this code:if form.is_valid():

         data = form.cleaned_data['txtdata']

         logger.info("Text to be processed is %s " % data) 

         lang = "TBD"

        

         allinfo['lang'] = lang

 

  1. Add code that invokes the language detection service after the line lang = “TBD” and before the line allinfo[‘lang’] = lang:
    lang = "TBD"

    [Add code here]

    allinfo['lang'] = lang

     

        if form.is_valid():

          data = form.cleaned_data['txtdata']

          logger.info("Text to be processed is %s " % data) 

          lang = "TBD"

          try:

            language_translation = LanguageTranslation(username='<user>',

                                                       password='<pwd>')

     

            langsdetected = language_translation.identify(data)                                                          

            logger.info(json.dumps(langsdetected, indent=2)) 

            logger.info(langsdetected["languages"][0]['language'])   

            logger.info(langsdetected["languages"][0]['confidence'])      

     

            primarylang = langsdetected["languages"][0]['language']

            confidence = langsdetected["languages"][0]['confidence']

             

            lang = "I am %s confident that it is %s" % (confidence, primarylang)

          except WatsonException as err:

           allinfo['error'] = err;

          

          allinfo['lang'] = lang

        else:

          allinfo['error'] = "Form is invalid"