Skip to content

Commit

Permalink
Separating logic from the controller as requested by @csokol
Browse files Browse the repository at this point in the history
  • Loading branch information
paulojribp committed May 27, 2014
1 parent f536953 commit 24612d6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 30 deletions.
38 changes: 8 additions & 30 deletions src/main/java/org/mamute/controllers/MessagesController.java
@@ -1,13 +1,11 @@
package org.mamute.controllers;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.inject.Inject;

import org.mamute.infra.locale.MessagesLoader;

import br.com.caelum.vraptor.Controller;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Result;
Expand All @@ -17,39 +15,19 @@
@Controller
public class MessagesController {

@Inject
private MessagesLoader loader;

@Inject
private Environment env;

@Inject private Result result;
@Inject
private Result result;

@Get("/messages/loadAll")
public void loadMessages() {
String locale = env.get("locale", "en");

Properties props = new Properties();
Map<String, String> messages = new HashMap<String, String>();
try {
props.load(this.getClass().getResourceAsStream("/messages.properties"));
Set<Object> keySet = props.keySet();
for (Object key : keySet) {
String value = (String) props.get(key);
messages.put((String) key, value);
}

if (!locale.equals("en")) {
props.load(this.getClass().getResourceAsStream("/messages_"+locale+".properties"));
keySet = props.keySet();
for (Object key : keySet) {
String value = (String) props.get(key);
messages.put((String) key, value);
}
}

} catch (IOException e) {
result.nothing();
return;
}

Map<String, String> messages = loader.loadBy(locale);
result.use(Results.json()).from(messages).serialize();
}

Expand Down
38 changes: 38 additions & 0 deletions src/main/java/org/mamute/infra/locale/MessagesLoader.java
@@ -0,0 +1,38 @@
package org.mamute.infra.locale;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class MessagesLoader {

public Map<String, String> loadBy(String locale) {
Properties props = new Properties();
Map<String, String> messages = new HashMap<String, String>();
try {
props.load(this.getClass().getResourceAsStream("/messages.properties"));
Set<Object> keySet = props.keySet();
for (Object key : keySet) {
String value = (String) props.get(key);
messages.put((String) key, value);
}

if (!locale.equals("en")) {
props.load(this.getClass().getResourceAsStream("/messages_"+locale+".properties"));
keySet = props.keySet();
for (Object key : keySet) {
String value = (String) props.get(key);
messages.put((String) key, value);
}
}

} catch (IOException e) {
throw new RuntimeException(e);
}

return messages;
}

}

0 comments on commit 24612d6

Please sign in to comment.