-
-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: xlsx import and export (#2829)
Fixes #2555
- Loading branch information
Showing
39 changed files
with
1,792 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 7 additions & 58 deletions
65
backend/data/src/main/kotlin/io/tolgee/formats/csv/out/CsvFileExporter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
backend/data/src/main/kotlin/io/tolgee/formats/csv/out/CsvFileWriter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
.../kotlin/io/tolgee/formats/csv/CsvModel.kt → ...tolgee/formats/genericTable/TableModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
backend/data/src/main/kotlin/io/tolgee/formats/genericTable/in/TableParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.tolgee.formats.genericTable.`in` | ||
|
||
import io.tolgee.formats.genericTable.TableEntry | ||
|
||
class TableParser( | ||
private val rawData: List<List<String>>, | ||
private val languageFallback: String, | ||
) { | ||
val headers: List<String>? by lazy { | ||
rawData.firstOrNull() | ||
} | ||
|
||
val languages: List<String> by lazy { | ||
headers?.takeIf { it.size > 1 }?.drop(1) ?: emptyList() | ||
} | ||
|
||
val languagesWithFallback: Sequence<String> | ||
get() = languages.asSequence().plus(generateSequence { languageFallback }) | ||
|
||
val rows: List<List<String>> by lazy { | ||
rawData.takeIf { it.size > 1 }?.drop(1) ?: emptyList() | ||
} | ||
|
||
fun List<String>.rowToTableEntries(): Sequence<TableEntry> { | ||
if (isEmpty()) { | ||
return emptySequence() | ||
} | ||
val keyName = getOrNull(0) ?: "" | ||
if (size == 1) { | ||
return sequenceOf(TableEntry(keyName, languageFallback, null)) | ||
} | ||
val translations = drop(1).asSequence() | ||
return translations | ||
.zip(languagesWithFallback) | ||
.map { (translation, languageTag) -> | ||
TableEntry( | ||
keyName, | ||
languageTag, | ||
translation, | ||
) | ||
} | ||
} | ||
|
||
fun parse(): List<TableEntry> { | ||
return rows.flatMap { | ||
it.rowToTableEntries() | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
backend/data/src/main/kotlin/io/tolgee/formats/genericTable/in/TableProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.tolgee.formats.genericTable.`in` | ||
|
||
import io.tolgee.formats.ImportFileProcessor | ||
import io.tolgee.formats.genericTable.TableEntry | ||
import io.tolgee.formats.importCommon.ImportFormat | ||
import io.tolgee.service.dataImport.processors.FileProcessorContext | ||
|
||
abstract class TableProcessor( | ||
override val context: FileProcessorContext, | ||
) : ImportFileProcessor() { | ||
override fun process() { | ||
val (data, format) = parse() | ||
data.importAll(format) | ||
} | ||
|
||
fun Iterable<TableEntry>.importAll(format: ImportFormat) { | ||
forEachIndexed { idx, it -> it.import(idx, format) } | ||
} | ||
|
||
fun TableEntry.import( | ||
index: Int, | ||
format: ImportFormat, | ||
) { | ||
val converted = | ||
format.messageConvertor.convert( | ||
value, | ||
language, | ||
convertPlaceholders = context.importSettings.convertPlaceholdersToIcu, | ||
isProjectIcuEnabled = context.projectIcuPlaceholdersEnabled, | ||
) | ||
context.addTranslation( | ||
key, | ||
language, | ||
converted.message, | ||
index, | ||
pluralArgName = converted.pluralArgName, | ||
rawData = value, | ||
convertedBy = format, | ||
) | ||
} | ||
|
||
protected abstract fun parse(): Pair<Iterable<TableEntry>, ImportFormat> | ||
} |
Oops, something went wrong.