doortts doortts 2017-09-25
i18n: Fix name rendering bug in mail contents
It was little bit difficult to modify existed codes to support
proper name renderin for each users whose have their own lang.

I don't want to be a prayer, but I hope it will work properly.

If it isn't, mail sending will stop.
@11d3f5e94aef3e0e022f2ac95d9ba0728e9858b4
app/models/NotificationMail.java
--- app/models/NotificationMail.java
+++ app/models/NotificationMail.java
@@ -612,7 +612,7 @@
         String renderred = null;
 
         if( resource != null) {
-            renderred = Markdown.render(message, resource.getProject());
+            renderred = Markdown.render(message, resource.getProject(), lang.code());
         } else {
             renderred = Markdown.render(message);
         }
app/models/User.java
--- app/models/User.java
+++ app/models/User.java
@@ -1011,6 +1011,22 @@
         return pureName;
     }
 
+    public String getPureNameOnly(String targetLang){
+        if (StringUtils.isNotBlank(englishName) && lang != null
+                && StringUtils.isNotBlank(targetLang) && targetLang.startsWith("en")) {
+            return englishName;
+        }
+        String pureName = this.name;
+        String [] spliters = { "[", "(" };
+        for(String spliter: spliters) {
+            if(pureName.contains(spliter)){
+                pureName = this.name.substring(0, this.name.indexOf(spliter));
+            }
+        }
+
+        return pureName;
+    }
+
     public String extractDepartmentPart(){
         String departmentName = this.name;
         String [] spliters = { "[", "(" };
app/utils/AutoLinkRenderer.java
--- app/utils/AutoLinkRenderer.java
+++ app/utils/AutoLinkRenderer.java
@@ -124,7 +124,7 @@
         this.project = project;
     }
 
-    public String render() {
+    public String render(String lang) {
         this.parse(PATH_WITH_ISSUE_PATTERN, new ToLink() {
             @Override
             public Link toLink(Matcher matcher) {
@@ -171,7 +171,7 @@
                 if (slashIndex > -1) {
                     return toValidProjectLink(path.substring(0, slashIndex), path.substring(slashIndex + 1));
                 } else {
-                    return toValidUserLink(path);
+                    return toValidUserLink(path, lang);
                 }
             }
         });
@@ -313,7 +313,7 @@
         return Link.EMPTY_LINK;
     }
 
-    private static Link toValidUserLink(String userId) {
+    private static Link toValidUserLink(String userId, String lang) {
         User user = User.findByLoginId(userId);
         Organization org = Organization.findByName(userId);
 
@@ -330,7 +330,15 @@
             } else {
                 avatarImage = "<img src='" + user.avatarUrl() + "' class='avatar-wrap smaller no-margin-no-padding vertical-top' alt='@" + user.name + " " + user.loginId + "'> ";
             }
-            return new Link(RouteUtil.getUrl(user), "no-text-decoration", "<span data-toggle='popover' data-placement='top' data-trigger='hover' data-html='true' data-content=\"" + StringEscapeUtils.escapeHtml4(avatarImage + user.name + " " + user.loginId) + "\">@" + user.getPureNameOnly() + "</span>");
+
+            String userName = null;
+            if( StringUtils.isBlank(lang)) {
+                userName = user.getPureNameOnly();
+            } else {
+                userName = user.getPureNameOnly(lang);
+            }
+
+            return new Link(RouteUtil.getUrl(user), "no-text-decoration", "<span data-toggle='popover' data-placement='top' data-trigger='hover' data-html='true' data-content=\"" + StringEscapeUtils.escapeHtml4(avatarImage + user.name + " " + user.loginId) + "\">@" + userName + "</span>");
         }
     }
 
app/utils/Markdown.java
--- app/utils/Markdown.java
+++ app/utils/Markdown.java
@@ -207,23 +207,32 @@
 
     public static String render(@Nonnull String source, Project project, boolean breaks) {
         AutoLinkRenderer autoLinkRenderer = new AutoLinkRenderer(renderWithHighlight(source, breaks), project);
-        return autoLinkRenderer.render();
+        return autoLinkRenderer.render(null);
+    }
+
+    public static String render(@Nonnull String source, Project project, boolean breaks, String lang) {
+        AutoLinkRenderer autoLinkRenderer = new AutoLinkRenderer(renderWithHighlight(source, breaks), project);
+        return autoLinkRenderer.render(lang);
     }
 
     public static String render(@Nonnull String source, Project project) {
         return render(source, project, true);
     }
 
+    public static String render(@Nonnull String source, Project project, String lang) {
+        return render(source, project, true, lang);
+    }
+
     public static String renderFileInCodeBrowser(@Nonnull String source, Project project) {
         String imageLinkFilter = replaceImageLinkPath(project, source);
         AutoLinkRenderer autoLinkRenderer = new AutoLinkRenderer(renderWithHighlight(imageLinkFilter, true), project);
-        return autoLinkRenderer.render();
+        return autoLinkRenderer.render(null);
     }
 
     public static String renderFileInReadme(@Nonnull String source, Project project) {
         String relativeLinksToCodeBrowserPath = replaceContentsLinkToCodeBrowerPath(project, source);
         AutoLinkRenderer autoLinkRenderer = new AutoLinkRenderer(renderWithHighlight(relativeLinksToCodeBrowserPath, true), project);
-        return autoLinkRenderer.render();
+        return autoLinkRenderer.render(null);
     }
 
     private static String replaceImageLinkPath(Project project, String text){
app/views/code/diff.scala.html
--- app/views/code/diff.scala.html
+++ app/views/code/diff.scala.html
@@ -19,7 +19,7 @@
 
 @autoLinkedMsg(msg:String) = @{
     val autoLinkRenderer = new AutoLinkRenderer(msg, project)
-    autoLinkRenderer.render()
+    autoLinkRenderer.render(null)
 }
 
 @projectLayout(Messages("code.commits") + " @" + commit.getId, project, utils.MenuType.CODE) {
app/views/code/history.scala.html
--- app/views/code/history.scala.html
+++ app/views/code/history.scala.html
@@ -18,7 +18,7 @@
 
 @autoLinkedMsg(msg:String) = @{
     val autoLinkRenderer = new AutoLinkRenderer(msg, project)
-    autoLinkRenderer.render()
+    autoLinkRenderer.render(null)
 }
 
 @getFilesURL = @{
Add a comment
List