-
-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
145 additions
and
0 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
46 changes: 46 additions & 0 deletions
46
backend/data/src/main/kotlin/io/tolgee/formats/csv/in/CSVImportFormatDetector.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,46 @@ | ||
package io.tolgee.formats.csv.`in` | ||
|
||
import io.tolgee.formats.genericStructuredFile.`in`.FormatDetectionUtil | ||
import io.tolgee.formats.genericStructuredFile.`in`.FormatDetectionUtil.ICU_DETECTION_REGEX | ||
import io.tolgee.formats.genericStructuredFile.`in`.FormatDetectionUtil.detectFromPossibleFormats | ||
import io.tolgee.formats.importCommon.ImportFormat | ||
import io.tolgee.formats.paramConvertors.`in`.JavaToIcuPlaceholderConvertor | ||
import io.tolgee.formats.paramConvertors.`in`.PhpToIcuPlaceholderConvertor | ||
import io.tolgee.formats.paramConvertors.`in`.RubyToIcuPlaceholderConvertor | ||
|
||
class CSVImportFormatDetector { | ||
companion object { | ||
private val possibleFormats = | ||
mapOf( | ||
ImportFormat.CSV_ICU to | ||
arrayOf( | ||
FormatDetectionUtil.regexFactor( | ||
ICU_DETECTION_REGEX, | ||
), | ||
), | ||
ImportFormat.CSV_PHP to | ||
arrayOf( | ||
FormatDetectionUtil.regexFactor( | ||
PhpToIcuPlaceholderConvertor.PHP_DETECTION_REGEX, | ||
), | ||
), | ||
ImportFormat.CSV_JAVA to | ||
arrayOf( | ||
FormatDetectionUtil.regexFactor( | ||
JavaToIcuPlaceholderConvertor.JAVA_DETECTION_REGEX, | ||
), | ||
), | ||
ImportFormat.CSV_RUBY to | ||
arrayOf( | ||
FormatDetectionUtil.regexFactor( | ||
RubyToIcuPlaceholderConvertor.RUBY_DETECTION_REGEX, | ||
0.95, | ||
), | ||
), | ||
) | ||
} | ||
|
||
fun detectFormat(data: List<Array<String>>): ImportFormat { | ||
return detectFromPossibleFormats(possibleFormats, data) ?: ImportFormat.CSV_ICU | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
backend/data/src/main/kotlin/io/tolgee/formats/csv/in/CsvFileProcessor.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,67 @@ | ||
package io.tolgee.formats.csv.`in` | ||
|
||
import com.opencsv.CSVParserBuilder | ||
import com.opencsv.CSVReaderBuilder | ||
import io.tolgee.exceptions.ImportCannotParseFileException | ||
import io.tolgee.formats.ImportFileProcessor | ||
import io.tolgee.formats.importCommon.ImportFormat | ||
import io.tolgee.service.dataImport.processors.FileProcessorContext | ||
|
||
class CsvFileProcessor( | ||
override val context: FileProcessorContext, | ||
) : ImportFileProcessor() { | ||
|
||
override fun process() { | ||
val inputStream = context.file.data.inputStream() | ||
val reader = inputStream.reader() | ||
val data: List<Array<String>> | ||
|
||
CSVReaderBuilder(reader) | ||
.withCSVParser( | ||
CSVParserBuilder() | ||
.withSeparator(';') // TODO make delimiter parametrizable | ||
.build() | ||
).build().use { csvReader -> data = csvReader.readAll() } | ||
|
||
|
||
val format = getFormat(data) | ||
|
||
// Read the first line to extract headers (language keys) - "key_name", ...languages | ||
val headers = data.firstOrNull() | ||
val languages = headers?.drop(1) ?: emptyList() | ||
|
||
// Parse body - key_name, ...translations | ||
for ((idx, row) in data.drop(1).withIndex()) { | ||
val keyName = row.getOrNull(0) ?: throw ImportCannotParseFileException(context.file.name, "empty row $idx") | ||
|
||
for (i in 1 until row.size) { | ||
val languageTag = languages.getOrNull(i - 1) ?: throw ImportCannotParseFileException( | ||
context.file.name, "more translations than defined languages in row $idx" | ||
) | ||
val translation = row.getOrNull(i) ?: throw ImportCannotParseFileException( | ||
context.file.name, "missing translation in row $idx" | ||
) | ||
|
||
val converted = format.messageConvertor.convert( | ||
translation, languageTag, | ||
convertPlaceholders = context.importSettings.convertPlaceholdersToIcu, | ||
isProjectIcuEnabled = context.projectIcuPlaceholdersEnabled | ||
) | ||
context.addTranslation( | ||
keyName, | ||
languageTag, | ||
converted.message, | ||
idx, | ||
pluralArgName = converted.pluralArgName, | ||
rawData = row[i], | ||
convertedBy = format, | ||
) | ||
} | ||
} | ||
} | ||
|
||
private fun getFormat(data: List<Array<String>>): ImportFormat { | ||
return context.mapping?.format ?: CSVImportFormatDetector().detectFormat(data) | ||
} | ||
|
||
} |
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