Skip to content

Commit

Permalink
fix: correctly handle plurals for languages with single plural form (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Anty0 authored Jan 14, 2025
1 parent 47ff103 commit 05653dc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
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,11 @@ val allPluralKeywords =
PluralRules.KEYWORD_OTHER,
)

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

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

0 comments on commit 05653dc

Please sign in to comment.