8000 fix: import with meta without creating new keys (#3000) by bdshadow · Pull Request #3113 · tolgee/tolgee-platform · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: import with meta without creating new keys (#3000) #3113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class ImportSettingsControllerApplicationTest : ProjectAuthControllerTest("/v2/p
@Value("classpath:import/po/example.po")
lateinit var poFile: Resource

@Value("classpath:import/po/simplePoWithSingleKeyMeta.po")
lateinit var simplePoWithSingleKeyMeta: Resource

@Value("classpath:import/android/strings_params_everywhere.xml")
lateinit var androidFile: Resource

Expand All @@ -44,7 +47,7 @@ class ImportSettingsControllerApplicationTest : ProjectAuthControllerTest("/v2/p
"Willkommen zurück, {0}! Dein letzter Besuch war am {1}",
)

applySettings(overrideKeyDescriptions = false, convertPlaceholdersToIcu = false)
applySettings(overrideKeyDescriptions = false, convertPlaceholdersToIcu = false, createNewKeys = true)
assertTranslation(
"%d page read.",
"{value, plural,\none {Eine Seite gelesen wurde.}\nother {%d Seiten gelesen wurden.}\n}",
Expand Down Expand Up @@ -73,7 +76,7 @@ class ImportSettingsControllerApplicationTest : ProjectAuthControllerTest("/v2/p
"Second item {0, number}",
)
assertTranslation("with_params", "{0, number} {3} {2, number, .00} {3, number, scientific} %+d")
applySettings(overrideKeyDescriptions = false, convertPlaceholdersToIcu = false)
applySettings(overrideKeyDescriptions = false, convertPlaceholdersToIcu = false, createNewKeys = true)
assertTranslation(
"dogs_count",
"{value, plural,\none {%d dog %s}\nother {%d dogs %s}\n}",
Expand Down Expand Up @@ -107,7 +110,7 @@ class ImportSettingsControllerApplicationTest : ProjectAuthControllerTest("/v2/p
"}",
)

applySettings(overrideKeyDescriptions = false, convertPlaceholdersToIcu = false)
applySettings(overrideKeyDescriptions = false, convertPlaceholdersToIcu = false, createNewKeys = true)

assertTranslation(
"Hi %lld",
Expand All @@ -123,6 +126,18 @@ class ImportSettingsControllerApplicationTest : ProjectAuthControllerTest("/v2/p
)
}

@Test
@ProjectJWTAuthTestMethod
fun `(gh-3000) import with createNewKeys=false when first key has meta`() {
saveAndPrepare()
applySettings(overrideKeyDescriptions = false, convertPlaceholdersToIcu = false, createNewKeys = false)
performImport(project.id, listOf("simplePoWithSingleKeyMeta.po" to simplePoWithSingleKeyMeta))
assertTranslation(
"simple message",
"einfache Nachricht",
)
}

@Test
@ProjectJWTAuthTestMethod
fun `doesn't override descriptions when disabled`() {
Expand Down Expand Up @@ -160,7 +175,11 @@ class ImportSettingsControllerApplicationTest : ProjectAuthControllerTest("/v2/p

private fun performImportWithSettings(overrideKeyDescriptions: Boolean) {
performImport(project.id, listOf("example.po" to poFile))
applySettings(overrideKeyDescriptions = overrideKeyDescriptions, convertPlaceholdersToIcu = true)
applySettings(
overrideKeyDescriptions = overrideKeyDescriptions,
convertPlaceholdersToIcu = true,
createNewKeys = true
)
performProjectAuthPut("import/apply?forceMode=OVERRIDE", null).andIsOk
}

Expand All @@ -180,12 +199,14 @@ class ImportSettingsControllerApplicationTest : ProjectAuthControllerTest("/v2/p
private fun applySettings(
overrideKeyDescriptions: Boolean,
convertPlaceholdersToIcu: Boolean,
createNewKeys: Boolean,
) {
performProjectAuthPut(
"import-settings",
mapOf(
"overrideKeyDescriptions" to overrideKeyDescriptions,
"convertPlaceholdersToIcu" to convertPlaceholdersToIcu,
"createNewKeys" to createNewKeys,
),
).andIsOk
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#: light_interface.c:404
msgid "simple message"
msgstr "einfache Nachricht"
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import io.tolgee.model.dataImport.ImportLanguage
import io.tolgee.model.dataImport.ImportTranslation
import io.tolgee.model.dataImport.issues.issueTypes.FileIssueType
import io.tolgee.model.dataImport.issues.paramTypes.FileIssueParamType
import io.tolgee.model.key.KeyMeta
import io.tolgee.service.dataImport.processors.FileProcessorContext
import io.tolgee.service.key.KeyMetaService
import io.tolgee.service.language.LanguageService
import io.tolgee.util.Logging
import io.tolgee.util.filterFiles
Expand All @@ -44,6 +46,9 @@ class CoreImportFilesProcessor(
}
private val tolgeeProperties: TolgeeProperties by lazy { applicationContext.getBean(TolgeeProperties::class.java) }
private val languageService: LanguageService by lazy { applicationContext.getBean(LanguageService::class.java) }
private val keyMetaService: KeyMetaService by lazy {
applicationContext.getBean(KeyMetaService::class.java)
}

private val existingLanguages by lazy {
languageService.getProjectLanguages(projectId = import.project.id)
Expand Down Expand Up @@ -292,13 +297,22 @@ class CoreImportFilesProcessor(
importDataManager.storedTranslations[translation.language]!!.let { it[translation.key]!!.add(translation) }
}

fun saveKeyMeta(keyMeta: KeyMeta) {
keyMeta.disableActivityLogging = true
keyMetaService.save(keyMeta)
keyMetaService.saveAllComments(keyMeta.comments)
keyMetaService.saveAllCodeReferences(keyMeta.codeReferences)
}

private fun FileProcessorContext.getOrCreateKey(name: String): ImportKey {
return importDataManager.storedKeys.computeIfAbsent(this.fileEntity to name) {
this.keys.computeIfAbsent(name) {
ImportKey(name = name, this.fileEntity)
}.also {
it.keyMeta?.also(importDataManager::prepareKeyMeta)
if (saveData) {
importService.saveKey(it)
it.keyMeta?.also(this@CoreImportFilesProcessor::saveKeyMeta)
}
}
}
Expand All @@ -312,10 +326,7 @@ class CoreImportFilesProcessor(
}
keyEntity.shouldBeImported = shouldImportKey(keyEntity.name)
}
importDataManager.prepareKeyMetas()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this methods are unused now, right? So they should be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if (saveData) {
importDataManager.saveAllStoredKeys()
importDataManager.saveAllKeyMetas()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this methods are unused now, right? So they should be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

importDataManager.saveAllStoredTranslations()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,20 +207,9 @@ class ImportDataManager(
this.importService.saveAllKeys(this.storedKeys.values)
}

fun prepareKeyMetas() {
this.storedKeys.mapNotNull { it.value.keyMeta }.forEach { meta ->
meta.comments.onEach { comment -> comment.author = comment.author ?: import.author }
meta.codeReferences.onEach { ref -> ref.author = ref.author ?: import.author }
}
}

fun saveAllKeyMetas() {
this.storedKeys.mapNotNull { it.value.keyMeta }.forEach { meta ->
meta.disableActivityLogging = true
keyMetaService.save(meta)
keyMetaService.saveAllComments(meta.comments)
keyMetaService.saveAllCodeReferences(meta.codeReferences)
}
fun prepareKeyMeta(keyMeta: KeyMeta) {
keyMeta.comments.onEach { comment -> comment.author = comment.author ?: import.author }
keyMeta.codeReferences.onEach { ref -> ref.author = ref.author ?: import.author }
}

private fun resetConflicts(importLanguage: ImportLanguage) {
Expand Down
0