Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit 2efc030

Browse files
Fix bugs around inconsistent locale (#1074)
* DialogContext.getLocale port from C# * BotAdapter updates for locale. Co-authored-by: tracyboehrer <tracyboehrer@users.noreply.github.com>
1 parent 5d3d1c3 commit 2efc030

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/BotAdapter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.microsoft.bot.schema.ResourceResponse;
1111

1212
import java.util.List;
13+
import java.util.Locale;
1314
import java.util.concurrent.CompletableFuture;
1415
import java.util.concurrent.CompletionException;
1516
import org.apache.commons.lang3.NotImplementedException;
@@ -196,6 +197,9 @@ protected CompletableFuture<Void> runPipeline(
196197
// Call any registered Middleware Components looking for ReceiveActivity()
197198
if (context.getActivity() != null) {
198199
if (!StringUtils.isEmpty(context.getActivity().getLocale())) {
200+
201+
Locale.setDefault(Locale.forLanguageTag(context.getActivity().getLocale()));
202+
199203
context.setLocale(context.getActivity().getLocale());
200204
}
201205

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/TurnContext.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* {@link Bot} {@link Middleware}
2525
*/
2626
public interface TurnContext {
27+
String STATE_TURN_LOCALE = "turn.locale";
28+
2729
/**
2830
* Sends a trace activity to the {@link BotAdapter} for logging purposes.
2931
*

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/TurnContextImpl.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ public class TurnContextImpl implements TurnContext, AutoCloseable {
6767
*/
6868
private Boolean responded = false;
6969

70-
private static final String STATE_TURN_LOCALE = "turn.locale";
71-
7270
/**
7371
* Creates a context object.
7472
*

libraries/bot-builder/src/test/java/com/microsoft/bot/builder/BotAdapterTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,27 @@ public void PassResourceResponsesThrough() {
4747
);
4848
}
4949

50+
@Test
51+
public void GetLocaleFromActivity() {
52+
Consumer<List<Activity>> validateResponse = (activities) -> {
53+
// no need to do anything.
54+
};
55+
SimpleAdapter a = new SimpleAdapter(validateResponse);
56+
TurnContextImpl c = new TurnContextImpl(a, new Activity(ActivityTypes.MESSAGE));
57+
58+
String activityId = UUID.randomUUID().toString();
59+
Activity activity = TestMessage.Message();
60+
activity.setId(activityId);
61+
activity.setLocale("de-DE");
62+
BotCallbackHandler callback = (turnContext) -> {
63+
Assert.assertEquals("de-DE", turnContext.getActivity().getLocale());
64+
return CompletableFuture.completedFuture(null);
65+
};
66+
67+
a.processRequest(activity, callback).join();
68+
}
69+
70+
5071
@Test
5172
public void ContinueConversation_DirectMsgAsync() {
5273
boolean[] callbackInvoked = new boolean[] { false };

libraries/bot-dialogs/src/main/java/com/microsoft/bot/dialogs/DialogContext.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.microsoft.bot.connector.Async;
1111
import java.util.HashMap;
1212
import java.util.List;
13+
import java.util.Locale;
1314
import java.util.concurrent.CompletableFuture;
1415
import org.apache.commons.lang3.StringUtils;
1516

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

531532
/**
532-
* Obtain the CultureInfo in DialogContext.
533+
* Obtain the locale in DialogContext.
533534
* @return A String representing the current locale.
534535
*/
535536
public String getLocale() {
536-
return getContext() != null ? getContext().getLocale() : null;
537-
}
537+
// turn.locale is the highest precedence.
538+
String locale = (String) state.get(TurnContext.STATE_TURN_LOCALE);
539+
if (!StringUtils.isEmpty(locale)) {
540+
return locale;
541+
}
542+
543+
// If turn.locale was not populated, fall back to activity locale
544+
locale = getContext().getActivity().getLocale();
545+
if (!StringUtils.isEmpty(locale)) {
546+
return locale;
547+
}
538548

549+
return Locale.getDefault().toString();
550+
}
539551

540552
/**
541553
* @param reason
@@ -545,7 +557,6 @@ private CompletableFuture<Void> endActiveDialog(DialogReason reason) {
545557
return endActiveDialog(reason, null);
546558
}
547559

548-
549560
/**
550561
* @param reason
551562
* @param result

0 commit comments

Comments
 (0)