diff --git a/docs/modules/configuration/pages/reporting.adoc b/docs/modules/configuration/pages/reporting.adoc index 179e9a1daa..be9bfcc7bf 100644 --- a/docs/modules/configuration/pages/reporting.adoc +++ b/docs/modules/configuration/pages/reporting.adoc @@ -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..` +|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 +---- + |=== diff --git a/vividus-allure-adaptor/src/main/java/org/vividus/report/allure/plugin/CustomTranslationsPlugin.java b/vividus-allure-adaptor/src/main/java/org/vividus/report/allure/plugin/CustomTranslationsPlugin.java new file mode 100644 index 0000000000..19e6f55e36 --- /dev/null +++ b/vividus-allure-adaptor/src/main/java/org/vividus/report/allure/plugin/CustomTranslationsPlugin.java @@ -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 pluginFiles; + + @SuppressFBWarnings("CT_CONSTRUCTOR_THROW") + public CustomTranslationsPlugin(PropertyMappedCollection> customTranslations, JsonUtils jsonUtils) + throws IOException + { + super(new PluginConfiguration() + .setId("custom-translations") + .setJsFiles(List.of(INDEX_JS)), + List.of(), null); + + List 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 getPluginFiles() + { + return pluginFiles; + } +} diff --git a/vividus-allure-adaptor/src/main/resources/org/vividus/report/allure/spring.xml b/vividus-allure-adaptor/src/main/resources/org/vividus/report/allure/spring.xml index 961dd6b239..d20bfce1ed 100644 --- a/vividus-allure-adaptor/src/main/resources/org/vividus/report/allure/spring.xml +++ b/vividus-allure-adaptor/src/main/resources/org/vividus/report/allure/spring.xml @@ -52,6 +52,15 @@ + + + + + + + + + diff --git a/vividus-allure-adaptor/src/test/java/org/vividus/report/allure/plugin/CustomTranslationsPluginTests.java b/vividus-allure-adaptor/src/test/java/org/vividus/report/allure/plugin/CustomTranslationsPluginTests.java new file mode 100644 index 0000000000..3a09ffb7b1 --- /dev/null +++ b/vividus-allure-adaptor/src/test/java/org/vividus/report/allure/plugin/CustomTranslationsPluginTests.java @@ -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())); + } +}