Skip to content

Commit

Permalink
Generate translations for locales via built-in Java functionality
Browse files Browse the repository at this point in the history
Closes keycloak#29124

Signed-off-by: Alexander Schwartz <aschwart@redhat.com>
  • Loading branch information
ahus1 committed Apr 28, 2024
1 parent a6e2ab5 commit fd99bea
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 62 deletions.
2 changes: 1 addition & 1 deletion js/apps/admin-ui/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export const generateId = () => Math.floor(Math.random() * 1000);
export const localeToDisplayName = (locale: string, displayLocale: string) => {
try {
return new Intl.DisplayNames([displayLocale], { type: "language" }).of(
locale,
locale === "zh-CN" ? "zh-HANS" : locale === "zh-TW" ? "zh-HANT" : locale,
);
} catch (error) {
return locale;
Expand Down
7 changes: 4 additions & 3 deletions js/libs/ui-shared/src/user-profile/LocaleSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ export const LocaleSelector = ({
supportedLocales,
}: LocaleSelectorProps) => {
const locales = supportedLocales.map((locale) => ({
key: locale,
value: localeToDisplayName(locale) || "",
}));
key: locale,
value: t("locale_" + locale, localeToDisplayName(locale) || locale),
}))
.sort((a, b) => a.value.localeCompare(b.value));
if (!locales.length) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
import java.util.ListIterator;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.keycloak.common.Profile;
import org.keycloak.services.util.LocaleUtil;

/**
Expand Down Expand Up @@ -266,6 +266,7 @@ private Map<Locale, Properties> getMessagesByLocale(String baseBundlename, Local
}
}

addlocaleTranslations(locale, currentMessages);

this.messages.putIfAbsent(baseBundlename, new ConcurrentHashMap<>());
this.messages.get(baseBundlename).putIfAbsent(locale, groupedMessages);
Expand All @@ -276,6 +277,32 @@ private Map<Locale, Properties> getMessagesByLocale(String baseBundlename, Local
}
}

protected void addlocaleTranslations(Locale locale, Properties m) throws IOException {
for (String l : getProperties().getProperty("locales", "").split(",")) {
l = l.trim();
String key = "locale_" + l;
String label = m.getProperty(key);
if (label == null) {
String rl = l;
if (l.equals("zh-CN")) {
rl = "zh-HANS";
} else if (l.equals("zh-TW")) {
rl = "zh-HANT";
}
Locale loc = Locale.forLanguageTag(rl);
label = capitalize(loc.getDisplayName(locale), locale);
if (!Objects.equals(loc, locale)) {
label += " (" + capitalize(loc.getDisplayName(loc), loc) + ")";
}
m.put(key, label);
}
}
}

private String capitalize(String name, Locale locale) {
return name.substring(0, 1).toUpperCase(locale) + name.substring(1);
}

@Override
public Properties getProperties() throws IOException {
if (properties == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.keycloak.models.RealmModel;

import jakarta.ws.rs.core.UriBuilder;
import java.text.Collator;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
Expand All @@ -37,12 +38,16 @@ public LocaleBean(RealmModel realm, java.util.Locale current, UriBuilder uriBuil
this.currentLanguageTag = current.toLanguageTag();
this.current = messages.getProperty("locale_" + this.currentLanguageTag, this.currentLanguageTag);

Collator collator = Collator.getInstance(current);
collator.setStrength(Collator.PRIMARY); // ignore case and accents

supported = realm.getSupportedLocalesStream()
.map(l -> {
String label = messages.getProperty("locale_" + l, l);
String url = uriBuilder.replaceQueryParam("kc_locale", l).build().toString();
return new Locale(l, label, url);
})
.sorted((o1, o2) -> collator.compare(o1.label, o2.label))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public String getLanguageDropdownText() {
}

public void openLanguage(String language){
WebElement langLink = localeDropdown.findElement(By.xpath("//a[text()='" + language + "']"));
WebElement langLink = localeDropdown.findElement(By.xpath("//a[text()[contains(.,'" + language + "')]]"));
String url = langLink.getAttribute("href");
DroneUtils.getCurrentDriver().navigate().to(url);
WaitUtils.waitForPageToLoad();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public void switchLocale() throws MalformedURLException {

try {
checkPageNotFound("/auth/realms/master/nosuch");
String url = driver.findElement(By.xpath("//a[text()='Deutsch']")).getAttribute("href");
String url = driver.findElement(By.xpath("//a[text()[contains(.,'Deutsch')]]")).getAttribute("href");
driver.navigate().to(url);
errorPage.assertCurrent();
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,33 +272,6 @@ havePermissionRequestsWaitingForApproval=You have {0} permission request(s) wait
clickHereForDetails=Click here for details.
resourceIsNotBeingShared=The resource is not being shared

locale_ar=عربي
locale_ca=Català
locale_cs=Čeština
locale_de=Deutsch
locale_en=English
locale_es=Español
locale_fr=Français
locale_hu=Magyar
locale_fa=فارسی
locale_it=Italiano
locale_ja=日本語
locale_lt=Lietuvių
locale_nl=Nederlands
locale_no=Norsk
locale_pl=Polski
locale_pt=Português
locale_pt-BR=Português (Brasil)
locale_ru=Русский
locale_sk=Slovenčina
locale_sv=Svenska
locale_th=ไทย
locale_tr=Türkçe
locale_uk=Українська
locale_zh-CN=中文简体
locale_zh-TW=繁體中文
locale_fi=Suomi

# Applications
applicationName=Name
applicationType=Application Type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,34 +371,6 @@ confirmAccountLinking=Confirm linking the account {0} of identity provider {1} w
confirmEmailAddressVerification=Confirm validity of e-mail address {0}.
confirmExecutionOfActions=Perform the following action(s)

locale_ar=عربي
locale_ca=Català
locale_cs=Čeština
locale_da=Dansk
locale_de=Deutsch
locale_en=English
locale_es=Español
locale_fi=Suomi
locale_fr=Français
locale_hu=Magyar
locale_it=Italiano
locale_ja=日本語
locale_lt=Lietuvių
locale_lv=Latviešu
locale_nl=Nederlands
locale_no=Norsk
locale_pl=Polski
locale_pt=Português
locale_pt_BR=Português (Brasil)
locale_ru=Русский
locale_sk=Slovenčina
locale_sv=Svenska
locale_th=ไทย
locale_tr=Türkçe
locale_uk=Українська
locale_zh-CN=中文简体
locale_zh-TW=繁體中文

backToApplication=&laquo; Back to Application
missingParameterMessage=Missing parameters\: {0}
clientNotFoundMessage=Client not found.
Expand Down

0 comments on commit fd99bea

Please sign in to comment.