diff --git a/tools/cldr-apps/js/src/esm/cldrLoad.mjs b/tools/cldr-apps/js/src/esm/cldrLoad.mjs index ab4d2c9d2d1..bc363df18dc 100644 --- a/tools/cldr-apps/js/src/esm/cldrLoad.mjs +++ b/tools/cldr-apps/js/src/esm/cldrLoad.mjs @@ -872,6 +872,9 @@ function loadAllRowsFromJson(json, theDiv) { if (json.dataLoadTime) { cldrDom.updateIf("dynload", json.dataLoadTime); } + if (json.loc) { + cldrStatus.setCurrentLocale(json.loc); // may replace "USER" + } cldrStatus.setCurrentSection(""); cldrStatus.setCurrentPage(json.pageId); updateHashAndMenus(); // now that we have a pageid diff --git a/tools/cldr-apps/js/src/esm/cldrMenu.mjs b/tools/cldr-apps/js/src/esm/cldrMenu.mjs index b86ea10349d..9f87b6b21fe 100644 --- a/tools/cldr-apps/js/src/esm/cldrMenu.mjs +++ b/tools/cldr-apps/js/src/esm/cldrMenu.mjs @@ -127,7 +127,7 @@ function addTopLocales(theDiv, locmap) { for (let n in locmap.locmap.topLocales) { const topLoc = locmap.locmap.topLocales[n]; const topLocInfo = locmap.getLocaleInfo(topLoc); - if (topLocInfo.special_type !== "scratch") { + if (topLocInfo?.special_type !== "scratch") { // Skip Sandbox locales here addTopLocale(topLoc, theDiv); } @@ -136,7 +136,7 @@ function addTopLocales(theDiv, locmap) { for (let n in locmap.locmap.topLocales) { const topLoc = locmap.locmap.topLocales[n]; const topLocInfo = locmap.getLocaleInfo(topLoc); - if (topLocInfo.special_type === "scratch") { + if (topLocInfo?.special_type === "scratch") { // Now only Sandbox locales here addTopLocale(topLoc, theDiv); } @@ -421,7 +421,7 @@ function updateLocaleMenu() { const locmap = cldrLoad.getTheLocaleMap(); cldrStatus.setCurrentLocaleName(locmap.getLocaleName(curLocale)); var bund = locmap.getLocaleInfo(curLocale); - if (bund.special_type === "scratch") { + if (bund?.special_type === "scratch") { prefixMessage = cldrText.get("scratch_locale") + ": "; } if (bund) { diff --git a/tools/cldr-apps/js/src/esm/cldrStatus.mjs b/tools/cldr-apps/js/src/esm/cldrStatus.mjs index b7444674116..da6b0d94d29 100644 --- a/tools/cldr-apps/js/src/esm/cldrStatus.mjs +++ b/tools/cldr-apps/js/src/esm/cldrStatus.mjs @@ -226,8 +226,13 @@ function getCurrentLocale() { function setCurrentLocale(loc) { currentLocale = loc; - dispatchEvent(new Event("locale")); - setRef("currentLocale", loc); + // loc may be "USER" temporarily, meaning the back end should choose an appropriate locale + // for the current user. The real locale ID should be set when a server response contains it. + // In the meantime, postpone calling dispatchEvent or setRef. + if ("USER" !== loc) { + dispatchEvent(new Event("locale")); + setRef("currentLocale", loc); + } } /** diff --git a/tools/cldr-apps/src/main/java/org/unicode/cldr/web/SurveyAjax.java b/tools/cldr-apps/src/main/java/org/unicode/cldr/web/SurveyAjax.java index cac7304942e..5af20d53db6 100644 --- a/tools/cldr-apps/src/main/java/org/unicode/cldr/web/SurveyAjax.java +++ b/tools/cldr-apps/src/main/java/org/unicode/cldr/web/SurveyAjax.java @@ -1055,21 +1055,7 @@ public static CLDRLocale validateLocale(PrintWriter out, String loc, String sess * loc can have either - or _; normalize to _ */ loc = loc.replace('-', '_'); - /* - * Support "USER" as a "wildcard" for locale name, replacing it with a locale suitable for the - * current user, or "fr" (French) as a fallback. - */ - if ("USER".equals(loc) && sess != null && !sess.isEmpty()) { - loc = "fr"; // fallback - CookieSession.checkForExpiredSessions(); - CookieSession mySession = CookieSession.retrieve(sess); - if (mySession.user != null) { - CLDRLocale exLoc = mySession.user.exampleLocale(); - if (exLoc != null) { - loc = exLoc.getBaseName(); - } - } - } + loc = UserRegistry.substituteUserWildcardLocale(loc, sess); if (loc != null && !loc.isEmpty()) { ret = CLDRLocale.getInstance(loc); } diff --git a/tools/cldr-apps/src/main/java/org/unicode/cldr/web/UserRegistry.java b/tools/cldr-apps/src/main/java/org/unicode/cldr/web/UserRegistry.java index f7782b56237..d7a2f67ef45 100644 --- a/tools/cldr-apps/src/main/java/org/unicode/cldr/web/UserRegistry.java +++ b/tools/cldr-apps/src/main/java/org/unicode/cldr/web/UserRegistry.java @@ -50,6 +50,25 @@ public class UserRegistry { */ private static final int ANONYMOUS_USER_COUNT = 20; + /* + * Support "USER" as a "wildcard" for locale name, replacing it with a locale suitable for the + * current user, or "fr" (French) as a fallback. + */ + public static String substituteUserWildcardLocale(String loc, String sess) { + if ("USER".equals(loc) && sess != null && !sess.isEmpty()) { + loc = "fr"; // fallback + CookieSession.checkForExpiredSessions(); + CookieSession mySession = CookieSession.retrieve(sess); + if (mySession.user != null) { + CLDRLocale exLoc = mySession.user.exampleLocale(); + if (exLoc != null) { + loc = exLoc.getBaseName(); + } + } + } + return loc; + } + /** * Thrown to indicate the caller should log out. * diff --git a/tools/cldr-apps/src/main/java/org/unicode/cldr/web/api/VoteAPI.java b/tools/cldr-apps/src/main/java/org/unicode/cldr/web/api/VoteAPI.java index 3ec5ee51b3d..544f482d433 100644 --- a/tools/cldr-apps/src/main/java/org/unicode/cldr/web/api/VoteAPI.java +++ b/tools/cldr-apps/src/main/java/org/unicode/cldr/web/api/VoteAPI.java @@ -232,6 +232,7 @@ public static final class DisplaySets { public DisplaySets displaySets; public JSONArray issues; + public String loc; // normally matches requested locale ID unless "USER" is requested public String localeDisplayName; public Dashboard.ReviewNotification[] notifications; public Page page; diff --git a/tools/cldr-apps/src/main/java/org/unicode/cldr/web/api/VoteAPIHelper.java b/tools/cldr-apps/src/main/java/org/unicode/cldr/web/api/VoteAPIHelper.java index 6f0287c80e6..1f5698a8553 100644 --- a/tools/cldr-apps/src/main/java/org/unicode/cldr/web/api/VoteAPIHelper.java +++ b/tools/cldr-apps/src/main/java/org/unicode/cldr/web/api/VoteAPIHelper.java @@ -69,7 +69,7 @@ public static class ArgsForGet { Boolean getDashboard = false; public ArgsForGet(String loc, String session) { - this.localeId = loc; + this.localeId = UserRegistry.substituteUserWildcardLocale(loc, session); this.sessionId = session; } } @@ -182,6 +182,7 @@ public static RowResponse getRowsResponse( r.page.nocontent = true; } else { r.canModify = UserRegistry.userCanModifyLocale(mySession.user, locale); + r.loc = locale.getBaseName(); r.localeDisplayName = locale.getDisplayName(); r.page.nocontent = false; Collection dataRows = pageData.getAll();