Truemag

  • Technology
  • Music
  • Travel
  • EPL
  • About Me
Stack Overflow




Quickly build a Reactive web app with Play and Watson Language Translator

IBM and Lightbend Partnership

I am really excited to learn about IBM and Lightbend partnership. The integration of IBM and Lightbend technology will open up a plethora of opportunities for developers to build fast, scalable, cognitive and AI-based applications. Lightbend technologies can be combined with Watson services to create highly scalable intelligent apps.

Play Framework

Play framework is a Lightbend technology to create a reactive REST based web applications. This framework allows you to quickly create a high velocity, modern, scalable web applications in no time. You can use SBT which is a faster way of creating, building and running your projects from scratch. SBT creates a project based on the predefined template and allows you to scale to your needs. Both of these technologies are easy to learn and can get you started in no time.

Let’s Create a Project

For this demo you need following prerequisites:
  • Java 1.5 or later
  • Scala
  • sbt installed in your path. For mac
1
brew install sbt

Creating new Project

Using sbt you can create a project and its folder structure in no time. sbt is a build tool for Scala, Java and more
sbt new <.g8 template name> – I have chosen a scala skeleton
1
sbt new playframework/play-scala-seed.g8
sbt new

sbt new

Build and Run

1
2
sbt compile -  will compile the project
sbt run - will run the project
 This will build and run your project which is accessible from http://localhost:9000
This will give you a page that has all the information about your project and what you can do next.
That’s it! you have created a reactive based project in scala in no time.
Welcome Page

Welcome Page

Integrating Watson Language Translator

To integrate Language translator to the framework create a service Language Translator from IBM Bluemix. You need to have an account in IBM Bluemix to create this service. It provides an REST-ful API to interact with the service. The easy way to test the language translator API is using following curl command before integrating the language translator API into your reactive web application using Play framework
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
curl -X POST --user "&lt;username&gt;":"password" --header "Content-Type: application/json" --header "Accept: application/json"
--data '{"text":"Hello, world!","source":"en","target":"es"}'
"https://gateway.watsonplatform.net/language-translator/api/v2/translate"
 
output:
 
{
"translations":[
{
"translation":"Hola, mundo!"
}
],
"word_count":2,
"character_count":13
}
From the reactive web application, we can create a service called WatsonLanguageTranslator which uses a function to make a REST based web service call. The call uses the credentials to authenticate and get the response in JSON. Authentication Credentials can be stored in application.conf as below.
1
2
3
4
5
6
7
8
9
#credentials for Watson Language Translator
languageTranslator {
  url: "https://gateway.watsonplatform.net/language-translator/api/v2/translate",
  username: "&lt;username&gt;",
  password: "&lt;password&gt;"
},
#disabling security filters for now
play.filters.disabled+=play.filters.csrf.CSRFFilter
play.filters.disabled+=play.filters.hosts.AllowedHostsFilter
Class to make REST call to Watson Language Translator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package services
import javax.inject._
import java.util.concurrent.TimeUnit
import com.typesafe.play.cachecontrol.ResponseSelectionActions.GatewayTimeout
import play.api.libs.ws._
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, ExecutionContext, Future}
 
@Singleton
class WatsonLanguageTranslator @Inject() ( implicit ec: ExecutionContext,ws: WSClient, configuration: play.api.Configuration) {
 
    val url= configuration.underlying.getString("languageTranslator.url")
    val username= configuration.underlying.getString("languageTranslator.username")
    val password=configuration.underlying.getString("languageTranslator.password")
 
  def getTranslatedTextFromWatson(text: String, translateFrom:String, translateTo: String) : String = {
    import play.api.libs.ws._
    import play.api.libs.json._
 
    val data = Json.obj(
      "text" -&gt; text,
      "source" -&gt; translateFrom,
      "target" -&gt; translateTo
    )
 
    val request: WSRequest = ws.url(url)
      .addHttpHeaders("Accept" -&gt; "application/json")
      .addHttpHeaders("Content-Type" -&gt; "application/json")
 
    val futureResponse: Future[String] = request
      .withAuth(username, password, WSAuthScheme.BASIC)
      .post(data)
      .map {
        response =&gt; (response.json \ "translations" \\ "translation").map(_.as[String]).head
      }
 
    val response = Await.result(futureResponse,Duration(1,TimeUnit.seconds))
    response
  }
}

Demo

Translate Page

Translate Page

Output after form submission

Translation Output

Translation Output

Github

Find the sample code here in Github.

Reference Links

  • http://developer.lightbend.com/start/
  • https://www.playframework.com/
  • https://www.ibm.com/cloud-computing/bluemix/getting-started
  • https://www.ibm.com/watson/
  • https://www.ibm.com/watson/services/language-translator/
Jun 29, 2017sanjeevghimire
Create a Home Search ChatBot using Swift and Watson servicesControlling Raspberry PI powered Robot using Kitura
You Might Also Like
 
Serverless chat application using OpenWhisk, Docker, Scala and IBM Cloudant
 
Building scalable and resilient web application using Akka
Comments: 0

Leave a Reply Cancel reply

Profile photo of sanjeevghimire
sanjeevghimire

Sanjeev Ghimire is passionate about problem-solving through technology. With more than a decade of software engineering experience ranging from fin-tech to healthcare, Sanjeev was also the CTO and co-founder of onTarget. These days, Sanjeev is a Developer Advocate with IBM focused on emerging technologies such as blockchain, IoT, and AR/VR. When not cranking away in Java, Swift, Python, or Scala, you can find Sanjeev at the gym, playing the drums, or catching an Arsenal F.C. (a.k.a. Gunners) soccer match.

Image1 year ago 1 Comment TechnologyIBM-Lightbend-partnership, java, Language translator, play-framework, reactive-programming, Scala, Watson1,088







2017 © Sanjeev Ghimire