Skip to content

Commit

Permalink
CLDR-17425 Fix bugs involving locale USER (#3572)
Browse files Browse the repository at this point in the history
  • Loading branch information
btangmu authored Mar 21, 2024
1 parent e9f86d0 commit c7e39f1
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 21 deletions.
3 changes: 3 additions & 0 deletions tools/cldr-apps/js/src/esm/cldrLoad.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tools/cldr-apps/js/src/esm/cldrMenu.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 7 additions & 2 deletions tools/cldr-apps/js/src/esm/cldrStatus.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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<DataRow> dataRows = pageData.getAll();
Expand Down

0 comments on commit c7e39f1

Please sign in to comment.