Search in sources :

Example 1 with EnglishLanguage

use of org.springframework.roo.addon.web.mvc.i18n.languages.EnglishLanguage 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 EnglishLanguage

use of org.springframework.roo.addon.web.mvc.i18n.languages.EnglishLanguage 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 EnglishLanguage

use of org.springframework.roo.addon.web.mvc.i18n.languages.EnglishLanguage in project spring-roo by spring-projects.

the class ThymeleafMVCViewResponseService method install.

@Override
public void install(Pom module) {
    // Is necessary to install thymeleaf dependencies
    addThymeleafDependencies(module);
    // Add JasperReport dependencies
    addJasperReportsDependencies(module);
    // Add JasperReport export classes
    addJasperReportExportClasses(module);
    // Is necessary to generate the main controller
    addMainController(module);
    // Thymeleaf needs Datatables component to list
    // data, so is necessary to install Datatables resources
    addThymeleafDatatablesResources(module);
    // Is necessary to copy static resources
    copyStaticResources(module);
    // Add application properties
    addApplicationProperties(module);
    // Add all available WebJar containing required static resources
    addWebJars(module);
    // Delegate on view generation to create view elements
    ViewContext<ThymeleafMetadata> ctx = new ViewContext<ThymeleafMetadata>();
    ctx.setProjectName(getProjectOperations().getProjectName(""));
    ctx.setVersion(getProjectOperations().getPomFromModuleName("").getVersion());
    getViewGenerationService().addIndexView(module.getModuleName(), ctx);
    getViewGenerationService().addLoginView(module.getModuleName(), ctx);
    getViewGenerationService().addErrorView(module.getModuleName(), ctx);
    getViewGenerationService().addDefaultLayout(module.getModuleName(), ctx);
    getViewGenerationService().addDefaultLayoutNoMenu(module.getModuleName(), ctx);
    getViewGenerationService().addHomeLayout(module.getModuleName(), ctx);
    getViewGenerationService().addDefaultListLayout(module.getModuleName(), ctx);
    getViewGenerationService().addFooter(module.getModuleName(), ctx);
    getViewGenerationService().addHeader(module.getModuleName(), ctx);
    getViewGenerationService().addMenu(module.getModuleName(), ctx);
    getViewGenerationService().addModal(module.getModuleName(), ctx);
    getViewGenerationService().addModalConfirm(module.getModuleName(), ctx);
    getViewGenerationService().addModalConfirmDelete(module.getModuleName(), ctx);
    getViewGenerationService().addModalExportEmptyError(module.getModuleName(), ctx);
    getViewGenerationService().addModalConfirmDeleteBatch(module.getModuleName(), ctx);
    getViewGenerationService().addSessionLinks(module.getModuleName(), ctx);
    getViewGenerationService().addLanguages(module.getModuleName(), ctx);
    getViewGenerationService().addAccessibilityView(module.getModuleName(), ctx);
    getViewGenerationService().addDefaultLayoutNoMenu(module.getModuleName(), ctx);
    // Add i18n support for english language and use it as default
    getI18nOperations().installLanguage(new EnglishLanguage(), true, module);
}
Also used : ViewContext(org.springframework.roo.addon.web.mvc.views.ViewContext) EnglishLanguage(org.springframework.roo.addon.web.mvc.i18n.languages.EnglishLanguage)

Aggregations

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