]> source.dussan.org Git - gitblit.git/commitdiff
Support languagecode-countrycode Markdown resources
authorJames Moger <james.moger@gitblit.com>
Fri, 7 Dec 2012 13:46:52 +0000 (08:46 -0500)
committerJames Moger <james.moger@gitblit.com>
Fri, 7 Dec 2012 14:20:30 +0000 (09:20 -0500)
src/com/gitblit/wicket/pages/BasePage.java
src/com/gitblit/wicket/pages/ProjectsPage.java
src/com/gitblit/wicket/pages/RepositoriesPage.java

index d1ee271071b223f91b3f32f465179e183ecd023f..9d46908332f24256e874838fec3742fb3bea8da8 100644 (file)
@@ -98,6 +98,10 @@ public abstract class BasePage extends WebPage {
                return GitBlitWebSession.get().getLocale().getLanguage();\r
        }\r
        \r
+       protected String getCountryCode() {\r
+               return GitBlitWebSession.get().getLocale().getCountry().toLowerCase();\r
+       }\r
+       \r
        protected TimeUtils getTimeUtils() {\r
                if (timeUtils == null) {\r
                        ResourceBundle bundle;          \r
index 7161d0f33f0ec8bde49204c1ec9207f0fe6abba5..4e3e6309cb8f54a576ca56aab2f150a8d5419bb1 100644 (file)
@@ -194,39 +194,47 @@ public class ProjectsPage extends RootPage {
        }\r
 \r
        private String readDefaultMarkdown(String file) {\r
-               String content = readDefaultMarkdown(file, getLanguageCode());\r
-               if (StringUtils.isEmpty(content)) {\r
-                       content = readDefaultMarkdown(file, null);\r
-               }\r
-               return content;\r
-       }\r
+               String base = file.substring(0, file.lastIndexOf('.'));\r
+               String ext = file.substring(file.lastIndexOf('.'));\r
+               String lc = getLanguageCode();\r
+               String cc = getCountryCode();\r
 \r
-       private String readDefaultMarkdown(String file, String lc) {\r
+               // try to read file_en-us.ext, file_en.ext, file.ext\r
+               List<String> files = new ArrayList<String>();\r
                if (!StringUtils.isEmpty(lc)) {\r
-                       // convert to file_lc.mkd\r
-                       file = file.substring(0, file.lastIndexOf('.')) + "_" + lc\r
-                                       + file.substring(file.lastIndexOf('.'));\r
+                       if (!StringUtils.isEmpty(cc)) {\r
+                               files.add(base + "_" + lc + "-" + cc + ext);\r
+                               files.add(base + "_" + lc + "_" + cc + ext);\r
+                       }\r
+                       files.add(base + "_" + lc + ext);\r
                }\r
-               String message;\r
-               try {\r
-                       ContextRelativeResource res = WicketUtils.getResource(file);\r
-                       InputStream is = res.getResourceStream().getInputStream();\r
-                       InputStreamReader reader = new InputStreamReader(is, Constants.CHARACTER_ENCODING);\r
-                       message = MarkdownUtils.transformMarkdown(reader);\r
-                       reader.close();\r
-               } catch (ResourceStreamNotFoundException t) {\r
-                       if (lc == null) {\r
-                               // could not find default language resource\r
+               files.add(file);\r
+               \r
+               for (String name : files) {\r
+                       String message;\r
+                       InputStreamReader reader = null;\r
+                       try {\r
+                               ContextRelativeResource res = WicketUtils.getResource(name);\r
+                               InputStream is = res.getResourceStream().getInputStream();\r
+                               reader = new InputStreamReader(is, Constants.CHARACTER_ENCODING);\r
+                               message = MarkdownUtils.transformMarkdown(reader);\r
+                               reader.close();\r
+                               return message;\r
+                       } catch (ResourceStreamNotFoundException t) {\r
+                               continue;\r
+                       } catch (Throwable t) {\r
                                message = MessageFormat.format(getString("gb.failedToReadMessage"), file);\r
                                error(message, t, false);\r
-                       } else {\r
-                               // ignore so we can try default language resource\r
-                               message = null;\r
-                       }\r
-               } catch (Throwable t) {\r
-                       message = MessageFormat.format(getString("gb.failedToReadMessage"), file);\r
-                       error(message, t, false);\r
+                               return message;\r
+                       } finally {\r
+                               if (reader != null) {\r
+                                       try {\r
+                                               reader.close();\r
+                                       } catch (Exception e) {\r
+                                       }\r
+                               }\r
+                       }                       \r
                }\r
-               return message;\r
+               return MessageFormat.format(getString("gb.failedToReadMessage"), file);\r
        }\r
 }\r
index 97c6aa236bd1f9b8024a10233a39bf46d74c4235..4816d45cd5cae170ee0e9f21ce1d591afa781455 100644 (file)
@@ -20,6 +20,7 @@ import java.io.FileInputStream;
 import java.io.InputStream;\r
 import java.io.InputStreamReader;\r
 import java.text.MessageFormat;\r
+import java.util.ArrayList;\r
 import java.util.List;\r
 \r
 import org.apache.wicket.Component;\r
@@ -139,37 +140,47 @@ public class RepositoriesPage extends RootPage {
        }\r
 \r
        private String readDefaultMarkdown(String file) {\r
-               String content = readDefaultMarkdown(file, getLanguageCode());\r
-               if (StringUtils.isEmpty(content)) {\r
-                       content = readDefaultMarkdown(file, null);\r
-               }\r
-               return content;\r
-       }\r
-       \r
-       private String readDefaultMarkdown(String file, String lc) {\r
+               String base = file.substring(0, file.lastIndexOf('.'));\r
+               String ext = file.substring(file.lastIndexOf('.'));\r
+               String lc = getLanguageCode();\r
+               String cc = getCountryCode();\r
+\r
+               // try to read file_en-us.ext, file_en.ext, file.ext\r
+               List<String> files = new ArrayList<String>();\r
                if (!StringUtils.isEmpty(lc)) {\r
-                       // convert to file_lc.mkd\r
-                       file = file.substring(0, file.lastIndexOf('.')) + "_" + lc + file.substring(file.lastIndexOf('.'));\r
+                       if (!StringUtils.isEmpty(cc)) {\r
+                               files.add(base + "_" + lc + "-" + cc + ext);\r
+                               files.add(base + "_" + lc + "_" + cc + ext);\r
+                       }\r
+                       files.add(base + "_" + lc + ext);\r
                }\r
-               String message;\r
-               try {                   \r
-                   InputStream is = GitBlit.self().getResourceAsStream(file);\r
-                       InputStreamReader reader = new InputStreamReader(is, Constants.CHARACTER_ENCODING);\r
-                       message = MarkdownUtils.transformMarkdown(reader);\r
-                       reader.close();\r
-               } catch (ResourceStreamNotFoundException t) {\r
-                       if (lc == null) {\r
-                               // could not find default language resource\r
+               files.add(file);\r
+\r
+               for (String name : files) {\r
+                       String message;\r
+                       InputStreamReader reader = null;\r
+                       try {\r
+                               ContextRelativeResource res = WicketUtils.getResource(name);\r
+                               InputStream is = res.getResourceStream().getInputStream();\r
+                               reader = new InputStreamReader(is, Constants.CHARACTER_ENCODING);\r
+                               message = MarkdownUtils.transformMarkdown(reader);\r
+                               reader.close();\r
+                               return message;\r
+                       } catch (ResourceStreamNotFoundException t) {\r
+                               continue;\r
+                       } catch (Throwable t) {\r
                                message = MessageFormat.format(getString("gb.failedToReadMessage"), file);\r
                                error(message, t, false);\r
-                       } else {\r
-                               // ignore so we can try default language resource\r
-                               message = null;\r
-                       }\r
-               } catch (Throwable t) {\r
-                       message = MessageFormat.format(getString("gb.failedToReadMessage"), file);\r
-                       error(message, t, false);\r
+                               return message;\r
+                       } finally {\r
+                               if (reader != null) {\r
+                                       try {\r
+                                               reader.close();\r
+                                       } catch (Exception e) {\r
+                                       }\r
+                               }\r
+                       }                       \r
                }\r
-               return message;\r
+               return MessageFormat.format(getString("gb.failedToReadMessage"), file);\r
        }\r
 }\r