如何使用 Java 创建多语言 Alexa 技能?

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

我对 VUI 和 Alexa 很陌生。我为 Alexa 语音服务培养了良好的工作技能。现在我想添加第二语言。我在 the develop documation, but I'm using Eclipse and Java to create my Lambda function like this walkthrough 中找到了教程。 问题是,我不知道如何在 Lambda 函数中启用第二语言选项。我必须对两种语言使用相同的 Lambda 函数。

我的 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");
}

}

我的 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();
}

谢谢

您不需要不同的 Lambda 逻辑来支持每种语言。您可以根据区域创建多个 Lambda 函数以避免延迟。如果您选择将代码部署到多个端点,则必须在所有端点维护相同的代码。该代码必须处理您支持的所有语言。您唯一需要做的设置是在 Alexa 技能信息中添加 Alexa 支持的语言,如下所示,

请阅读官方文档here

很难解决。 我通过询问用户使用哪种语言解决了这个问题。

在Spechlet.java我添加了代码:

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;
        }
...
}

接下来我可以使用布尔德语并设置我的回复。

如果有更好的解决方案,适用于两种语言,我会期待发布的答案或评论。但目前这是在 Java.

中处理多种语言的最佳解决方法