Search in sources :

Example 1 with I18n

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();
    }
}
Also used : HashMap(java.util.HashMap) EnglishLanguage(org.springframework.roo.addon.web.mvc.i18n.languages.EnglishLanguage) InputStream(java.io.InputStream) LogicalPath(org.springframework.roo.project.LogicalPath) IOException(java.io.IOException) Properties(java.util.Properties) I18n(org.springframework.roo.addon.web.mvc.i18n.components.I18n)

Example 2 with I18n

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);
}
Also used : EnglishLanguage(org.springframework.roo.addon.web.mvc.i18n.languages.EnglishLanguage) ArrayList(java.util.ArrayList) LogicalPath(org.springframework.roo.project.LogicalPath) I18n(org.springframework.roo.addon.web.mvc.i18n.components.I18n)

Example 3 with I18n

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);
}
Also used : LogicalPath(org.springframework.roo.project.LogicalPath) I18n(org.springframework.roo.addon.web.mvc.i18n.components.I18n)

Example 4 with I18n

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);
}
Also used : ArrayList(java.util.ArrayList) I18n(org.springframework.roo.addon.web.mvc.i18n.components.I18n)

Example 5 with I18n

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);
}
Also used : LogicalPath(org.springframework.roo.project.LogicalPath) I18n(org.springframework.roo.addon.web.mvc.i18n.components.I18n)

Aggregations

I18n (org.springframework.roo.addon.web.mvc.i18n.components.I18n)5 LogicalPath (org.springframework.roo.project.LogicalPath)4 ArrayList (java.util.ArrayList)2 EnglishLanguage (org.springframework.roo.addon.web.mvc.i18n.languages.EnglishLanguage)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1