Skip to content
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

fix: correctly handle plurals for languages with single plural form #2820

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -20,6 +20,7 @@ class GenericStructuredProcessor(
context = context,
data = data,
pluralsViaSuffixesParser = format.pluralsViaSuffixesParser,
languageTag = languageTagOrGuess,
).preprocess()
}
processedData.import("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.tolgee.formats.genericStructuredFile.`in`

import io.tolgee.formats.MessageConvertorResult
import io.tolgee.formats.allPluralKeywords
import io.tolgee.formats.getPluralFormsForLocaleOrAll
import io.tolgee.formats.importCommon.ImportFormat
import io.tolgee.formats.importCommon.unwrapString

Expand Down Expand Up @@ -79,7 +80,7 @@ class GenericStructuredRawDataToTextConvertor(
return null
}

if (map.size < 2) {
if (map.size < pluralKeywords.size && map.size < 2) {
return null
}

Expand All @@ -100,4 +101,8 @@ class GenericStructuredRawDataToTextConvertor(

return listOf(converted)
}

private val pluralKeywords by lazy {
getPluralFormsForLocaleOrAll(languageTag)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.tolgee.formats.genericStructuredFile.`in`

import io.tolgee.formats.allPluralKeywords
import io.tolgee.formats.getPluralFormsForLocaleOrAll
import io.tolgee.formats.importCommon.ParsedPluralsKey
import io.tolgee.formats.importCommon.PluralsKeyParser
import io.tolgee.service.dataImport.processors.FileProcessorContext
Expand All @@ -9,6 +10,7 @@ class GenericSuffixedPluralsPreprocessor(
val context: FileProcessorContext,
private val data: Any?,
private val pluralsViaSuffixesParser: PluralsKeyParser,
private val languageTag: String,
) {
fun preprocess(): Any? {
return data.preprocess()
Expand Down Expand Up @@ -68,10 +70,14 @@ class GenericSuffixedPluralsPreprocessor(

private fun Map<*, *>.preprocessMap(): Map<*, *> {
return this.groupByPlurals(pluralsViaSuffixesParser).flatMap { (commonKey, values) ->
if (commonKey == null || values.size < 2) {
if (commonKey == null || (values.size < pluralKeywords.size && values.size < 2)) {
return@flatMap values.useOriginalKey()
}
return@flatMap values.usePluralsKey(commonKey)
}.toMap()
}

private val pluralKeywords by lazy {
getPluralFormsForLocaleOrAll(languageTag)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ val allPluralKeywords =
PluralRules.KEYWORD_OTHER,
)

fun getPluralFormsForLocaleOrAll(languageTag: String): List<String> {
return try {
val locale = getULocaleFromTag(languageTag)
PluralRules.forLocale(locale)?.keywords?.toList() ?: allPluralKeywords
} catch (_: Exception) {
allPluralKeywords
}
}

fun getPluralFormsForLocale(languageTag: String): MutableSet<String> {
val uLocale = getULocaleFromTag(languageTag)
val pluralRules = PluralRules.forLocale(uLocale)
Expand Down
Loading