use of org.springframework.roo.addon.web.mvc.i18n.components.I18n in project spring-roo by spring-projects.
the class WebFlowOperationsImpl method addLocalizedMessages.
/**
* Add this add-on localized messages from its message bundles to the project's
* message bundles, for each installed language, plus English. Existing messages
* will be replaced.
*
* @param moduleName the module name where the message bundles are.
* @param flowName the name/id of the flow to prefix the messages.
*/
private void addLocalizedMessages(String moduleName) {
// Install localized messages for each installed language
for (I18n i18n : i18nSupport.getSupportedLanguages()) {
if (i18n.getLanguage().equals(new EnglishLanguage().getLanguage())) {
continue;
}
// Get theme specific messages
InputStream themeMessagesInputStream = null;
try {
themeMessagesInputStream = FileUtils.getInputStream(getClass(), String.format("messages_%s.properties", i18n.getLocale()));
} catch (NullPointerException ex) {
LOGGER.warning(String.format("There aren't translations for %1$s language. Adding english messages to messages_%1$s.properties instead.", i18n.getLocale()));
themeMessagesInputStream = FileUtils.getInputStream(getClass(), String.format("messages.properties", i18n.getLocale()));
}
final Properties loadedProperties = propFilesManagerService.loadProperties(themeMessagesInputStream);
// Add theme messages to localized message bundle
final LogicalPath resourcesPath = LogicalPath.getInstance(Path.SRC_MAIN_RESOURCES, moduleName);
final String targetDirectory = pathResolver.getIdentifier(resourcesPath, "");
String bundlePath = String.format("%s%smessages_%s.properties", targetDirectory, AntPathMatcher.DEFAULT_PATH_SEPARATOR, i18n.getLocale());
if (fileManager.exists(bundlePath)) {
Map<String, String> newProperties = new HashMap<String, String>();
for (Entry<Object, Object> entry : loadedProperties.entrySet()) {
String key = (String) entry.getKey();
String value = (String) entry.getValue();
// Prefix with flow name
key = String.format("%s_%s", this.flowName.toLowerCase(), key);
newProperties.put(key, value);
newProperties.put("label_".concat(this.flowName.toLowerCase()), StringUtils.capitalize(this.flowName));
}
propFilesManagerService.addProperties(resourcesPath, String.format("messages_%s.properties", i18n.getLocale()), newProperties, true, true);
}
// Close InputStream
try {
themeMessagesInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// Always install english messages
InputStream themeMessagesInputStream = FileUtils.getInputStream(getClass(), "messages.properties");
EnglishLanguage english = new EnglishLanguage();
final Properties loadedProperties = propFilesManagerService.loadProperties(themeMessagesInputStream);
// Add theme messages to localized message bundle
final LogicalPath resourcesPath = LogicalPath.getInstance(Path.SRC_MAIN_RESOURCES, moduleName);
final String targetDirectory = pathResolver.getIdentifier(resourcesPath, "");
String bundlePath = String.format("%s%smessages.properties", targetDirectory, AntPathMatcher.DEFAULT_PATH_SEPARATOR, english.getLocale());
if (fileManager.exists(bundlePath)) {
Map<String, String> newProperties = new HashMap<String, String>();
for (Entry<Object, Object> entry : loadedProperties.entrySet()) {
String key = (String) entry.getKey();
String value = (String) entry.getValue();
// Prefix with flow name
key = String.format("%s_%s", this.flowName.toLowerCase(), key);
newProperties.put(key, value);
newProperties.put("label_".concat(this.flowName.toLowerCase()), StringUtils.capitalize(this.flowName));
}
propFilesManagerService.addProperties(resourcesPath, "messages.properties", newProperties, true, true);
}
// Close InputStream
try {
themeMessagesInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
use of org.springframework.roo.addon.web.mvc.i18n.components.I18n in project spring-roo by spring-projects.
the class I18nOperationsImpl method getInstalledLanguages.
/**
* Return a list of installed languages in the provided application module.
*
* @param moduleName
* the module name to search for installed languages.
* @return a list with the available languages.
*/
@Override
public List<I18n> getInstalledLanguages(String moduleName) {
final LogicalPath resourcesPath = LogicalPath.getInstance(Path.SRC_MAIN_RESOURCES, moduleName);
final String targetDirectory = getPathResolver().getIdentifier(resourcesPath, "");
// Create list for installed languages
List<I18n> installedLanguages = new ArrayList<I18n>();
// Get all available languages
Set<I18n> supportedLanguages = getI18nSupport().getSupportedLanguages();
for (I18n i18n : supportedLanguages) {
String messageBundle = String.format("messages_%s.properties", i18n.getLocale().getLanguage());
String bundlePath = String.format("%s%s%s", targetDirectory, AntPathMatcher.DEFAULT_PATH_SEPARATOR, messageBundle);
if (getFileManager().exists(bundlePath)) {
installedLanguages.add(i18n);
}
}
// Always add English language as default
installedLanguages.add(new EnglishLanguage());
return Collections.unmodifiableList(installedLanguages);
}
use of org.springframework.roo.addon.web.mvc.i18n.components.I18n in project spring-roo by spring-projects.
the class ExceptionsOperationsImpl method addExceptionLabel.
/**
* Writes a label with a default exception message on messages.properties files
*
* @param exception
* @param moduleName
*/
private void addExceptionLabel(JavaType exception, String moduleName) {
if (getProjectOperations().isMultimoduleProject()) {
Validate.notBlank(moduleName, "Module name is required");
}
final LogicalPath resourcesPath = LogicalPath.getInstance(Path.SRC_MAIN_RESOURCES, moduleName);
final String targetDirectory = getPathResolver().getIdentifier(resourcesPath, "");
final String exceptionName = exception.getSimpleTypeName();
final String labelKey = LABEL_PREFIX.concat(exceptionName.toLowerCase());
Set<I18n> supportedLanguages = getI18nSupport().getSupportedLanguages();
for (I18n i18n : supportedLanguages) {
String messageBundle = String.format("messages_%s.properties", i18n.getLocale().getLanguage());
String bundlePath = String.format("%s%s%s", targetDirectory, AntPathMatcher.DEFAULT_PATH_SEPARATOR, messageBundle);
if (getFileManager().exists(bundlePath)) {
getPropFilesManager().addPropertyIfNotExists(resourcesPath, messageBundle, labelKey, String.format(LABEL_MESSAGE, exceptionName), true);
}
}
// Always update english message bundles
getPropFilesManager().addPropertyIfNotExists(resourcesPath, "messages.properties", labelKey, String.format(LABEL_MESSAGE, exceptionName), true);
}
use of org.springframework.roo.addon.web.mvc.i18n.components.I18n in project spring-roo by spring-projects.
the class AbstractViewGenerationService method addLanguages.
@Override
public void addLanguages(String moduleName, ViewContext<T> ctx) {
// Add installed languages
List<I18n> installedLanguages = getI18nOperations().getInstalledLanguages(moduleName);
ctx.addExtraParameter("languages", installedLanguages);
// Process elements to generate
DOC newDoc = null;
// Getting new viewName
String viewName = getFragmentsFolder(moduleName).concat("/languages").concat(getViewsExtension());
// Generate ids to search when merge new and existing doc
List<String> requiredIds = new ArrayList<String>();
for (I18n language : installedLanguages) {
requiredIds.add(language.getLocale().getLanguage() + "Flag");
}
// Check if new view to generate exists or not
if (existsFile(viewName)) {
DOC existingDoc = loadExistingDoc(viewName);
if (!isUserManagedDocument(existingDoc)) {
newDoc = merge("fragments/languages", existingDoc, ctx);
}
} else {
newDoc = process("fragments/languages", ctx);
}
// Write newDoc on disk
writeDoc(newDoc, viewName);
}
use of org.springframework.roo.addon.web.mvc.i18n.components.I18n in project spring-roo by spring-projects.
the class I18nOperationsImpl method addOrUpdateLabels.
/**
* Add labels to all installed languages
*
* @param moduleName
* @param labels
*/
@Override
public void addOrUpdateLabels(String moduleName, final Map<String, String> labels) {
final LogicalPath resourcesPath = LogicalPath.getInstance(Path.SRC_MAIN_RESOURCES, moduleName);
final String targetDirectory = getPathResolver().getIdentifier(resourcesPath, "");
Set<I18n> supportedLanguages = getI18nSupport().getSupportedLanguages();
for (I18n i18n : supportedLanguages) {
String messageBundle = String.format("messages_%s.properties", i18n.getLocale().getLanguage());
String bundlePath = String.format("%s%s%s", targetDirectory, AntPathMatcher.DEFAULT_PATH_SEPARATOR, messageBundle);
if (getFileManager().exists(bundlePath)) {
// Adding labels if not exists already
getPropFilesManager().addPropertiesIfNotExists(resourcesPath, messageBundle, labels, true, false);
}
}
// Allways update english message bundles if label not exists already
getPropFilesManager().addPropertiesIfNotExists(resourcesPath, "messages.properties", labels, true, false);
}
Aggregations