From bef82184e52c6c0d7c5b243e1f2156f69a839184 Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 13 Sep 2024 12:57:33 +0200 Subject: [PATCH] feat: more common json formatting (#2432) remove a space before the colon at json format. --- .../CustomPrettyPrinterConfiguration.kt | 13 +++++++ .../out/CustomPrettyPrinter.kt | 14 +++++++ .../out/GenericStructuredFileExporter.kt | 3 +- .../formats/json/out/JsonFileExporter.kt | 3 ++ .../formats/yaml/out/YamlFileExporter.kt | 7 +++- .../service/export/FileExporterFactory.kt | 4 ++ .../formats/json/out/JsonFileExporterTest.kt | 38 ++++++++++++++++--- .../formats/yaml/out/YamlExportTestData.kt | 2 + 8 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 backend/data/src/main/kotlin/io/tolgee/configuration/CustomPrettyPrinterConfiguration.kt create mode 100644 backend/data/src/main/kotlin/io/tolgee/formats/genericStructuredFile/out/CustomPrettyPrinter.kt diff --git a/backend/data/src/main/kotlin/io/tolgee/configuration/CustomPrettyPrinterConfiguration.kt b/backend/data/src/main/kotlin/io/tolgee/configuration/CustomPrettyPrinterConfiguration.kt new file mode 100644 index 0000000000..ee700f2f61 --- /dev/null +++ b/backend/data/src/main/kotlin/io/tolgee/configuration/CustomPrettyPrinterConfiguration.kt @@ -0,0 +1,13 @@ +package io.tolgee.configuration + +import io.tolgee.formats.genericStructuredFile.out.CustomPrettyPrinter +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration + +@Configuration +class CustomPrettyPrinterConfiguration { + @Bean + fun customPrettyPrinter(): CustomPrettyPrinter { + return CustomPrettyPrinter() + } +} diff --git a/backend/data/src/main/kotlin/io/tolgee/formats/genericStructuredFile/out/CustomPrettyPrinter.kt b/backend/data/src/main/kotlin/io/tolgee/formats/genericStructuredFile/out/CustomPrettyPrinter.kt new file mode 100644 index 0000000000..595815d51c --- /dev/null +++ b/backend/data/src/main/kotlin/io/tolgee/formats/genericStructuredFile/out/CustomPrettyPrinter.kt @@ -0,0 +1,14 @@ +package io.tolgee.formats.genericStructuredFile.out + +import com.fasterxml.jackson.core.JsonGenerator +import com.fasterxml.jackson.core.util.DefaultPrettyPrinter + +class CustomPrettyPrinter : DefaultPrettyPrinter() { + override fun createInstance(): DefaultPrettyPrinter { + return CustomPrettyPrinter() + } + + override fun writeObjectFieldValueSeparator(jg: JsonGenerator) { + jg.writeRaw(": ") + } +} diff --git a/backend/data/src/main/kotlin/io/tolgee/formats/genericStructuredFile/out/GenericStructuredFileExporter.kt b/backend/data/src/main/kotlin/io/tolgee/formats/genericStructuredFile/out/GenericStructuredFileExporter.kt index ae5f884f36..80f19221b1 100644 --- a/backend/data/src/main/kotlin/io/tolgee/formats/genericStructuredFile/out/GenericStructuredFileExporter.kt +++ b/backend/data/src/main/kotlin/io/tolgee/formats/genericStructuredFile/out/GenericStructuredFileExporter.kt @@ -19,6 +19,7 @@ class GenericStructuredFileExporter( private val rootKeyIsLanguageTag: Boolean = false, private val supportArrays: Boolean, private val messageFormat: ExportMessageFormat, + private val customPrettyPrinter: CustomPrettyPrinter, ) : FileExporter { val result: LinkedHashMap = LinkedHashMap() @@ -26,7 +27,7 @@ class GenericStructuredFileExporter( prepare() return result.asSequence().map { (fileName, modelBuilder) -> fileName to - objectMapper.writerWithDefaultPrettyPrinter().writeValueAsBytes(modelBuilder.result) + objectMapper.writer(customPrettyPrinter).writeValueAsBytes(modelBuilder.result) .inputStream() }.toMap() } diff --git a/backend/data/src/main/kotlin/io/tolgee/formats/json/out/JsonFileExporter.kt b/backend/data/src/main/kotlin/io/tolgee/formats/json/out/JsonFileExporter.kt index aa9f566767..59862c93cc 100644 --- a/backend/data/src/main/kotlin/io/tolgee/formats/json/out/JsonFileExporter.kt +++ b/backend/data/src/main/kotlin/io/tolgee/formats/json/out/JsonFileExporter.kt @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper import io.tolgee.dtos.IExportParams import io.tolgee.formats.ExportFormat import io.tolgee.formats.ExportMessageFormat +import io.tolgee.formats.genericStructuredFile.out.CustomPrettyPrinter import io.tolgee.formats.genericStructuredFile.out.GenericStructuredFileExporter import io.tolgee.formats.nestedStructureModel.StructureModelBuilder import io.tolgee.service.export.dataProvider.ExportTranslationView @@ -15,6 +16,7 @@ class JsonFileExporter( val exportParams: IExportParams, projectIcuPlaceholdersSupport: Boolean, val objectMapper: ObjectMapper, + customPrettyPrinter: CustomPrettyPrinter, ) : FileExporter { private val fileExtension: String = ExportFormat.JSON.extension @@ -33,6 +35,7 @@ class JsonFileExporter( objectMapper = objectMapper, supportArrays = supportArrays, messageFormat = messageFormat, + customPrettyPrinter = customPrettyPrinter, ) private val supportArrays diff --git a/backend/data/src/main/kotlin/io/tolgee/formats/yaml/out/YamlFileExporter.kt b/backend/data/src/main/kotlin/io/tolgee/formats/yaml/out/YamlFileExporter.kt index b7987e0c58..e2615436e3 100644 --- a/backend/data/src/main/kotlin/io/tolgee/formats/yaml/out/YamlFileExporter.kt +++ b/backend/data/src/main/kotlin/io/tolgee/formats/yaml/out/YamlFileExporter.kt @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper import io.tolgee.dtos.IExportParams import io.tolgee.formats.ExportFormat import io.tolgee.formats.ExportMessageFormat +import io.tolgee.formats.genericStructuredFile.out.CustomPrettyPrinter import io.tolgee.formats.genericStructuredFile.out.GenericStructuredFileExporter import io.tolgee.formats.nestedStructureModel.StructureModelBuilder import io.tolgee.service.export.dataProvider.ExportTranslationView @@ -15,6 +16,7 @@ class YamlFileExporter( val exportParams: IExportParams, objectMapper: ObjectMapper, projectIcuPlaceholdersSupport: Boolean, + customPrettyPrinter: CustomPrettyPrinter, ) : FileExporter { private val fileExtension: String = exportParams.format.extension @@ -28,12 +30,13 @@ class YamlFileExporter( GenericStructuredFileExporter( translations = translations, exportParams = exportParams, - projectIcuPlaceholdersSupport = projectIcuPlaceholdersSupport, fileExtension = fileExtension, + projectIcuPlaceholdersSupport = projectIcuPlaceholdersSupport, objectMapper = objectMapper, rootKeyIsLanguageTag = rootKeyIsLanguageTag, - messageFormat = messageFormat, supportArrays = supportArrays, + messageFormat = messageFormat, + customPrettyPrinter = customPrettyPrinter, ) private val rootKeyIsLanguageTag diff --git a/backend/data/src/main/kotlin/io/tolgee/service/export/FileExporterFactory.kt b/backend/data/src/main/kotlin/io/tolgee/service/export/FileExporterFactory.kt index ce16158853..fe8b9d2ce2 100644 --- a/backend/data/src/main/kotlin/io/tolgee/service/export/FileExporterFactory.kt +++ b/backend/data/src/main/kotlin/io/tolgee/service/export/FileExporterFactory.kt @@ -8,6 +8,7 @@ import io.tolgee.formats.android.out.AndroidStringsXmlExporter import io.tolgee.formats.apple.out.AppleStringsStringsdictExporter import io.tolgee.formats.apple.out.AppleXliffExporter import io.tolgee.formats.flutter.out.FlutterArbFileExporter +import io.tolgee.formats.genericStructuredFile.out.CustomPrettyPrinter import io.tolgee.formats.json.out.JsonFileExporter import io.tolgee.formats.po.out.PoFileExporter import io.tolgee.formats.properties.out.PropertiesFileExporter @@ -23,6 +24,7 @@ class FileExporterFactory( private val objectMapper: ObjectMapper, @Qualifier("yamlObjectMapper") private val yamlObjectMapper: ObjectMapper, + private val customPrettyPrinter: CustomPrettyPrinter, ) { fun create( data: List, @@ -38,6 +40,7 @@ class FileExporterFactory( exportParams, objectMapper = objectMapper, projectIcuPlaceholdersSupport = projectIcuPlaceholdersSupport, + customPrettyPrinter = customPrettyPrinter, ) ExportFormat.YAML_RUBY, ExportFormat.YAML -> @@ -46,6 +49,7 @@ class FileExporterFactory( exportParams, objectMapper = yamlObjectMapper, projectIcuPlaceholdersSupport = projectIcuPlaceholdersSupport, + customPrettyPrinter, ) ExportFormat.XLIFF -> diff --git a/backend/data/src/test/kotlin/io/tolgee/unit/formats/json/out/JsonFileExporterTest.kt b/backend/data/src/test/kotlin/io/tolgee/unit/formats/json/out/JsonFileExporterTest.kt index 501582b243..dfb642bd60 100644 --- a/backend/data/src/test/kotlin/io/tolgee/unit/formats/json/out/JsonFileExporterTest.kt +++ b/backend/data/src/test/kotlin/io/tolgee/unit/formats/json/out/JsonFileExporterTest.kt @@ -4,6 +4,7 @@ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue import io.tolgee.dtos.request.export.ExportParams import io.tolgee.formats.ExportMessageFormat +import io.tolgee.formats.genericStructuredFile.out.CustomPrettyPrinter import io.tolgee.formats.json.out.JsonFileExporter import io.tolgee.model.enums.TranslationState import io.tolgee.service.export.dataProvider.ExportKeyView @@ -100,8 +101,8 @@ class JsonFileExporterTest { "cs.json", """ |{ - | "key3" : "{count, plural, one {# den {icuParam}} few {# dny} other {# dní}}", - | "item" : "I will be first {icuParam, number}" + | "key3": "{count, plural, one {# den {icuParam}} few {# dny} other {# dní}}", + | "item": "I will be first {icuParam, number}" |} """.trimMargin(), ) @@ -134,13 +135,39 @@ class JsonFileExporterTest { "cs.json", """ |{ - | "key3" : "{count, plural, one {# den {icuParam, number}} few {# dny} other {# dní}}", - | "item" : "I will be first '{'icuParam'}' {hello, number}" + | "key3": "{count, plural, one {# den {icuParam, number}} few {# dny} other {# dní}}", + | "item": "I will be first '{'icuParam'}' {hello, number}" |} """.trimMargin(), ) } + @Test + fun `correct exports translation with colon`() { + val exporter = getExporter(getTranslationWithColon()) + val data = getExported(exporter) + data.assertFile( + "cs.json", + """ + |{ + | "item": "name : {name}" + |} + """.trimMargin(), + ) + } + + private fun getTranslationWithColon(): MutableList { + val built = + buildExportTranslationList { + add( + languageTag = "cs", + keyName = "item", + text = "name : {name}", + ) + } + return built.translations + } + private fun getIcuPlaceholdersEnabledExporter(): JsonFileExporter { val built = buildExportTranslationList { @@ -180,7 +207,7 @@ class JsonFileExporterTest { "cs.json", """ |{ - | "item" : "I will be first '{'icuParam'}' %d" + | "item": "I will be first '{'icuParam'}' %d" |} """.trimMargin(), ) @@ -285,6 +312,7 @@ class JsonFileExporterTest { exportParams = exportParams, projectIcuPlaceholdersSupport = isProjectIcuPlaceholdersEnabled, objectMapper = jacksonObjectMapper(), + customPrettyPrinter = CustomPrettyPrinter(), ) } } diff --git a/backend/data/src/test/kotlin/io/tolgee/unit/formats/yaml/out/YamlExportTestData.kt b/backend/data/src/test/kotlin/io/tolgee/unit/formats/yaml/out/YamlExportTestData.kt index cc6464a945..b28f2813e9 100644 --- a/backend/data/src/test/kotlin/io/tolgee/unit/formats/yaml/out/YamlExportTestData.kt +++ b/backend/data/src/test/kotlin/io/tolgee/unit/formats/yaml/out/YamlExportTestData.kt @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.dataformat.yaml.YAMLFactory import io.tolgee.dtos.request.export.ExportParams import io.tolgee.formats.ExportFormat +import io.tolgee.formats.genericStructuredFile.out.CustomPrettyPrinter import io.tolgee.formats.yaml.out.YamlFileExporter import io.tolgee.service.export.dataProvider.ExportTranslationView import io.tolgee.util.buildExportTranslationList @@ -96,6 +97,7 @@ object YamlExportTestData { }, projectIcuPlaceholdersSupport = isProjectIcuPlaceholdersEnabled, objectMapper = ObjectMapper(YAMLFactory()), + customPrettyPrinter = CustomPrettyPrinter(), ) } }