use of com.serotonin.m2m2.i18n.Translations in project ma-modules-public by infiniteautomation.
the class ServerRestV2Controller method sendTestEmail.
@PreAuthorize("isAdmin()")
@ApiOperation(value = "Send a test email", notes = "Sends email to supplied address")
@RequestMapping(method = RequestMethod.PUT, value = "/email/test")
public ResponseEntity<String> sendTestEmail(@RequestParam(value = "email", required = true, defaultValue = "") String email, @RequestParam(value = "username", required = true, defaultValue = "") String username, HttpServletRequest request) {
try {
Translations translations = Common.getTranslations();
Map<String, Object> model = new HashMap<>();
model.put("message", new TranslatableMessage("ftl.userTestEmail", username));
MangoEmailContent cnt = new MangoEmailContent("testEmail", model, translations, translations.translate("ftl.testEmail"), Common.UTF8);
EmailWorkItem.queueEmail(email, cnt);
return new ResponseEntity<String>(new TranslatableMessage("common.testEmailSent", email).translate(Common.getTranslations()), HttpStatus.OK);
} catch (Exception e) {
throw new GenericRestException(HttpStatus.INTERNAL_SERVER_ERROR, e);
}
}
use of com.serotonin.m2m2.i18n.Translations in project ma-modules-public by infiniteautomation.
the class ServerRestController method sendTestEmail.
@ApiOperation(value = "Send a test email", notes = "Sends email to supplied address")
@RequestMapping(method = RequestMethod.PUT, consumes = { "application/json", "text/csv" }, produces = { "application/json", "text/csv" }, value = "/email/test")
public ResponseEntity<String> sendTestEmail(@RequestParam(value = "email", required = true, defaultValue = "") String email, @RequestParam(value = "username", required = true, defaultValue = "") String username, HttpServletRequest request) throws RestValidationFailedException {
RestProcessResult<String> result = new RestProcessResult<String>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
if (Permissions.hasAdmin(user)) {
try {
Translations translations = Common.getTranslations();
Map<String, Object> model = new HashMap<>();
model.put("message", new TranslatableMessage("ftl.userTestEmail", username));
MangoEmailContent cnt = new MangoEmailContent("testEmail", model, translations, translations.translate("ftl.testEmail"), Common.UTF8);
EmailWorkItem.queueEmail(email, cnt);
return result.createResponseEntity(new TranslatableMessage("common.testEmailSent", email).translate(Common.getTranslations()));
} catch (Exception e) {
result.addRestMessage(this.getInternalServerErrorMessage(e.getMessage()));
}
} else {
LOG.warn("Non admin user: " + user.getUsername() + " attempted to send a test email.");
result.addRestMessage(this.getUnauthorizedMessage());
}
}
return result.createResponseEntity();
}
use of com.serotonin.m2m2.i18n.Translations in project ma-modules-public by infiniteautomation.
the class TranslationsController method publicNamespacedTranslations.
@ApiOperation(value = "Get translations for public namespaces", notes = "Namespace must be base , ie public not public.messages. Returns sub-namespaces too. For > 1 use comma common,public")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" }, value = "/public/{namespaces}")
public ResponseEntity<Map<String, ?>> publicNamespacedTranslations(@ApiParam(value = "Message Namespaces, simmilar to java package structure", allowMultiple = true) @PathVariable String[] namespaces, @ApiParam(value = "Language for translation (must have language pack installed)", allowMultiple = false) @RequestParam(value = "language", required = false) String language, @ApiParam(value = "Use server language for translation", allowMultiple = false) @RequestParam(value = "server", required = false, defaultValue = "false") boolean server, @RequestParam(value = "browser", required = false, defaultValue = "false") boolean browser, HttpServletRequest request) {
RestProcessResult<Map<String, ?>> result = new RestProcessResult<Map<String, ?>>(HttpStatus.OK);
// Confirm the requested namespace is indeed public
for (String namespace : namespaces) {
if (!this.publicNamespaces.contains(namespace)) {
result.addRestMessage(getUnauthorizedMessage());
return result.createResponseEntity();
}
}
if (result.isOk()) {
Map<String, Object> resultMap = new HashMap<String, Object>();
Locale locale = this.getLocale(language, server, browser, request, Common.getHttpUser());
resultMap.put("locale", locale.toLanguageTag());
resultMap.put("translations", getTranslationMap(namespaces, locale));
return result.createResponseEntity(resultMap);
}
return result.createResponseEntity();
}
use of com.serotonin.m2m2.i18n.Translations in project ma-modules-public by infiniteautomation.
the class TranslationsController method namespacedTranslations.
@ApiOperation(value = "Get translations based on namespaces", notes = "Namespace must be base namespace, ie common not common.messages. Returns sub-namespaces too. For > 1 use comma common,public")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" }, value = "/{namespaces}")
public ResponseEntity<Map<String, ?>> namespacedTranslations(@ApiParam(value = "Message Namespaces, simmilar to java package structure", allowMultiple = true) @PathVariable String[] namespaces, @ApiParam(value = "Language for translation (must have language pack installed)", allowMultiple = false) @RequestParam(value = "language", required = false) String language, @ApiParam(value = "Use server language for translation", allowMultiple = false) @RequestParam(value = "server", required = false, defaultValue = "false") boolean server, @RequestParam(value = "browser", required = false, defaultValue = "false") boolean browser, HttpServletRequest request) {
RestProcessResult<Map<String, ?>> result = new RestProcessResult<Map<String, ?>>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
Map<String, Object> resultMap = new HashMap<String, Object>();
Locale locale = this.getLocale(language, server, browser, request, user);
resultMap.put("locale", locale.toLanguageTag());
resultMap.put("translations", getTranslationMap(namespaces, locale));
return result.createResponseEntity(resultMap);
}
return result.createResponseEntity();
}
use of com.serotonin.m2m2.i18n.Translations in project ma-modules-public by infiniteautomation.
the class TranslationsController method getTranslationMap.
/**
* Get a set of translations for many namespaces
* @param namespaces
* @param locale
* @return
*/
private Map<String, Map<String, String>> getTranslationMap(String[] namespaces, Locale locale) {
Translations translations = Translations.getTranslations(locale);
Map<String, Map<String, String>> resultMap = new HashMap<String, Map<String, String>>();
for (String namespace : namespaces) {
Map<String, Map<String, String>> tranMap = translations.asMap(namespace);
Iterator<String> it = tranMap.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
Map<String, String> submap = resultMap.get(key);
if (submap == null) {
submap = new HashMap<String, String>();
resultMap.put(key, submap);
}
submap.putAll(tranMap.get(key));
}
}
return resultMap;
}
Aggregations