From 55037b1691c736b3cf95eadf583a8be526b9a3a2 Mon Sep 17 00:00:00 2001 From: James Moger Date: Fri, 7 Dec 2012 08:46:52 -0500 Subject: [PATCH] Support languagecode-countrycode Markdown resources --- src/com/gitblit/wicket/pages/BasePage.java | 4 ++ .../gitblit/wicket/pages/ProjectsPage.java | 64 ++++++++++-------- .../wicket/pages/RepositoriesPage.java | 65 +++++++++++-------- 3 files changed, 78 insertions(+), 55 deletions(-) diff --git a/src/com/gitblit/wicket/pages/BasePage.java b/src/com/gitblit/wicket/pages/BasePage.java index d1ee2710..9d469083 100644 --- a/src/com/gitblit/wicket/pages/BasePage.java +++ b/src/com/gitblit/wicket/pages/BasePage.java @@ -98,6 +98,10 @@ public abstract class BasePage extends WebPage { return GitBlitWebSession.get().getLocale().getLanguage(); } + protected String getCountryCode() { + return GitBlitWebSession.get().getLocale().getCountry().toLowerCase(); + } + protected TimeUtils getTimeUtils() { if (timeUtils == null) { ResourceBundle bundle; diff --git a/src/com/gitblit/wicket/pages/ProjectsPage.java b/src/com/gitblit/wicket/pages/ProjectsPage.java index 7161d0f3..4e3e6309 100644 --- a/src/com/gitblit/wicket/pages/ProjectsPage.java +++ b/src/com/gitblit/wicket/pages/ProjectsPage.java @@ -194,39 +194,47 @@ public class ProjectsPage extends RootPage { } private String readDefaultMarkdown(String file) { - String content = readDefaultMarkdown(file, getLanguageCode()); - if (StringUtils.isEmpty(content)) { - content = readDefaultMarkdown(file, null); - } - return content; - } + String base = file.substring(0, file.lastIndexOf('.')); + String ext = file.substring(file.lastIndexOf('.')); + String lc = getLanguageCode(); + String cc = getCountryCode(); - private String readDefaultMarkdown(String file, String lc) { + // try to read file_en-us.ext, file_en.ext, file.ext + List files = new ArrayList(); if (!StringUtils.isEmpty(lc)) { - // convert to file_lc.mkd - file = file.substring(0, file.lastIndexOf('.')) + "_" + lc - + file.substring(file.lastIndexOf('.')); + if (!StringUtils.isEmpty(cc)) { + files.add(base + "_" + lc + "-" + cc + ext); + files.add(base + "_" + lc + "_" + cc + ext); + } + files.add(base + "_" + lc + ext); } - String message; - try { - ContextRelativeResource res = WicketUtils.getResource(file); - InputStream is = res.getResourceStream().getInputStream(); - InputStreamReader reader = new InputStreamReader(is, Constants.CHARACTER_ENCODING); - message = MarkdownUtils.transformMarkdown(reader); - reader.close(); - } catch (ResourceStreamNotFoundException t) { - if (lc == null) { - // could not find default language resource + files.add(file); + + for (String name : files) { + String message; + InputStreamReader reader = null; + try { + ContextRelativeResource res = WicketUtils.getResource(name); + InputStream is = res.getResourceStream().getInputStream(); + reader = new InputStreamReader(is, Constants.CHARACTER_ENCODING); + message = MarkdownUtils.transformMarkdown(reader); + reader.close(); + return message; + } catch (ResourceStreamNotFoundException t) { + continue; + } catch (Throwable t) { message = MessageFormat.format(getString("gb.failedToReadMessage"), file); error(message, t, false); - } else { - // ignore so we can try default language resource - message = null; - } - } catch (Throwable t) { - message = MessageFormat.format(getString("gb.failedToReadMessage"), file); - error(message, t, false); + return message; + } finally { + if (reader != null) { + try { + reader.close(); + } catch (Exception e) { + } + } + } } - return message; + return MessageFormat.format(getString("gb.failedToReadMessage"), file); } } diff --git a/src/com/gitblit/wicket/pages/RepositoriesPage.java b/src/com/gitblit/wicket/pages/RepositoriesPage.java index 97c6aa23..4816d45c 100644 --- a/src/com/gitblit/wicket/pages/RepositoriesPage.java +++ b/src/com/gitblit/wicket/pages/RepositoriesPage.java @@ -20,6 +20,7 @@ import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.text.MessageFormat; +import java.util.ArrayList; import java.util.List; import org.apache.wicket.Component; @@ -139,37 +140,47 @@ public class RepositoriesPage extends RootPage { } private String readDefaultMarkdown(String file) { - String content = readDefaultMarkdown(file, getLanguageCode()); - if (StringUtils.isEmpty(content)) { - content = readDefaultMarkdown(file, null); - } - return content; - } - - private String readDefaultMarkdown(String file, String lc) { + String base = file.substring(0, file.lastIndexOf('.')); + String ext = file.substring(file.lastIndexOf('.')); + String lc = getLanguageCode(); + String cc = getCountryCode(); + + // try to read file_en-us.ext, file_en.ext, file.ext + List files = new ArrayList(); if (!StringUtils.isEmpty(lc)) { - // convert to file_lc.mkd - file = file.substring(0, file.lastIndexOf('.')) + "_" + lc + file.substring(file.lastIndexOf('.')); + if (!StringUtils.isEmpty(cc)) { + files.add(base + "_" + lc + "-" + cc + ext); + files.add(base + "_" + lc + "_" + cc + ext); + } + files.add(base + "_" + lc + ext); } - String message; - try { - InputStream is = GitBlit.self().getResourceAsStream(file); - InputStreamReader reader = new InputStreamReader(is, Constants.CHARACTER_ENCODING); - message = MarkdownUtils.transformMarkdown(reader); - reader.close(); - } catch (ResourceStreamNotFoundException t) { - if (lc == null) { - // could not find default language resource + files.add(file); + + for (String name : files) { + String message; + InputStreamReader reader = null; + try { + ContextRelativeResource res = WicketUtils.getResource(name); + InputStream is = res.getResourceStream().getInputStream(); + reader = new InputStreamReader(is, Constants.CHARACTER_ENCODING); + message = MarkdownUtils.transformMarkdown(reader); + reader.close(); + return message; + } catch (ResourceStreamNotFoundException t) { + continue; + } catch (Throwable t) { message = MessageFormat.format(getString("gb.failedToReadMessage"), file); error(message, t, false); - } else { - // ignore so we can try default language resource - message = null; - } - } catch (Throwable t) { - message = MessageFormat.format(getString("gb.failedToReadMessage"), file); - error(message, t, false); + return message; + } finally { + if (reader != null) { + try { + reader.close(); + } catch (Exception e) { + } + } + } } - return message; + return MessageFormat.format(getString("gb.failedToReadMessage"), file); } } -- 2.39.5