Search code examples
javaeclipseamazon-web-servicesaws-lambdaalexa-voice-service

How to create a Multi-Language Alexa Skill using Java?


I'm quite new to VUI and Alexa. I developed a good working Skill for Alexa Voice Services. Now I want to add a second language. I've found a tutorial in the develop documation, but I'm using Eclipse and Java to create my Lambda function like this walkthrough. The problem is, that I have no idea how to enable a second language option in the Lambda function. I have to use the same Lambda function for both languages.

My StreamRequestHandler:

public class ApiOmatBlogSpeechletStreamRequestHandler extends SpeechletRequestStreamHandler {
private static final Set<String> supportedApplicationIds = new HashSet<String>();
static {
    /*
     * This Id can be found on https://developer.amazon.com/edw/home.html#/ "Edit" the relevant
     * Alexa Skill and put the relevant Application Ids in this Set.
     */
    supportedApplicationIds.add("amzn1.ask.skill.xxxxxxxx");
}

public ApiOmatBlogSpeechletStreamRequestHandler() {
    super(new ApiOmatBlogSkillSpeechlet(), supportedApplicationIds);
    System.out.println("Super ApiOmatBlogSpeechletStreamRequestHandler");
}

}

My Spechlet:

public SpeechletResponse onIntent(IntentRequest intentRequest, Session session) {
    Intent intent = intentRequest.getIntent();
    String intentName = (intent != null) ? intent.getName() : null;
    System.out.println("onIntent requestId={ " + intentRequest.getRequestId() + " }, sessionId={ "
            + session.getSessionId() + " } ");

    Integer step = (Integer) session.getAttribute("step");
    System.out.println("IntentName= " + intentName + " | step = " + step);

    if ("AMAZON.HelpIntent".equals(intentName)) {
        return getHelpResponse();
    } else if ("AMAZON.StopIntent".equals(intentName)) {
        return getStopResponse();
    } else if (step != null) {
        return testing(intent, session, step);
    } else {
        if ("TestIntent".equals(intentName)) {
            step = 1;
            session.setAttribute("step", step);
            return testing(intent, session, step);
        } else {
            SsmlOutputSpeech speechText = new SsmlOutputSpeech();

            speechText.setSsml("<speak> " + "The intent is invalid." + "Please repeat your demand. "
                    + "<break time='0.5s'/> " + "  </speak>");

            // Create reprompt
            PlainTextOutputSpeech speech2 = new PlainTextOutputSpeech();
            speech2.setText("I'm sorry. Please repeat your statement.");
            Reprompt reprompt = new Reprompt();
            reprompt.setOutputSpeech(speech2);

            return SpeechletResponse.newAskResponse(speechText, reprompt);
        }
    }
}

/**
 * This function will be called if you say 'start'
 */
public SpeechletResponse onLaunch(final LaunchRequest request, final Session session) throws SpeechletException {
    System.out.println(
            "onLaunch requestId={ " + request.getRequestId() + " }, sessionId={ " + session.getSessionId() + " } ");

    return getHelpResponse();
}

Thank you


Solution

  • It's quite difficult to settle it up. I solved it by asking, which language the user speaks.

    In the Spechlet.java I added the code:

    private Boolean german;
        public SpeechletResponse onIntent(IntentRequest intentRequest, Session session) {
            if (intentRequest.getLocale().getLanguage().equals(new Locale("de").getLanguage())){
                System.out.println("Spoken language == Deutsch");
                german=true;
            }else{
                System.out.println("Spoken language == Englisch");
                german=false;
            }
    ...
    }
    

    and follwing I can work with the Boolean german and set up my response.

    If there is a better solution, which works on two languages, than I would looking forward to a posted answer or comment. But for the moment this is the best workaround to handle multiple language in Java.