Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit bd2cce4

Browse files
authored
Support for translations (#189)
* Support for translations * pr feedback
1 parent cad937a commit bd2cce4

33 files changed

+343
-76
lines changed

docs/index.html

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ <h1>Spring Auto REST Docs</h1>
467467
<li><a href="#paging">Paging support</a></li>
468468
<li><a href="#preprocessors">Preprocessors</a></li>
469469
<li><a href="#deprecation">Deprecation support</a></li>
470+
<li><a href="#localization">Localization support</a></li>
470471
</ul>
471472
</li>
472473
<li><a href="#contributing">Contributing</a>
@@ -1669,6 +1670,22 @@ <h3 id="deprecation"><a class="link" href="#deprecation">Deprecation support</a>
16691670
See how it looks in the <a href="https://htmlpreview.github.io/?https://github.com/ScaCap/spring-auto-restdocs/blob/master/spring-auto-restdocs-example/generated-docs/index.html#resources-item-resource-test-clone-item">example project</a>.</p>
16701671
</div>
16711672
</div>
1673+
<div class="sect2">
1674+
<h3 id="localization"><a class="link" href="#localization">Localization support</a></h3>
1675+
<div class="paragraph">
1676+
<p>For custom translations of section titles, table headers and other messages:</p>
1677+
</div>
1678+
<div class="olist arabic">
1679+
<ol class="arabic">
1680+
<li>
1681+
<p>Create a file <code>SnippetMessages.properties</code> in <code>capital.scalable.restdocs.i18n</code> package.</p>
1682+
</li>
1683+
<li>
1684+
<p>Add translations to the file using keys from <a href="https://github.com/ScaCap/spring-auto-restdocs/blob/master/spring-auto-restdocs-core/src/test/resources/capital/scalable/restdocs/i18n/DefaultSnippetMessages.properties">DefaultSnippetMessages</a>.</p>
1685+
</li>
1686+
</ol>
1687+
</div>
1688+
</div>
16721689
</div>
16731690
</div>
16741691
<div class="sect1">
@@ -1750,7 +1767,7 @@ <h4 id="contributing-building-build"><a class="link" href="#contributing-buildin
17501767
</div>
17511768
<div id="footer">
17521769
<div id="footer-text">
1753-
Last updated 2017-12-01 21:41:24 CET
1770+
Last updated 2018-01-03 12:37:41 CET
17541771
</div>
17551772
</div>
17561773
<link rel="stylesheet" href="highlight/styles/github.min.css">

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/SnippetRegistry.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ public class SnippetRegistry {
2424

2525
static {
2626
CLASSIC_SNIPPETS = new HashMap<>();
27-
CLASSIC_SNIPPETS.put(CURL_REQUEST, section(CURL_REQUEST, "Example request", true));
28-
CLASSIC_SNIPPETS.put(HTTPIE_REQUEST, section(HTTPIE_REQUEST, "Example request", true));
29-
CLASSIC_SNIPPETS.put(HTTP_REQUEST, section(HTTP_REQUEST, "Example request", true));
30-
CLASSIC_SNIPPETS.put(HTTP_RESPONSE, section(HTTP_RESPONSE, "Example response", true));
27+
CLASSIC_SNIPPETS.put(CURL_REQUEST, section(CURL_REQUEST, "example-request", true));
28+
CLASSIC_SNIPPETS.put(HTTPIE_REQUEST, section(HTTPIE_REQUEST, "example-request", true));
29+
CLASSIC_SNIPPETS.put(HTTP_REQUEST, section(HTTP_REQUEST, "example-request", true));
30+
CLASSIC_SNIPPETS.put(HTTP_RESPONSE, section(HTTP_RESPONSE, "example-response", true));
3131
}
3232

3333
public static SectionSupport getClassicSnippet(String snippetName) {
@@ -40,12 +40,12 @@ private static SectionSupport section(String snippetName, String header, boolean
4040

4141
private static class ClassicSnippetSection implements SectionSupport {
4242
private String fileName;
43-
private String header;
43+
private String headerKey;
4444
private boolean hasContent;
4545

46-
public ClassicSnippetSection(String fileName, String header, boolean hasContent) {
46+
public ClassicSnippetSection(String fileName, String headerKey, boolean hasContent) {
4747
this.fileName = fileName;
48-
this.header = header;
48+
this.headerKey = headerKey;
4949
this.hasContent = hasContent;
5050
}
5151

@@ -55,8 +55,8 @@ public String getFileName() {
5555
}
5656

5757
@Override
58-
public String getHeader() {
59-
return header;
58+
public String getHeaderKey() {
59+
return headerKey;
6060
}
6161

6262
@Override
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package capital.scalable.restdocs.i18n;
18+
19+
import java.util.Locale;
20+
import java.util.MissingResourceException;
21+
import java.util.ResourceBundle;
22+
23+
/**
24+
* Inspired by
25+
* {@link org.springframework.restdocs.constraints.ResourceBundleConstraintDescriptionResolver}
26+
*/
27+
public class SnippetTranslationResolver {
28+
29+
private static ResourceBundle defaultMessages = getBundle("DefaultSnippetMessages");
30+
31+
private static ResourceBundle userMessages = getBundle("SnippetMessages");
32+
33+
private static ResourceBundle getBundle(String name) {
34+
try {
35+
return ResourceBundle.getBundle(
36+
SnippetTranslationResolver.class.getPackage()
37+
.getName() + "." + name,
38+
Locale.getDefault(), Thread.currentThread().getContextClassLoader());
39+
} catch (MissingResourceException ex) {
40+
return null;
41+
}
42+
}
43+
44+
public static String translate(String key) {
45+
try {
46+
if (userMessages != null) {
47+
return userMessages.getString(key);
48+
}
49+
} catch (MissingResourceException ex) {
50+
// Continue and return default description, if available
51+
}
52+
return defaultMessages.getString(key);
53+
}
54+
55+
// visible for testing
56+
public static void setUserMessages(String name) {
57+
userMessages = getBundle(name);
58+
}
59+
}

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/misc/AuthorizationSnippet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public String getFileName() {
6666
}
6767

6868
@Override
69-
public String getHeader() {
70-
return "Authorization";
69+
public String getHeaderKey() {
70+
return "authorization";
7171
}
7272

7373
@Override

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/payload/AbstractJacksonFieldSnippet.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static capital.scalable.restdocs.OperationAttributeHelper.getHandlerMethod;
2121
import static capital.scalable.restdocs.OperationAttributeHelper.getJavadocReader;
2222
import static capital.scalable.restdocs.OperationAttributeHelper.getObjectMapper;
23+
import static capital.scalable.restdocs.i18n.SnippetTranslationResolver.translate;
2324
import static capital.scalable.restdocs.util.FieldDescriptorUtil.assertAllDocumented;
2425

2526
import java.lang.reflect.ParameterizedType;
@@ -77,7 +78,7 @@ protected Collection<FieldDescriptor> createFieldDescriptors(Operation operation
7778
}
7879

7980
if (shouldFailOnUndocumentedFields()) {
80-
assertAllDocumented(fieldDescriptors.values(), getHeader().toLowerCase());
81+
assertAllDocumented(fieldDescriptors.values(), translate(getHeaderKey()).toLowerCase());
8182
}
8283
return fieldDescriptors.values();
8384
}

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/payload/JacksonRequestFieldSnippet.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,23 @@ public Type getGenericComponentType() {
8686
}
8787

8888
@Override
89-
public String getHeader() {
90-
return "Request fields";
89+
public String getHeaderKey() {
90+
return "request-fields";
9191
}
9292

9393
@Override
9494
protected boolean shouldFailOnUndocumentedFields() {
9595
return failOnUndocumentedFields;
9696
}
97+
98+
@Override
99+
protected String[] getTranslationKeys() {
100+
return new String[]{
101+
"th-path",
102+
"th-type",
103+
"th-optional",
104+
"th-description",
105+
"no-request-body"
106+
};
107+
}
97108
}

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/payload/JacksonResponseFieldSnippet.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,25 @@ private boolean isPageResponse(HandlerMethod handlerMethod) {
8989
}
9090

9191
@Override
92-
public String getHeader() {
93-
return "Response fields";
92+
public String getHeaderKey() {
93+
return "response-fields";
9494
}
9595

9696
@Override
9797
protected boolean shouldFailOnUndocumentedFields() {
9898
return failOnUndocumentedFields;
9999
}
100+
101+
@Override
102+
protected String[] getTranslationKeys() {
103+
return new String[]{
104+
"th-path",
105+
"th-type",
106+
"th-optional",
107+
"th-description",
108+
"pagination-response-adoc",
109+
"pagination-response-md",
110+
"no-response-body"
111+
};
112+
}
100113
}

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/request/AbstractParameterSnippet.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static capital.scalable.restdocs.constraints.ConstraintReader.DEFAULT_VALUE_ATTRIBUTE;
2424
import static capital.scalable.restdocs.constraints.ConstraintReader.DEPRECATED_ATTRIBUTE;
2525
import static capital.scalable.restdocs.constraints.ConstraintReader.OPTIONAL_ATTRIBUTE;
26+
import static capital.scalable.restdocs.i18n.SnippetTranslationResolver.translate;
2627
import static capital.scalable.restdocs.util.FieldDescriptorUtil.assertAllDocumented;
2728
import static capital.scalable.restdocs.util.TypeUtil.determineTypeName;
2829
import static java.util.Collections.singletonList;
@@ -67,7 +68,7 @@ protected List<FieldDescriptor> createFieldDescriptors(Operation operation,
6768
}
6869

6970
if (shouldFailOnUndocumentedParams()) {
70-
assertAllDocumented(fieldDescriptors, getHeader().toLowerCase());
71+
assertAllDocumented(fieldDescriptors, translate(getHeaderKey()).toLowerCase());
7172
}
7273

7374
return fieldDescriptors;

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/request/PathParametersSnippet.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ protected PathVariable getAnnotation(MethodParameter param) {
5353
}
5454

5555
@Override
56-
public String getHeader() {
57-
return "Path parameters";
56+
public String getHeaderKey() {
57+
return "path-parameters";
5858
}
5959

6060
@Override
@@ -67,4 +67,15 @@ protected String getDefaultValue(final PathVariable annotation) {
6767
// @PathVariable does not have a default value
6868
return null;
6969
}
70+
71+
@Override
72+
protected String[] getTranslationKeys() {
73+
return new String[]{
74+
"th-parameter",
75+
"th-type",
76+
"th-optional",
77+
"th-description",
78+
"no-params"
79+
};
80+
}
7081
}

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/request/RequestHeaderSnippet.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ protected RequestHeader getAnnotation(MethodParameter param) {
5353
}
5454

5555
@Override
56-
public String getHeader() {
57-
return "Request headers";
56+
public String getHeaderKey() {
57+
return "request-headers";
5858
}
5959

6060
@Override
@@ -66,4 +66,15 @@ protected boolean shouldFailOnUndocumentedParams() {
6666
protected String getDefaultValue(final RequestHeader annotation) {
6767
return annotation.defaultValue();
6868
}
69+
70+
@Override
71+
protected String[] getTranslationKeys() {
72+
return new String[]{
73+
"th-header",
74+
"th-type",
75+
"th-optional",
76+
"th-description",
77+
"no-headers"
78+
};
79+
}
6980
}

0 commit comments

Comments
 (0)