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

如何使用Java创建多语言Alexa技能?,java,eclipse,amazon-web-services,aws-lambda,alexa-voice-service,Java,Eclipse,Amazon Web Services,Aws Lambda,Alexa Voice Service,我是VUI和Alexa的新手。我为Alexa Voice Services培养了良好的工作技能。现在我想添加第二种语言。我在中找到了一个教程,但我正在使用Eclipse和Java创建我的Lambda函数,如下所示。 问题是,我不知道如何在Lambda函数中启用第二语言选项。我必须对两种语言使用相同的Lambda函数 我的StreamRequestHandler: public class ApiOmatBlogSpeechletStreamRequestHandler extends Speec

我是VUI和Alexa的新手。我为Alexa Voice Services培养了良好的工作技能。现在我想添加第二种语言。我在中找到了一个教程,但我正在使用Eclipse和Java创建我的Lambda函数,如下所示。 问题是,我不知道如何在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");
}
公共类ApiOmatBlogSpeechletStreamRequestHandler扩展SpeechletRequestStreamHandler{
私有静态最终集SupportedApplicationId=new HashSet();
静止的{
/*
*可以在上找到此Idhttps://developer.amazon.com/edw/home.html#/ “编辑”相关文件
*Alexa Skill并将相关的应用程序ID放入该集中。
*/
支持的应用程序ID.add(“amzn1.ask.skill.xxxxxxxx”);
}
公共ApiOmatBlogSpeechletStreamRequestHandler(){
super(新的ApiOmatBlogSkillSpeechlet(),支持的应用程序ID);
System.out.println(“超级ApiOmatBlogSpeechletStreamRequestHandler”);
}
}

我的说明书:

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();
}
public SpeechletResponse onIntent(IntentRequest IntentRequest,Session){
Intent-Intent=intentRequest.getIntent();
字符串intentName=(intent!=null)?intent.getName():null;
System.out.println(“onIntent requestId={”+intentRequest.getRequestId()+“},sessionId={”
+session.getSessionId()+“}”);
整数步长=(整数)session.getAttribute(“步长”);
System.out.println(“IntentName=“+IntentName+”| step=“+step”);
if(“AMAZON.HelpIntent.equals(intentName)){
返回getHelpResponse();
}else if(“AMAZON.StopIntent.equals(intentName)){
返回getStopResponse();
}else if(步骤!=null){
返回测试(意图、会话、步骤);
}否则{
if(“TestIntent”.equals(intentName)){
步骤=1;
session.setAttribute(“步骤”,步骤);
返回测试(意图、会话、步骤);
}否则{
SSMLOutputSpeechText=新SSMLOutputSpeechText();
speechText.setSsml(“+”意图无效。“+”请重复您的要求。“
+ " " + "  ");
//创建复制
PlainTextOutputSpeech2=新的PlainTextOutputSpeech();
setText(“对不起,请重复您的发言。”);
Reprompt Reprompt=新的Reprompt();
reprompt.setOutputSpeech(speech2);
返回SpeechletResponse.newAskResponse(speechText,reprompt);
}
}
}
/**
*如果您说“开始”,将调用此函数
*/
public SpeechletResponse onLaunch(最终启动请求,最终会话会话)引发SpeechletException{
System.out.println(
“onLaunch requestId={”+请求.getRequestId()+“},sessionId={”+会话.getSessionId()+“}”);
返回getHelpResponse();
}

谢谢

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


请阅读官方文件,这件事很难解决。 我通过问用户说哪种语言来解决这个问题

在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中处理多种语言的最佳解决方案。

这是不对的,因为我创造了一种英语技能,可以用英语回答文本。现在我添加了第二种语言,德语。开发者门户不允许我插入第二个lambda函数。我必须对英语和德语使用相同的功能。因为我不想在德语模式下得到英语答案,所以我必须决定该技能是用德语还是用英语。如果您阅读了我提供的文档链接,您可能会注意到一行“代码必须处理您支持的所有语言”,这正是关键所在!但是只有一个Javascript文档,没有Java文档,所以我如何在Lambda函数中划分语言,正如我在问题中所问的:“问题是,我不知道如何在Lambda函数中启用第二语言选项……”不幸的是,我从来没有为德语做过Alexa。我只为美国、英国和印度做了这件事,而且都是用英语做的。如果你能特别提到“德语”,那就太好了。另外,由于我的回答对你没有帮助,我将删除它。我认为第二语言(美国、英国、印度或德语)应该没有区别。事实是,你必须认识到哪种语言在使用,哪种文本是回应。我正在寻找这个决策者,它决定用德语或英语文本回应,也决定使用哪个函数。