mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
i18n implementation (#55)
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
package com.commafeed.backend;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
@@ -14,6 +18,7 @@ import javax.ejb.Startup;
|
||||
import javax.enterprise.inject.Instance;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang.mutable.MutableBoolean;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -27,6 +32,7 @@ import com.commafeed.backend.model.ApplicationSettings;
|
||||
import com.commafeed.backend.model.UserRole.Role;
|
||||
import com.commafeed.backend.services.ApplicationSettingsService;
|
||||
import com.commafeed.backend.services.UserService;
|
||||
import com.google.api.client.util.Maps;
|
||||
|
||||
@Startup
|
||||
@Singleton
|
||||
@@ -59,6 +65,7 @@ public class StartupBean {
|
||||
Instance<FeedRefreshWorker> workers;
|
||||
|
||||
private long startupTime;
|
||||
private Map<String, String> supportedLanguages = Maps.newHashMap();
|
||||
|
||||
private ExecutorService executor;
|
||||
private MutableBoolean running = new MutableBoolean(true);
|
||||
@@ -70,6 +77,8 @@ public class StartupBean {
|
||||
initialData();
|
||||
}
|
||||
|
||||
initSupportedLanguages();
|
||||
|
||||
ApplicationSettings settings = applicationSettingsService.get();
|
||||
int threads = settings.getBackgroundThreads();
|
||||
log.info("Starting {} background threads", threads);
|
||||
@@ -88,13 +97,30 @@ public class StartupBean {
|
||||
|
||||
}
|
||||
|
||||
private void initSupportedLanguages() {
|
||||
Properties props = new Properties();
|
||||
InputStream is = null;
|
||||
try {
|
||||
is = getClass().getResourceAsStream("/i18n/languages.properties");
|
||||
props.load(new InputStreamReader(is, "UTF-8"));
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
} finally {
|
||||
IOUtils.closeQuietly(is);
|
||||
}
|
||||
for (Object key : props.keySet()) {
|
||||
supportedLanguages.put(key.toString(),
|
||||
props.getProperty(key.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
private void initialData() {
|
||||
log.info("Populating database with default values");
|
||||
|
||||
|
||||
ApplicationSettings settings = new ApplicationSettings();
|
||||
settings.setAnnouncement("Set the Public URL in the admin section !");
|
||||
applicationSettingsService.save(settings);
|
||||
|
||||
|
||||
userService.register(USERNAME_ADMIN, "admin",
|
||||
Arrays.asList(Role.ADMIN, Role.USER));
|
||||
userService.register(USERNAME_DEMO, "demo", Arrays.asList(Role.USER));
|
||||
@@ -104,6 +130,10 @@ public class StartupBean {
|
||||
return startupTime;
|
||||
}
|
||||
|
||||
public Map<String, String> getSupportedLanguages() {
|
||||
return supportedLanguages;
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void shutdown() {
|
||||
running.setValue(false);
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package com.commafeed.frontend.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
import com.google.api.client.util.Maps;
|
||||
import com.wordnik.swagger.annotations.ApiClass;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
@@ -15,6 +17,7 @@ import com.wordnik.swagger.annotations.ApiClass;
|
||||
public class ServerInfo implements Serializable {
|
||||
|
||||
private String announcement;
|
||||
private Map<String, String> supportedLanguages = Maps.newHashMap();
|
||||
|
||||
public String getAnnouncement() {
|
||||
return announcement;
|
||||
@@ -24,4 +27,12 @@ public class ServerInfo implements Serializable {
|
||||
this.announcement = announcement;
|
||||
}
|
||||
|
||||
public Map<String, String> getSupportedLanguages() {
|
||||
return supportedLanguages;
|
||||
}
|
||||
|
||||
public void setSupportedLanguages(Map<String, String> supportedLanguages) {
|
||||
this.supportedLanguages = supportedLanguages;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.apache.wicket.request.cycle.RequestCycle;
|
||||
import org.apache.wicket.util.crypt.Base64;
|
||||
|
||||
import com.commafeed.backend.MetricsBean;
|
||||
import com.commafeed.backend.StartupBean;
|
||||
import com.commafeed.backend.dao.FeedCategoryDAO;
|
||||
import com.commafeed.backend.dao.FeedDAO;
|
||||
import com.commafeed.backend.dao.FeedEntryDAO;
|
||||
@@ -89,6 +90,9 @@ public abstract class AbstractREST {
|
||||
@Inject
|
||||
UserService userService;
|
||||
|
||||
@Inject
|
||||
StartupBean startupBean;
|
||||
|
||||
@Inject
|
||||
UserSettingsDAO userSettingsDAO;
|
||||
|
||||
|
||||
@@ -18,6 +18,8 @@ public class ServerREST extends AbstractResourceREST {
|
||||
ServerInfo infos = new ServerInfo();
|
||||
infos.setAnnouncement(applicationSettingsService.get()
|
||||
.getAnnouncement());
|
||||
infos.getSupportedLanguages().putAll(
|
||||
startupBean.getSupportedLanguages());
|
||||
return infos;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,6 +64,10 @@ public class UserREST extends AbstractResourceREST {
|
||||
public Response saveSettings(@ApiParam Settings settings) {
|
||||
Preconditions.checkNotNull(settings);
|
||||
|
||||
if (startupBean.getSupportedLanguages().get(settings.getLanguage()) == null) {
|
||||
settings.setLanguage("en");
|
||||
}
|
||||
|
||||
UserSettings s = userSettingsDAO.findByUser(getUser());
|
||||
if (s == null) {
|
||||
s = new UserSettings();
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.commafeed.frontend.utils;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Properties;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -67,9 +68,10 @@ public class InternationalizationDevelopmentFilter implements Filter {
|
||||
String lang = settings.getLanguage() == null ? "en" : settings
|
||||
.getLanguage();
|
||||
|
||||
byte[] bytes = translate(wrapper.toString(), lang).getBytes();
|
||||
response.getOutputStream().write(bytes);
|
||||
byte[] bytes = translate(wrapper.toString(), lang).getBytes("UTF-8");
|
||||
response.setContentLength(bytes.length);
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
response.getOutputStream().write(bytes);
|
||||
response.getOutputStream().close();
|
||||
|
||||
}
|
||||
@@ -80,7 +82,7 @@ public class InternationalizationDevelopmentFilter implements Filter {
|
||||
try {
|
||||
is = getClass()
|
||||
.getResourceAsStream("/i18n/" + lang + ".properties");
|
||||
props.load(is);
|
||||
props.load(new InputStreamReader(is, "UTF-8"));
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
} finally {
|
||||
@@ -98,7 +100,9 @@ public class InternationalizationDevelopmentFilter implements Filter {
|
||||
while (m.find()) {
|
||||
String var = m.group(1);
|
||||
Object replacement = props.get(var);
|
||||
m.appendReplacement(sb, replacement.toString());
|
||||
String replacementValue = replacement == null ? var : replacement
|
||||
.toString();
|
||||
m.appendReplacement(sb, replacementValue);
|
||||
}
|
||||
m.appendTail(sb);
|
||||
return sb.toString();
|
||||
|
||||
Reference in New Issue
Block a user