Skip to content

Commit

Permalink
Add translation service
Browse files Browse the repository at this point in the history
  • Loading branch information
janfaracik committed Jan 16, 2022
1 parent f918424 commit 013f733
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 deletions.
6 changes: 6 additions & 0 deletions core/src/main/java/hudson/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2337,6 +2337,12 @@ public static String tryGetIconPath(String iconGuess, JellyContext context) {
iconSource = rootURL + (iconGuess.startsWith("/images/") || iconGuess.startsWith("/plugin/") ? getResourcePath() : "") + getResourcePath() + iconGuess;
}

if (iconMetadata != null && iconMetadata.getClassSpec() != null) {
if (IconSet.tryTranslateTangoIconToSymbol(iconMetadata.getClassSpec()) != null) {
return IconSet.tryTranslateTangoIconToSymbol(iconMetadata.getClassSpec());
}
}

return iconSource;
}
}
28 changes: 27 additions & 1 deletion core/src/main/java/org/jenkins/ui/icon/IconSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ private static String prependTitleIfRequired(String icon, String title) {

// for Jelly
@Restricted(NoExternalUse.class)
public static String getSymbol(String name, String title) {
public static String getSymbol(String name, String title, String classes) {
if (SYMBOLS.containsKey(name)) {
String symbol = SYMBOLS.get(name);
symbol = symbol.replaceAll("(class=\")[^&]*?(\")", "$1$2");
symbol = symbol.replaceAll("<svg", "<svg class=\"" + classes + "\"");
return prependTitleIfRequired(symbol, title);
}

Expand All @@ -99,7 +101,9 @@ public static String getSymbol(String name, String title) {
}

symbol = symbol.replaceAll("(<title>)[^&]*(</title>)", "$1$2");
symbol = symbol.replaceAll("(class=\")[^&]*?(\")", "$1$2");
symbol = symbol.replaceAll("<svg", "<svg aria-hidden=\"true\"");
symbol = symbol.replaceAll("<svg", "<svg class=\"" + classes + "\"");
symbol = symbol.replace("stroke:#000", "stroke:currentColor");

SYMBOLS.put(name, symbol);
Expand Down Expand Up @@ -528,4 +532,26 @@ private static void initializeSVGs() {
}
}
}

/**
* This is a temporary function to replace Tango icons across Jenkins and plugins with
* appropriate Jenkins Symbols
*
* @param tangoIcon A tango icon in the format 'icon-* size-*', e.g. 'icon-gear icon-lg'
* @return a Jenkins Symbol (if one exists) otherwise null
*/
public static String tryTranslateTangoIconToSymbol(String tangoIcon) {
if (tangoIcon != null) {
tangoIcon = tangoIcon.split(" ")[0];
}

Map<String, String> translations = new HashMap<>();
translations.put("icon-clock", "symbol-play");
translations.put("icon-edit-delete", "symbol-trash");
translations.put("icon-gear", "symbol-settings");
translations.put("icon-gear2", "symbol-settings");
translations.put("icon-plugin", "symbol-plugins");

return translations.getOrDefault(tangoIcon, null);
}
}
33 changes: 17 additions & 16 deletions core/src/main/resources/lib/layout/icon.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,29 @@ THE SOFTWARE.
<st:attribute name="alt">alt</st:attribute>
</st:documentation>

<j:set var="iconSrc" value="${h.tryGetIconPath(src, context)}"/>
<j:set var="iconMetadata" value="${h.tryGetIcon(src)}"/>
<j:set var="imgStyle" value="${attrs.style}"/>

<j:if test="${iconSrc == null}">
<j:set var="iconSrc" value="${h.tryGetIconPath(class, context)}"/>
<j:set var="iconMetadata" value="${h.tryGetIcon(class)}"/>
</j:if>

<j:if test="${iconMetadata != null}">
<j:set var="imgStyle" value="${iconMetadata.style} ${attrs.style}"/>
</j:if>

<j:choose>
<j:when test="${attrs.src.startsWith('symbol-')}">
<j:when test="${iconSrc.startsWith('symbol-')}">
<j:invokeStatic var="icon" className="org.jenkins.ui.icon.IconSet" method="getSymbol">
<j:arg value="${attrs.src.substring(7)}" />
<j:arg value="${attrs.alt ?: ''}" />
<j:arg value="${iconSrc.substring(7)}" />
<j:arg value="${alt ?: ''}" />
<j:arg value="${iconMetadata.classSpec ?: ''}" />
</j:invokeStatic>
<j:out value="${icon}" />
</j:when>
<j:otherwise>
<j:set var="iconSrc" value="${h.tryGetIconPath(src, context)}"/>
<j:set var="iconMetadata" value="${h.tryGetIcon(src)}"/>
<j:set var="imgStyle" value="${attrs.style}"/>

<j:if test="${iconSrc == null}">
<j:set var="iconSrc" value="${h.tryGetIconPath(class, context)}"/>
<j:set var="iconMetadata" value="${h.tryGetIcon(class)}"/>
</j:if>

<j:if test="${iconMetadata != null}">
<j:set var="imgStyle" value="${iconMetadata.style} ${attrs.style}"/>
</j:if>

<j:choose>
<j:when test="${iconMetadata.buildStatus}">
<j:set var="outerLayer" value="${iconMetadata.inProgress ? 'build-status-in-progress' : 'build-status-static'}" />
Expand Down
2 changes: 2 additions & 0 deletions war/src/main/less/modules/section.less
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@

.icon, .icon-xlg, svg {
position: relative;
min-width: 48px !important;
min-height: 48px !important;
width: 48px !important;
height: 48px !important;
pointer-events: none;
Expand Down
1 change: 1 addition & 0 deletions war/src/main/resources/images/symbols/plugins.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 013f733

Please sign in to comment.