Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLDR-17425 Fix bugs involving locale USER #3572

Merged
merged 1 commit into from
Mar 21, 2024
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
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('-', '_');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this line ? or maybe it should also go into a utility function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have to assume it's needed... ideally it should be encapsulated in a module specific to locales, not SurveyAjax; and as much as possible we should pass around CLDRLocale objects instead of strings...

/*
* 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
Loading