Skip to content

Commit

Permalink
feat(core): i18n capabilities in the templates (#46) #38
Browse files Browse the repository at this point in the history
- Localized templates
- Multiple new template customization option
- Updated docs and samples
  • Loading branch information
nandorholozsnyak authored Mar 4, 2023
1 parent bb315e4 commit 332c750
Show file tree
Hide file tree
Showing 53 changed files with 836 additions and 309 deletions.
89 changes: 82 additions & 7 deletions docs/template-customization.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ There are few Java classes to be discovered if you wish:

|tableOfContentsEnabled
|boolean
|Controls if the "Table of contents" sections should be enabled or not
|Controls if the "Table of Contents" sections should be enabled or not
|*true*
|0.2.0

Expand All @@ -415,6 +415,12 @@ There are few Java classes to be discovered if you wish:
|*true*
|0.3.0

|unknownGroupLocalization
|String
|Overwrites the name of the `Unknown group`
|*Unknown group*
|0.4.0

|includeGenerationDate
|boolean
|Controls if the generation date should be rendered to the final document or not.
Expand All @@ -433,6 +439,18 @@ There are few Java classes to be discovered if you wish:
|*false*
|0.4.0

|tocTitle
|String
|'Table of Contents' section's title. By default it is coming from the `messages.properties`
|
|0.4.0

|locale
|String
|Set's the locale of the final document. - It will be converted to Locale with the `Locale.forLanguageTag` method.
|JVM's default locale.
|0.4.0

|===

.`org.rodnansol.core.generator.template.customization.MarkdownTemplateCustomization`
Expand All @@ -456,12 +474,6 @@ There are few Java classes to be discovered if you wish:
|===
|Variable |Type |Description |Default value |Since

|tocTitle
|String
|"Table of Contents" replacement title
|Table of Contents
|0.2.0

|tocPlacement
|TocPlacement (AUTO, LEFT, RIGHT)
|"Table of Contents" placement
Expand Down Expand Up @@ -538,3 +550,66 @@ There are few Java classes to be discovered if you wish:
|===

NOTE: The full stylesheet can be found here in this file: https://github.com/rodnansol/spring-configuration-property-documenter/blob/master/spring-configuration-property-documenter-core/src/main/resources/templates/partials/html/header.html.hbs

=== i18n resources

The following languages are supported, feel free to open new pull requests for additional languages.

* English
* Hungarian

The default file can be found here: `spring-configuration-property-documenter-core/src/main/resources/messages.properties`

.The following keys are available in your custom templates
[cols="1,1,1"]
|===
|Key |Description |Since

|toc
|Title for the `Table of Contents` section
|0.4.0

|generationDate
|Localization for the generation date footer value
|0.4.0

|backToTop
|Localization for the back to top section in the HTML template
|0.4.0

|class
|Localization for the class field
|0.4.0

|key
|Localization for the key field
|0.4.0

|type
|Localization for the type field
|0.4.0

|description
|Localization for the description field
|0.4.0

|defaultValue
|Localization for the default value field
|0.4.0

|deprecation
|Localization for the deprecation field
|0.4.0

|environmentVariable
|Localization for the environmentVariable field
|0.4.0

|===

It's the default content:

[source,properties]
----
include::../spring-configuration-property-documenter-core/src/main/resources/messages.properties[]
----
2 changes: 2 additions & 0 deletions samples/single-module/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
<configuration>
<type>ADOC</type>
<asciiDocCustomization>
<locale>hu</locale>
<unknownGroupLocalization>Random name for the Unknown group</unknownGroupLocalization>
<includeEnvFormat>true</includeEnvFormat>
</asciiDocCustomization>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.rodnansol.core.generator.template.customization.HtmlTemplateCustomization;
import org.rodnansol.core.generator.template.customization.MarkdownTemplateCustomization;
import org.rodnansol.core.generator.template.customization.XmlTemplateCustomization;
import org.rodnansol.core.generator.writer.postprocess.PropertyGroupFilterService;
import org.rodnansol.core.project.ProjectFactory;
import org.rodnansol.core.project.simple.SimpleProject;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.rodnansol.core.generator.template.customization.HtmlTemplateCustomization;
import org.rodnansol.core.generator.template.customization.MarkdownTemplateCustomization;
import org.rodnansol.core.generator.template.customization.XmlTemplateCustomization;
import org.rodnansol.core.generator.writer.postprocess.PropertyGroupFilterService;
import org.rodnansol.core.project.ProjectFactory;
import org.rodnansol.core.project.simple.SimpleProject;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ <h1 class="center center-text">IT</h1>


<div class="center">
<button type="button" class="collapsible">Table of contents</button>
<button type="button" class="collapsible">Table of Contents</button>
<div id="toc-content">


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@



## Table of contents
## Table of Contents


* [**Document 1**](#Document 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ <h1 class="center center-text">IT</h1>


<div class="center">
<button type="button" class="collapsible">Table of contents</button>
<button type="button" class="collapsible">Table of Contents</button>
<div id="toc-content">


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@



## Table of contents
## Table of Contents


* [**Document 1**](#Document 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ <h1 class="center center-text">IT</h1>


<div class="center">
<button type="button" class="collapsible">Table of contents</button>
<button type="button" class="collapsible">Table of Contents</button>
<div id="toc-content">


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@



## Table of contents
## Table of Contents

* [**Unknown group** - `Unknown`](#Unknown group)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ <h1 class="center center-text">IT</h1>


<div class="center">
<button type="button" class="collapsible">Table of contents</button>
<button type="button" class="collapsible">Table of Contents</button>
<div id="toc-content">


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@



## Table of contents
## Table of Contents

* [**Unknown group** - `Unknown`](#Unknown group)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.rodnansol.core.generator.template.customization.TemplateCustomization;
import org.rodnansol.core.generator.writer.CreateDocumentCommand;
import org.rodnansol.core.generator.writer.Documenter;
import org.rodnansol.core.generator.writer.PropertyGroupFilterService;
import org.rodnansol.core.generator.writer.postprocess.PropertyGroupFilterService;
import org.rodnansol.core.project.Project;
import org.rodnansol.core.util.CoreFileUtils;
import org.slf4j.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.rodnansol.core.generator.template.Property;
import org.rodnansol.core.generator.template.PropertyDeprecation;
import org.rodnansol.core.generator.template.PropertyGroup;
import org.rodnansol.core.generator.template.PropertyGroupConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
Expand Down Expand Up @@ -136,7 +137,7 @@ private Map<String, List<PropertyGroup>> getPropertyGroups(ConfigurationMetadata
.collect(Collectors.groupingBy(PropertyGroup::getSourceType, Collectors.toList()));
List<PropertyGroup> value = new ArrayList<>();
value.add(PropertyGroup.createUnknownGroup());
propertyGroupMap.put(PropertyGroup.UNKNOWN, value);
propertyGroupMap.put(PropertyGroupConstants.UNKNOWN, value);
return propertyGroupMap;
}

Expand All @@ -151,7 +152,7 @@ private Map<String, List<Property>> getPropertyMap(ConfigurationMetadata configu
}

private String getSourceTypeOrDefault(ItemMetadata current) {
return Optional.ofNullable(current.getSourceType()).orElse(PropertyGroup.UNKNOWN);
return Optional.ofNullable(current.getSourceType()).orElse(PropertyGroupConstants.UNKNOWN);
}

private Property mapToProperty(ItemMetadata itemMetadata) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.rodnansol.core.generator.template;

import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.helper.I18nHelper;
import com.github.jknack.handlebars.io.ClassPathTemplateLoader;
import org.rodnansol.core.generator.DocumentGenerationException;
import org.rodnansol.core.generator.template.handlebars.EnvironmentVariableHelper;
Expand All @@ -11,6 +13,8 @@
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Locale;
import java.util.Objects;

/**
* Template compiler implementation that uses <a href="https://jknack.github.io/handlebars.java"/>Handlebars</a>to compile a template.
Expand Down Expand Up @@ -41,7 +45,10 @@ private static Handlebars createDefaultHandlebarsInstance() {

public String compileTemplate(String templatePath, TemplateData templateData) throws DocumentGenerationException {
LOGGER.debug("Compiling template:[{}] with data:[{}]", templatePath, templatePath);
Objects.requireNonNull(templatePath,"templatePath is NULL");
Objects.requireNonNull(templateData,"templateData is NULL");
try {
setLocalizationIfPossible(templateData);
return handlebars
.compile(templatePath)
.apply(templateData);
Expand All @@ -50,4 +57,19 @@ public String compileTemplate(String templatePath, TemplateData templateData) th
}
}

private void setLocalizationIfPossible(TemplateData templateData) {
try {
if(templateData.getTemplateCustomization() != null && templateData.getTemplateCustomization().getLocale() != null) {
Helper<String> i18n = handlebars.helper("i18n");
if(i18n instanceof I18nHelper) {
String locale = templateData.getTemplateCustomization().getLocale();
LOGGER.debug("Setting locale to: [{}]", locale);
((I18nHelper) i18n).setDefaultLocale(Locale.forLanguageTag(locale));
}
}
} catch (Exception e) {
LOGGER.warn("Error during setting the Localization for the documents, please check the logs for more information.");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.util.List;
import java.util.Objects;

import static org.rodnansol.core.generator.template.PropertyGroupConstants.UNKNOWN;
import static org.rodnansol.core.generator.template.PropertyGroupConstants.UNKNOWN_GROUP;

/**
* Class representing a property group.
*
Expand All @@ -12,10 +15,7 @@
*/
public class PropertyGroup {

public static final String UNKNOWN_GROUP = "Unknown group";
public static final String UNKNOWN = "Unknown";

private final String groupName;
private String groupName;
private final String type;
private final String sourceType;
private List<Property> properties;
Expand Down Expand Up @@ -50,6 +50,10 @@ public static PropertyGroup createUnknownGroup() {
return propertyGroup;
}

public void setGroupName(String groupName) {
this.groupName = groupName;
}

public String getType() {
return type;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.rodnansol.core.generator.template;

/**
* Class grouping constants together for the property groups.
*
* @author nandorholozsnyak
* @since 0.4.0
*/
public class PropertyGroupConstants {

public static final String UNKNOWN_GROUP = "Unknown group";
public static final String UNKNOWN = "Unknown";

}
Loading

0 comments on commit 332c750

Please sign in to comment.