Skip to content

Commit

Permalink
fix: structural and readability fixes + removing duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
Anty0 committed Sep 16, 2024
1 parent 29e1b15 commit d222a4f
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package io.tolgee.formats.genericStructuredFile.`in`
import io.tolgee.formats.ImportFileProcessor
import io.tolgee.formats.MessageConvertorResult
import io.tolgee.formats.allPluralKeywords
import io.tolgee.formats.i18next.ParsedI18nextKey
import io.tolgee.formats.i18next.PluralsI18nextKeyParser
import io.tolgee.formats.importCommon.ImportFormat
import io.tolgee.formats.importCommon.ParsedPluralsKey
import io.tolgee.formats.importCommon.PluralsKeyParser
import io.tolgee.service.dataImport.processors.FileProcessorContext

class GenericStructuredProcessor(
Expand All @@ -15,8 +15,6 @@ class GenericStructuredProcessor(
private val languageTag: String? = null,
private val format: ImportFormat,
) : ImportFileProcessor() {
private val keyParser = PluralsI18nextKeyParser()

override fun process() {
data.preprocess().import("")
}
Expand All @@ -41,44 +39,49 @@ class GenericStructuredProcessor(
return this.map { it.preprocess() }
}

private fun Map<*, *>.groupByPlurals(keyRegex: Regex): Map<String?, List<Pair<ParsedI18nextKey, Any?>>> {
private fun Any?.parsePluralsKey(keyParser: PluralsKeyParser): ParsedPluralsKey? {
val key = this as? String ?: return null
return keyParser.parse(key).takeIf {
it.key != null && it.plural in allPluralKeywords
} ?: ParsedPluralsKey(null, null, key)
}

private fun Map<*, *>.groupByPlurals(
keyParser: PluralsKeyParser
): Map<String?, List<Pair<ParsedPluralsKey, Any?>>> {
return this.entries.mapIndexedNotNull { idx, (key, value) ->
if (key !is String) {
context.fileEntity.addKeyIsNotStringIssue(key.toString(), idx)
return@mapIndexedNotNull null
key.parsePluralsKey(keyParser)?.let { it to value }.also {
if (it == null) {
context.fileEntity.addKeyIsNotStringIssue(key.toString(), idx)
}
}
val default = ParsedI18nextKey(null, null, key)

val match = keyRegex.find(key) ?: return@mapIndexedNotNull default to value
val parsedKey = keyParser.parse(match)
}.groupBy { (parsedKey, _) -> parsedKey.key }.toMap()
}

if (parsedKey?.key == null || parsedKey.plural == null || parsedKey.plural !in allPluralKeywords) {
return@mapIndexedNotNull default to value
}
private fun List<Pair<ParsedPluralsKey, Any?>>.useOriginalKey(): List<Pair<String, Any?>> {
return map { (parsedKey, value) ->
parsedKey.originalKey to value
}
}

return@mapIndexedNotNull parsedKey to value
}.groupBy { (parsedKey, _) ->
parsedKey.key
}.toMap()
private fun List<Pair<ParsedPluralsKey, Any?>>.usePluralsKey(commonKey: String): List<Pair<String, Any?>> {
return listOf(commonKey to this.associate { (parsedKey, value) ->
parsedKey.plural to value
})
}

private fun Map<*, *>.preprocessMap(): Map<*, *> {
if (format.pluralsViaSuffixesRegex == null) {
if (format.pluralsViaSuffixesParser == null) {
return this.mapValues { (_, value) -> value.preprocess() }
}

val plurals = this.groupByPlurals(format.pluralsViaSuffixesRegex)
val plurals = this.groupByPlurals(format.pluralsViaSuffixesParser)

return plurals.flatMap { (key, values) ->
if (key == null || values.size < 2) {
// Fallback for non-plural keys
values.map { (parsedKey, value) ->
parsedKey.fullMatch to value
}
return plurals.flatMap { (commonKey, values) ->
if (commonKey == null || values.size < 2) {
values.useOriginalKey()
} else {
listOf(key to values.map { (parsedKey, value) ->
parsedKey.plural to value
}.toMap())
values.usePluralsKey(commonKey)
}
}.toMap()
}
Expand Down
12 changes: 12 additions & 0 deletions backend/data/src/main/kotlin/io/tolgee/formats/getGroupOrNull.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.tolgee.formats

fun MatchGroupCollection.getGroupOrNull(name: String): MatchGroup? {
try {
return this[name]
} catch (e: IllegalArgumentException) {
if (e.message?.contains("No group with name") != true) {
throw e
}
return null
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package io.tolgee.formats.i18next
package io.tolgee.formats.i18next.`in`

import io.tolgee.formats.getGroupOrNull

class I18nextParameterParser {
fun parse(match: MatchResult): ParsedI18nextParam? {
Expand All @@ -9,16 +11,4 @@ class I18nextParameterParser {
fullMatch = match.value,
)
}

// FIXME: move somewhere shared
private fun MatchGroupCollection.getGroupOrNull(name: String): MatchGroup? {
try {
return this[name]
} catch (e: IllegalArgumentException) {
if (e.message?.contains("No group with name") != true) {
throw e
}
return null
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.tolgee.formats.i18next
package io.tolgee.formats.i18next.`in`

data class ParsedI18nextParam(
val key: String? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.tolgee.formats.i18next.`in`

import io.tolgee.formats.getGroupOrNull
import io.tolgee.formats.importCommon.ParsedPluralsKey
import io.tolgee.formats.importCommon.PluralsKeyParser

class PluralsI18nextKeyParser(private val keyRegex: Regex) : PluralsKeyParser {
override fun parse(key: String): ParsedPluralsKey {
val match = keyRegex.find(key)
return ParsedPluralsKey(
key = match?.groups?.getGroupOrNull("key")?.value,
plural = match?.groups?.getGroupOrNull("plural")?.value,
originalKey = key,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import io.tolgee.formats.po.`in`.PoToIcuMessageConvertor
enum class ImportFormat(
val fileFormat: ImportFileFormat,
val pluralsViaNesting: Boolean = false,
val pluralsViaSuffixesRegex: Regex? = null,
val pluralsViaSuffixesParser: PluralsKeyParser? = null,
val messageConvertorOrNull: ImportMessageConvertor? = null,
val rootKeyIsLanguageTag: Boolean = false,
) {
JSON_I18NEXT(
ImportFileFormat.JSON,
messageConvertorOrNull = GenericMapPluralImportRawDataConvertor { I18nextToIcuPlaceholderConvertor() },
pluralsViaSuffixesRegex = I18nextToIcuPlaceholderConvertor.I18NEXT_PLURAL_SUFFIX_REGEX,
pluralsViaSuffixesParser = I18nextToIcuPlaceholderConvertor.I18NEXT_PLURAL_SUFFIX_KEY_PARSER,
),
JSON_ICU(
ImportFileFormat.JSON,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.tolgee.formats.importCommon

data class ParsedPluralsKey(
val key: String? = null,
val plural: String? = null,
val originalKey: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.tolgee.formats.importCommon

interface PluralsKeyParser {
fun parse(key: String): ParsedPluralsKey
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package io.tolgee.formats.paramConvertors.`in`

import io.tolgee.formats.ToIcuPlaceholderConvertor
import io.tolgee.formats.escapeIcu
import io.tolgee.formats.i18next.I18nextParameterParser
import io.tolgee.formats.i18next.`in`.I18nextParameterParser
import io.tolgee.formats.i18next.`in`.PluralsI18nextKeyParser

class I18nextToIcuPlaceholderConvertor : ToIcuPlaceholderConvertor {
private val parser = I18nextParameterParser()
Expand Down Expand Up @@ -64,5 +65,7 @@ class I18nextToIcuPlaceholderConvertor : ToIcuPlaceholderConvertor {

val I18NEXT_PLURAL_SUFFIX_REGEX = """^(?<key>\w+)_(?<plural>\w+)$""".toRegex()

val I18NEXT_PLURAL_SUFFIX_KEY_PARSER = PluralsI18nextKeyParser(I18NEXT_PLURAL_SUFFIX_REGEX)

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.tolgee.formats.po.`in`

import io.tolgee.formats.getGroupOrNull

class CLikeParameterParser {
fun parse(match: MatchResult): ParsedCLikeParam? {
val specifierGroup = match.groups["specifier"]
Expand All @@ -18,15 +20,4 @@ class CLikeParameterParser {
fullMatch = match.value,
)
}

private fun MatchGroupCollection.getGroupOrNull(name: String): MatchGroup? {
try {
return this[name]
} catch (e: IllegalArgumentException) {
if (e.message?.contains("No group with name") != true) {
throw e
}
return null
}
}
}

0 comments on commit d222a4f

Please sign in to comment.