Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.microsoft.bot.schema.ResourceResponse;

import java.util.List;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import org.apache.commons.lang3.NotImplementedException;
Expand Down Expand Up @@ -196,6 +197,9 @@ protected CompletableFuture<Void> runPipeline(
// Call any registered Middleware Components looking for ReceiveActivity()
if (context.getActivity() != null) {
if (!StringUtils.isEmpty(context.getActivity().getLocale())) {

Locale.setDefault(Locale.forLanguageTag(context.getActivity().getLocale()));

context.setLocale(context.getActivity().getLocale());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* {@link Bot} {@link Middleware}
*/
public interface TurnContext {
String STATE_TURN_LOCALE = "turn.locale";

/**
* Sends a trace activity to the {@link BotAdapter} for logging purposes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public class TurnContextImpl implements TurnContext, AutoCloseable {
*/
private Boolean responded = false;

private static final String STATE_TURN_LOCALE = "turn.locale";

/**
* Creates a context object.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ public void PassResourceResponsesThrough() {
);
}

@Test
public void GetLocaleFromActivity() {
Consumer<List<Activity>> validateResponse = (activities) -> {
// no need to do anything.
};
SimpleAdapter a = new SimpleAdapter(validateResponse);
TurnContextImpl c = new TurnContextImpl(a, new Activity(ActivityTypes.MESSAGE));

String activityId = UUID.randomUUID().toString();
Activity activity = TestMessage.Message();
activity.setId(activityId);
activity.setLocale("de-DE");
BotCallbackHandler callback = (turnContext) -> {
Assert.assertEquals("de-DE", turnContext.getActivity().getLocale());
return CompletableFuture.completedFuture(null);
};

a.processRequest(activity, callback).join();
}


@Test
public void ContinueConversation_DirectMsgAsync() {
boolean[] callbackInvoked = new boolean[] { false };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.microsoft.bot.connector.Async;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;
import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -529,13 +530,24 @@ public CompletableFuture<Boolean> emitEvent(String name, Object value, boolean b
}

/**
* Obtain the CultureInfo in DialogContext.
* Obtain the locale in DialogContext.
* @return A String representing the current locale.
*/
public String getLocale() {
return getContext() != null ? getContext().getLocale() : null;
}
// turn.locale is the highest precedence.
String locale = (String) state.get(TurnContext.STATE_TURN_LOCALE);
if (!StringUtils.isEmpty(locale)) {
return locale;
}

// If turn.locale was not populated, fall back to activity locale
locale = getContext().getActivity().getLocale();
if (!StringUtils.isEmpty(locale)) {
return locale;
}

return Locale.getDefault().toString();
}

/**
* @param reason
Expand All @@ -545,7 +557,6 @@ private CompletableFuture<Void> endActiveDialog(DialogReason reason) {
return endActiveDialog(reason, null);
}


/**
* @param reason
* @param result
Expand Down