Skip to content

Commit

Permalink
feat: more common json formatting (#2432)
Browse files Browse the repository at this point in the history
remove a space before the colon at json format.
  • Loading branch information
huglx committed Sep 13, 2024
1 parent b466321 commit bef8218
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -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(": ")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ class GenericStructuredFileExporter(
private val rootKeyIsLanguageTag: Boolean = false,
private val supportArrays: Boolean,
private val messageFormat: ExportMessageFormat,
private val customPrettyPrinter: CustomPrettyPrinter,
) : FileExporter {
val result: LinkedHashMap<String, StructureModelBuilder> = LinkedHashMap()

override fun produceFiles(): Map<String, InputStream> {
prepare()
return result.asSequence().map { (fileName, modelBuilder) ->
fileName to
objectMapper.writerWithDefaultPrettyPrinter().writeValueAsBytes(modelBuilder.result)
objectMapper.writer(customPrettyPrinter).writeValueAsBytes(modelBuilder.result)
.inputStream()
}.toMap()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,6 +16,7 @@ class JsonFileExporter(
val exportParams: IExportParams,
projectIcuPlaceholdersSupport: Boolean,
val objectMapper: ObjectMapper,
customPrettyPrinter: CustomPrettyPrinter,
) : FileExporter {
private val fileExtension: String = ExportFormat.JSON.extension

Expand All @@ -33,6 +35,7 @@ class JsonFileExporter(
objectMapper = objectMapper,
supportArrays = supportArrays,
messageFormat = messageFormat,
customPrettyPrinter = customPrettyPrinter,
)

private val supportArrays
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,6 +16,7 @@ class YamlFileExporter(
val exportParams: IExportParams,
objectMapper: ObjectMapper,
projectIcuPlaceholdersSupport: Boolean,
customPrettyPrinter: CustomPrettyPrinter,
) : FileExporter {
private val fileExtension: String = exportParams.format.extension

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,6 +24,7 @@ class FileExporterFactory(
private val objectMapper: ObjectMapper,
@Qualifier("yamlObjectMapper")
private val yamlObjectMapper: ObjectMapper,
private val customPrettyPrinter: CustomPrettyPrinter,
) {
fun create(
data: List<ExportTranslationView>,
Expand All @@ -38,6 +40,7 @@ class FileExporterFactory(
exportParams,
objectMapper = objectMapper,
projectIcuPlaceholdersSupport = projectIcuPlaceholdersSupport,
customPrettyPrinter = customPrettyPrinter,
)

ExportFormat.YAML_RUBY, ExportFormat.YAML ->
Expand All @@ -46,6 +49,7 @@ class FileExporterFactory(
exportParams,
objectMapper = yamlObjectMapper,
projectIcuPlaceholdersSupport = projectIcuPlaceholdersSupport,
customPrettyPrinter,
)

ExportFormat.XLIFF ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
)
Expand Down Expand Up @@ -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<ExportTranslationView> {
val built =
buildExportTranslationList {
add(
languageTag = "cs",
keyName = "item",
text = "name : {name}",
)
}
return built.translations
}

private fun getIcuPlaceholdersEnabledExporter(): JsonFileExporter {
val built =
buildExportTranslationList {
Expand Down Expand Up @@ -180,7 +207,7 @@ class JsonFileExporterTest {
"cs.json",
"""
|{
| "item" : "I will be first '{'icuParam'}' %<hello>d"
| "item": "I will be first '{'icuParam'}' %<hello>d"
|}
""".trimMargin(),
)
Expand Down Expand Up @@ -285,6 +312,7 @@ class JsonFileExporterTest {
exportParams = exportParams,
projectIcuPlaceholdersSupport = isProjectIcuPlaceholdersEnabled,
objectMapper = jacksonObjectMapper(),
customPrettyPrinter = CustomPrettyPrinter(),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -96,6 +97,7 @@ object YamlExportTestData {
},
projectIcuPlaceholdersSupport = isProjectIcuPlaceholdersEnabled,
objectMapper = ObjectMapper(YAMLFactory()),
customPrettyPrinter = CustomPrettyPrinter(),
)
}
}

0 comments on commit bef8218

Please sign in to comment.