James Moger
2012-12-07 55037b1691c736b3cf95eadf583a8be526b9a3a2
Support languagecode-countrycode Markdown resources
3 files modified
133 ■■■■■ changed files
src/com/gitblit/wicket/pages/BasePage.java 4 ●●●● patch | view | raw | blame | history
src/com/gitblit/wicket/pages/ProjectsPage.java 64 ●●●●● patch | view | raw | blame | history
src/com/gitblit/wicket/pages/RepositoriesPage.java 65 ●●●●● patch | view | raw | blame | history
src/com/gitblit/wicket/pages/BasePage.java
@@ -98,6 +98,10 @@
        return GitBlitWebSession.get().getLocale().getLanguage();
    }
    
    protected String getCountryCode() {
        return GitBlitWebSession.get().getLocale().getCountry().toLowerCase();
    }
    protected TimeUtils getTimeUtils() {
        if (timeUtils == null) {
            ResourceBundle bundle;        
src/com/gitblit/wicket/pages/ProjectsPage.java
@@ -194,39 +194,47 @@
    }
    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<String> files = new ArrayList<String>();
        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);
    }
}
src/com/gitblit/wicket/pages/RepositoriesPage.java
@@ -20,6 +20,7 @@
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 @@
    }
    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<String> files = new ArrayList<String>();
        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);
    }
}