Просмотр исходного кода

Support languagecode-countrycode Markdown resources

tags/v1.2.0
James Moger 11 лет назад
Родитель
Сommit
55037b1691

+ 4
- 0
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;

+ 36
- 28
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<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);
}
}

+ 38
- 27
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<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);
}
}

Загрузка…
Отмена
Сохранить