Skip to content

Commit

Permalink
[allure-adaptor] Add ability to specify custom translations for report
Browse files Browse the repository at this point in the history
  • Loading branch information
valfirst committed Sep 16, 2024
1 parent eb41e3b commit 04989de
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/modules/configuration/pages/reporting.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ story-level xref:ROOT:glossary.adoc#_examplestable[ExamplesTable] if any of them
contains numerous records, the Parameters section can become lengthy, thus increasing the difficulty of navigating to
the actual Execution body. The property can be used to hide the Parameters section, making navigation easier.

|`report.translations.<lang>.<translation key>`
|Alternative translation for the specified key.
|
a|This family of properties allows overriding text values in report controls, such as labels, buttons, etc. These values
represent translations for the chosen language. The list of languages and default translations can be found in the
official report repository, for example, here is translation configuration for English:
https://github.com/allure-framework/allure2/blob/main/allure-generator/src/main/javascript/translations/en.json[en.json].

To override a translation value it is necessary to provide translation key, which is essentially a path to the value in
JSON file. For example, to specify custom English message for duration chart without data the following property should
be used:
[source,properties]
----
report.translations.en.chart.duration.empty=No data to display
----

|===


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.vividus.report.allure.plugin;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.vividus.util.ResourceUtils;
import org.vividus.util.json.JsonUtils;
import org.vividus.util.property.PropertyMappedCollection;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.qameta.allure.PluginConfiguration;
import io.qameta.allure.plugin.DefaultPlugin;

public class CustomTranslationsPlugin extends DefaultPlugin
{
private static final String INDEX_JS = "index.js";
private final Map<String, Path> pluginFiles;

@SuppressFBWarnings("CT_CONSTRUCTOR_THROW")
public CustomTranslationsPlugin(PropertyMappedCollection<Map<String, ?>> customTranslations, JsonUtils jsonUtils)
throws IOException
{
super(new PluginConfiguration()
.setId("custom-translations")
.setJsFiles(List.of(INDEX_JS)),
List.of(), null);

List<String> jsFileLines = new ArrayList<>();
jsFileLines.add("'use strict';");
customTranslations.getData().forEach((lang, value) -> jsFileLines.add(
"allure.api.addTranslation('%s', %s);".formatted(lang, jsonUtils.toJson(value))
)
);

Path indexJs = ResourceUtils.createTempFile("index", ".js");
Files.write(indexJs, jsFileLines);

this.pluginFiles = Map.of(INDEX_JS, indexJs);
}

@Override
public Map<String, Path> getPluginFiles()
{
return pluginFiles;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@
<constructor-arg index="2" ref="pluginFilesLoader" />
</bean>

<bean class="org.vividus.report.allure.plugin.CustomTranslationsPlugin">

Check failure on line 55 in vividus-allure-adaptor/src/main/resources/org/vividus/report/allure/spring.xml

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Incorrect constructor injection in XML Spring bean

No matching constructor found in class 'CustomTranslationsPlugin'#treeend *** ** * ** *** |-----------------------------------------------------------------|---|-----------| | **CustomTranslationsPlugin(...):** | | **Bean:** | | PropertyMappedCollection\<Map\<String, ?\>\> customTranslations | | **???** | | JsonUtils jsonUtils | | **???** |
<constructor-arg>
<bean factory-bean="propertyMapper" factory-method="readValues">

Check failure on line 57 in vividus-allure-adaptor/src/main/resources/org/vividus/report/allure/spring.xml

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Incorrect constructor injection in XML Spring bean

No matching factory method found in class

Check failure on line 57 in vividus-allure-adaptor/src/main/resources/org/vividus/report/allure/spring.xml

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Incorrect Spring Core XML-based application context

Cannot resolve bean 'propertyMapper'

Check failure on line 57 in vividus-allure-adaptor/src/main/resources/org/vividus/report/allure/spring.xml

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Incorrect Spring Core XML-based application context

Cannot resolve method 'readValues'
<constructor-arg value="report.translations." />
<constructor-arg value="java.util.Map" />
</bean>
</constructor-arg>
</bean>

<bean class="org.vividus.report.allure.AllureRunContext" />

<bean class="org.vividus.report.allure.adapter.VerificationErrorAdapter" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.vividus.report.allure.plugin;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.nio.file.Files;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.vividus.util.json.JsonUtils;
import org.vividus.util.property.PropertyMappedCollection;

class CustomTranslationsPluginTests
{
@Test
void shouldGenerateIndexJs() throws IOException
{
var plugin = new CustomTranslationsPlugin(new PropertyMappedCollection<>(Map.of("en", Map.of(
"tab", Map.of(
"suites", Map.of(
"name", "Batches Tab"
)
)
))), new JsonUtils());
var pluginFiles = plugin.getPluginFiles();
assertEquals(1, pluginFiles.size());
var indexJsPath = pluginFiles.get("index.js");
assertEquals("""
'use strict';
allure.api.addTranslation('en', {"tab":{"suites":{"name":"Batches Tab"}}});
""", Files.readString(indexJsPath).replaceAll("\r\n|\n", System.lineSeparator()));
}
}

0 comments on commit 04989de

Please # to comment.