Skip to content

Commit

Permalink
feat: Add simple properties file importer (#2050)
Browse files Browse the repository at this point in the history
See #2042

---------

Co-authored-by: Benoît Allard <benoit.allard@sla.de>
  • Loading branch information
benallard and allardbe-sla committed Dec 28, 2023
1 parent 1e5cdbb commit 11dd90f
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ProcessorFactory {
"po" -> PoFileProcessor(context)
"xliff" -> XliffFileProcessor(context)
"xlf" -> XliffFileProcessor(context)
"properties" -> PropertyFileProcessor(context)
else -> throw ImportCannotParseFileException(file.name, "No matching processor")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.tolgee.service.dataImport.processors

import java.util.Properties

class PropertyFileProcessor(
override val context: FileProcessorContext,
) : ImportFileProcessor() {
override fun process() {
val props = Properties()
props.load(context.file.inputStream)
props.entries.forEachIndexed { idx, it ->
context.addTranslation(it.key.toString(), languageNameGuesses[0], it.value, idx)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.tolgee.unit.service.dataImport.processors.processors

import io.tolgee.dtos.dataImport.ImportFileDto
import io.tolgee.model.dataImport.Import
import io.tolgee.model.dataImport.ImportFile
import io.tolgee.service.dataImport.processors.FileProcessorContext
import io.tolgee.service.dataImport.processors.PropertyFileProcessor
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.kotlin.mock
import java.io.File

class PropertiesParserTest {
private lateinit var importMock: Import
private lateinit var importFile: ImportFile
private lateinit var importFileDto: ImportFileDto
private lateinit var fileProcessorContext: FileProcessorContext

@BeforeEach
fun setup() {
importMock = mock()
importFile = ImportFile("messages_en.properties", importMock)
importFileDto = ImportFileDto(
"messages_en.properties",
File("src/test/resources/import/example.properties")
.inputStream()
)
fileProcessorContext = FileProcessorContext(importFileDto, importFile)
}

@Test
fun `returns correct parsed result`() {
PropertyFileProcessor(fileProcessorContext).process()
Assertions.assertThat(fileProcessorContext.languages).hasSize(1)
Assertions.assertThat(fileProcessorContext.translations).hasSize(4)
val text = fileProcessorContext.translations["Register"]?.get(0)?.text
Assertions.assertThat(text).isEqualTo("Veuillez vous enregistrer sur la page suivante.")
val multiLineText = fileProcessorContext.translations["Cleanup"]?.get(0)?.text
Assertions.assertThat(multiLineText).hasLineCount(3)
}
}
7 changes: 7 additions & 0 deletions backend/data/src/test/resources/import/example.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Localization_tools=Lokaliza?ní nástroje

Welcome=Herzlich Wilkommen
# This is a comment
Register=Veuillez vous enregistrer \
sur la page suivante.
Cleanup=Veeg uw\n voeten\n voordat u binnengaat.
2 changes: 1 addition & 1 deletion webapp/src/i18n/cs.json
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@
"import_file_issue_param_type_value" : "hodnota: {value}",
"import_file_issues_title" : "Problémy souboru",
"import_files_uploaded" : "Soubory byly nahrány",
"import_file_supported_formats" : "podporované formáty jsou .json, .xliff, .po",
"import_file_supported_formats" : "podporované formáty jsou .json, .xliff, .po, .properties",
"import_language_select" : "Jazyk",
"import_max_file_count_message" : "Příliš mnoho souborů",
"import_namespace_name_header" : "Namespace",
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/i18n/da.json
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@
"import_file_issue_param_type_value" : "værdi: {value}",
"import_file_issues_title" : "Problemer med filer",
"import_files_uploaded" : "Filer uploaded",
"import_file_supported_formats" : "understøttede formater er .json, .xliff, .po",
"import_file_supported_formats" : "understøttede formater er .json, .xliff, .po, .properties",
"import_language_select" : "Sprog",
"import_max_file_count_message" : "For mange filer",
"import_namespace_name_header" : "Navnerum",
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@
"import_file_issue_param_type_line" : "Zeile: {value}",
"import_file_issue_param_type_value" : "Wert: {value}",
"import_file_issues_title" : "Datei-Probleme",
"import_file_supported_formats" : "unterstützte Formate sind .json, .xliff, .po",
"import_file_supported_formats" : "unterstützte Formate sind .json, .xliff, .po, .properties",
"import_language_select" : "Sprache",
"import_max_file_count_message" : "Zu viele Dateien",
"import_namespace_name_header" : "Namespace",
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@
"import_file_issue_param_type_value" : "value: {value}",
"import_file_issues_title" : "File issues",
"import_files_uploaded" : "Files uploaded",
"import_file_supported_formats" : "supported formats are .json, .xliff, .po",
"import_file_supported_formats" : "supported formats are .json, .xliff, .po, .properties",
"import_language_select" : "Language",
"import_max_file_count_message" : "Too many files",
"import_namespace_name_header" : "Namespace",
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/i18n/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@
"import_file_issue_param_type_line" : "línea: {value}",
"import_file_issue_param_type_value" : "valor: {value}",
"import_file_issues_title" : "Problemas con los archivos",
"import_file_supported_formats" : "los formatos compatibles son .json, .xliff, .po",
"import_file_supported_formats" : "los formatos compatibles son .json, .xliff, .po, .properties",
"import_language_select" : "Idioma",
"import_namespace_name_header" : "Namespace",
"import_not_resolved_error_dialog_cancel_button" : "Volver",
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@
"import_file_issue_param_type_value" : "valeur : {value}",
"import_file_issues_title" : "Problèmes de fichier",
"import_files_uploaded" : "Fichiers téléchargés",
"import_file_supported_formats" : "les formats supportés sont .json, .xliff, .po",
"import_file_supported_formats" : "les formats supportés sont .json, .xliff, .po, .properties",
"import_language_select" : "Langue",
"import_max_file_count_message" : "Trop de fichiers.",
"import_namespace_name_header" : "Namespace",
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/i18n/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@
"import_file_issue_param_type_line" : "linha: {value}",
"import_file_issue_param_type_value" : "valor: {value}",
"import_file_issues_title" : "Problemas com o arquivo",
"import_file_supported_formats" : "os formatos suportados são .json, .xliff, .po",
"import_file_supported_formats" : "os formatos suportados são .json, .xliff, .po, .properties",
"import_language_select" : "Idioma",
"import_namespace_name_header" : "Namespace",
"import_not_resolved_error_dialog_cancel_button" : "Voltar",
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/i18n/ro.json
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@
"import_file_issue_param_type_value" : "valoare: {value}",
"import_file_issues_title" : "Probleme fișier",
"import_files_uploaded" : "Fișierele au fost încărcate",
"import_file_supported_formats" : "Formatele suportate sunt .json, .xliff, .po",
"import_file_supported_formats" : "Formatele suportate sunt .json, .xliff, .po, .properties",
"import_language_select" : "Limba",
"import_max_file_count_message" : "Prea multe fișiere",
"import_namespace_name_header" : "Spațiu de nume",
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/i18n/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@
"import_file_issue_param_type_line" : "行:{value}",
"import_file_issue_param_type_value" : "值:{value}",
"import_file_issues_title" : "文件问题",
"import_file_supported_formats" : "支持的格式为 .json、.xliff、.po",
"import_file_supported_formats" : "支持的格式为 .json、.xliff、.po、.properties",
"import_language_select" : "语言",
"import_max_file_count_message" : "文件太多",
"import_namespace_name_header" : "命名空间",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ const ImportFileInput: FunctionComponent<ImportFileInputProps> = (props) => {
const { t } = useTranslate();
const fileRef = React.createRef<HTMLInputElement>();
const config = useConfig();
const ALLOWED_EXTENSIONS = ['json', 'zip', 'po', 'xliff', 'xlf'];
const ALLOWED_EXTENSIONS = [
'json',
'zip',
'po',
'xliff',
'xlf',
'properties',
];
const [resetKey, setResetKey] = useState(0);
function resetInput() {
setResetKey((key) => key + 1);
Expand Down

0 comments on commit 11dd90f

Please sign in to comment.